增加formily json schema生成
This commit is contained in:
parent
9d1f77c069
commit
4c79000f30
@ -12,6 +12,7 @@ public @interface FormilyField {
|
|||||||
String title(); // 字段标题
|
String title(); // 字段标题
|
||||||
String description() default ""; // 字段描述
|
String description() default ""; // 字段描述
|
||||||
String type() default "string"; // 字段类型(string/number/boolean/array/object/map)
|
String type() default "string"; // 字段类型(string/number/boolean/array/object/map)
|
||||||
|
int order() default Integer.MAX_VALUE; // 字段顺序,值越小越靠前
|
||||||
|
|
||||||
// 组件属性
|
// 组件属性
|
||||||
String component() default "Input"; // 组件类型
|
String component() default "Input"; // 组件类型
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import java.lang.reflect.Field;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class FormilySchemaFactory {
|
public class FormilySchemaFactory {
|
||||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
@ -46,13 +47,21 @@ public class FormilySchemaFactory {
|
|||||||
ObjectNode properties = schema.putObject("properties");
|
ObjectNode properties = schema.putObject("properties");
|
||||||
boolean hasFields = false;
|
boolean hasFields = false;
|
||||||
|
|
||||||
for (Field field : allFields) {
|
// 将字段按order排序
|
||||||
|
List<Field> sortedFields = allFields.stream()
|
||||||
|
.filter(field -> field.isAnnotationPresent(FormilyField.class))
|
||||||
|
.sorted((f1, f2) -> {
|
||||||
|
FormilyField a1 = f1.getAnnotation(FormilyField.class);
|
||||||
|
FormilyField a2 = f2.getAnnotation(FormilyField.class);
|
||||||
|
return Integer.compare(a1.order(), a2.order());
|
||||||
|
})
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
for (Field field : sortedFields) {
|
||||||
FormilyField fieldDef = field.getAnnotation(FormilyField.class);
|
FormilyField fieldDef = field.getAnnotation(FormilyField.class);
|
||||||
if (fieldDef != null) {
|
|
||||||
properties.set(field.getName(), generateFieldSchema(fieldDef));
|
properties.set(field.getName(), generateFieldSchema(fieldDef));
|
||||||
hasFields = true;
|
hasFields = true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!hasFields) {
|
if (!hasFields) {
|
||||||
throw new IllegalArgumentException("No @FormilyField annotations found in class hierarchy: " + clazz.getName());
|
throw new IllegalArgumentException("No @FormilyField annotations found in class hierarchy: " + clazz.getName());
|
||||||
|
|||||||
@ -4,6 +4,10 @@ import com.fasterxml.jackson.databind.JsonNode;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
@ -18,6 +22,14 @@ public class FormilySchemaFactoryTest {
|
|||||||
assertEquals("测试表单", schema.get("name").asText());
|
assertEquals("测试表单", schema.get("name").asText());
|
||||||
assertTrue(schema.has("properties"));
|
assertTrue(schema.has("properties"));
|
||||||
|
|
||||||
|
// 验证字段顺序
|
||||||
|
Iterator<String> fieldNames = schema.get("properties").fieldNames();
|
||||||
|
List<String> actualOrder = new ArrayList<>();
|
||||||
|
fieldNames.forEachRemaining(actualOrder::add);
|
||||||
|
|
||||||
|
List<String> expectedOrder = Arrays.asList("a", "b", "c", "envVars", "script");
|
||||||
|
assertEquals(expectedOrder, actualOrder, "字段顺序应该按照order排序");
|
||||||
|
|
||||||
// 验证字段A
|
// 验证字段A
|
||||||
JsonNode fieldA = schema.get("properties").get("a");
|
JsonNode fieldA = schema.get("properties").get("a");
|
||||||
assertEquals("string", fieldA.get("type").asText());
|
assertEquals("string", fieldA.get("type").asText());
|
||||||
@ -136,6 +148,7 @@ class TestForm {
|
|||||||
@FormilyField(
|
@FormilyField(
|
||||||
title = "选择A",
|
title = "选择A",
|
||||||
component = "Select",
|
component = "Select",
|
||||||
|
order = 1,
|
||||||
props = @FormilyComponentProps(
|
props = @FormilyComponentProps(
|
||||||
api = "/api/options/A",
|
api = "/api/options/A",
|
||||||
labelField = "name",
|
labelField = "name",
|
||||||
@ -154,6 +167,7 @@ class TestForm {
|
|||||||
@FormilyField(
|
@FormilyField(
|
||||||
title = "选择B",
|
title = "选择B",
|
||||||
component = "Select",
|
component = "Select",
|
||||||
|
order = 2,
|
||||||
props = @FormilyComponentProps(
|
props = @FormilyComponentProps(
|
||||||
labelField = "viewName",
|
labelField = "viewName",
|
||||||
valueField = "id"
|
valueField = "id"
|
||||||
@ -177,6 +191,7 @@ class TestForm {
|
|||||||
@FormilyField(
|
@FormilyField(
|
||||||
title = "选择C",
|
title = "选择C",
|
||||||
component = "Select",
|
component = "Select",
|
||||||
|
order = 3,
|
||||||
props = @FormilyComponentProps(
|
props = @FormilyComponentProps(
|
||||||
labelField = "jobName",
|
labelField = "jobName",
|
||||||
valueField = "id"
|
valueField = "id"
|
||||||
@ -197,10 +212,31 @@ class TestForm {
|
|||||||
)
|
)
|
||||||
private String c;
|
private String c;
|
||||||
|
|
||||||
|
@FormilyField(
|
||||||
|
title = "环境变量",
|
||||||
|
type = "map",
|
||||||
|
order = 4,
|
||||||
|
mapConfig = @FormilyMapConfig(
|
||||||
|
keyTitle = "变量名",
|
||||||
|
valueTitle = "变量值",
|
||||||
|
keyComponent = "Select",
|
||||||
|
valueComponent = "Input",
|
||||||
|
keyProps = @FormilyComponentProps(
|
||||||
|
api = "/api/v1/env-vars/keys",
|
||||||
|
showSearch = true,
|
||||||
|
allowClear = true
|
||||||
|
),
|
||||||
|
addText = "添加环境变量",
|
||||||
|
allowCustomKey = true
|
||||||
|
)
|
||||||
|
)
|
||||||
|
private Map<String, String> envVars;
|
||||||
|
|
||||||
@FormilyField(
|
@FormilyField(
|
||||||
title = "Pipeline script",
|
title = "Pipeline script",
|
||||||
type = "string",
|
type = "string",
|
||||||
component = "MonacoEditor",
|
component = "MonacoEditor",
|
||||||
|
order = 5,
|
||||||
props = @FormilyComponentProps(
|
props = @FormilyComponentProps(
|
||||||
editor = @FormilyEditorProps(
|
editor = @FormilyEditorProps(
|
||||||
language = "groovy",
|
language = "groovy",
|
||||||
@ -223,23 +259,4 @@ class TestForm {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
private String script;
|
private String script;
|
||||||
|
|
||||||
@FormilyField(
|
|
||||||
title = "环境变量",
|
|
||||||
type = "map",
|
|
||||||
mapConfig = @FormilyMapConfig(
|
|
||||||
keyTitle = "变量名",
|
|
||||||
valueTitle = "变量值",
|
|
||||||
keyComponent = "Select",
|
|
||||||
valueComponent = "Input",
|
|
||||||
keyProps = @FormilyComponentProps(
|
|
||||||
api = "/api/v1/env-vars/keys",
|
|
||||||
showSearch = true,
|
|
||||||
allowClear = true
|
|
||||||
),
|
|
||||||
addText = "添加环境变量",
|
|
||||||
allowCustomKey = true
|
|
||||||
)
|
|
||||||
)
|
|
||||||
private Map<String, String> envVars;
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user