增加formily json schema生成
This commit is contained in:
parent
5cd9f4c4d3
commit
9d1f77c069
@ -11,7 +11,7 @@ public @interface FormilyField {
|
||||
// 基础属性
|
||||
String title(); // 字段标题
|
||||
String description() default ""; // 字段描述
|
||||
String type() default "string"; // 字段类型(string/number/boolean/array/object)
|
||||
String type() default "string"; // 字段类型(string/number/boolean/array/object/map)
|
||||
|
||||
// 组件属性
|
||||
String component() default "Input"; // 组件类型
|
||||
@ -29,4 +29,7 @@ public @interface FormilyField {
|
||||
String pattern() default "editable"; // 展示模式(editable/disabled/readOnly)
|
||||
String display() default "visible"; // 显示模式(visible/hidden/none)
|
||||
boolean hidden() default false; // 是否隐藏
|
||||
|
||||
// Map类型配置
|
||||
FormilyMapConfig mapConfig() default @FormilyMapConfig; // Map配置
|
||||
}
|
||||
@ -0,0 +1,65 @@
|
||||
package com.qqchen.deploy.backend.framework.annotation.formily;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface FormilyMapConfig {
|
||||
/**
|
||||
* 键的标题
|
||||
*/
|
||||
String keyTitle() default "键";
|
||||
|
||||
/**
|
||||
* 值的标题
|
||||
*/
|
||||
String valueTitle() default "值";
|
||||
|
||||
/**
|
||||
* 键的组件类型
|
||||
*/
|
||||
String keyComponent() default "Input";
|
||||
|
||||
/**
|
||||
* 值的组件类型
|
||||
*/
|
||||
String valueComponent() default "Input";
|
||||
|
||||
/**
|
||||
* 键的组件属性
|
||||
*/
|
||||
FormilyComponentProps keyProps() default @FormilyComponentProps;
|
||||
|
||||
/**
|
||||
* 值的组件属性
|
||||
*/
|
||||
FormilyComponentProps valueProps() default @FormilyComponentProps;
|
||||
|
||||
/**
|
||||
* 是否允许添加
|
||||
*/
|
||||
boolean allowAdd() default true;
|
||||
|
||||
/**
|
||||
* 是否允许删除
|
||||
*/
|
||||
boolean allowRemove() default true;
|
||||
|
||||
/**
|
||||
* 是否允许自定义键
|
||||
*/
|
||||
boolean allowCustomKey() default true;
|
||||
|
||||
/**
|
||||
* 添加按钮文本
|
||||
*/
|
||||
String addText() default "添加";
|
||||
|
||||
/**
|
||||
* 删除按钮文本
|
||||
*/
|
||||
String removeText() default "删除";
|
||||
}
|
||||
@ -108,13 +108,65 @@ public class FormilySchemaFactory {
|
||||
|
||||
// 组件属性
|
||||
fieldSchema.put("x-decorator", field.decorator());
|
||||
fieldSchema.put("x-component", field.component());
|
||||
if ("map".equals(field.type())) {
|
||||
// 对于Map类型,使用ArrayItems组件
|
||||
fieldSchema.put("x-component", "ArrayItems");
|
||||
|
||||
// 生成Map配置
|
||||
ObjectNode componentProps = generateMapComponentProps(field.mapConfig());
|
||||
if (componentProps.size() > 0) {
|
||||
fieldSchema.set("x-component-props", componentProps);
|
||||
}
|
||||
|
||||
// 生成Map项的Schema
|
||||
ObjectNode items = fieldSchema.putObject("items");
|
||||
items.put("type", "object");
|
||||
items.put("x-component", "ArrayItems.Item");
|
||||
|
||||
// 生成Map项的属性Schema
|
||||
ObjectNode properties = items.putObject("properties");
|
||||
|
||||
// 键的Schema
|
||||
ObjectNode keySchema = properties.putObject("key");
|
||||
keySchema.put("type", "string");
|
||||
keySchema.put("title", field.mapConfig().keyTitle());
|
||||
keySchema.put("x-decorator", "FormItem");
|
||||
keySchema.put("x-component", field.mapConfig().keyComponent());
|
||||
ObjectNode keyProps = generateComponentProps(field.mapConfig().keyProps(), field.mapConfig().keyComponent());
|
||||
if (keyProps.size() > 0) {
|
||||
keySchema.set("x-component-props", keyProps);
|
||||
}
|
||||
|
||||
// 值的Schema
|
||||
ObjectNode valueSchema = properties.putObject("value");
|
||||
valueSchema.put("type", "string");
|
||||
valueSchema.put("title", field.mapConfig().valueTitle());
|
||||
valueSchema.put("x-decorator", "FormItem");
|
||||
valueSchema.put("x-component", field.mapConfig().valueComponent());
|
||||
ObjectNode valueProps = generateComponentProps(field.mapConfig().valueProps(), field.mapConfig().valueComponent());
|
||||
if (valueProps.size() > 0) {
|
||||
valueSchema.set("x-component-props", valueProps);
|
||||
}
|
||||
|
||||
// 操作按钮
|
||||
ObjectNode operationSchema = properties.putObject("operations");
|
||||
operationSchema.put("type", "void");
|
||||
operationSchema.put("x-component", "ArrayItems.Remove");
|
||||
operationSchema.put("x-component-props", field.mapConfig().removeText());
|
||||
|
||||
// 添加按钮
|
||||
ObjectNode addition = fieldSchema.putObject("properties").putObject("addition");
|
||||
addition.put("type", "void");
|
||||
addition.put("title", field.mapConfig().addText());
|
||||
addition.put("x-component", "ArrayItems.Addition");
|
||||
} else {
|
||||
fieldSchema.put("x-component", field.component());
|
||||
// 组件属性配置
|
||||
ObjectNode componentProps = generateComponentProps(field.props(), field.component());
|
||||
if (componentProps.size() > 0) {
|
||||
fieldSchema.set("x-component-props", componentProps);
|
||||
}
|
||||
}
|
||||
|
||||
// 装饰器属性配置
|
||||
ObjectNode decoratorProps = generateDecoratorProps(field.decoratorProps());
|
||||
@ -146,6 +198,16 @@ public class FormilySchemaFactory {
|
||||
return fieldSchema;
|
||||
}
|
||||
|
||||
private static ObjectNode generateMapComponentProps(FormilyMapConfig config) {
|
||||
ObjectNode props = objectMapper.createObjectNode();
|
||||
|
||||
props.put("allowAdd", config.allowAdd());
|
||||
props.put("allowRemove", config.allowRemove());
|
||||
props.put("allowCustomKey", config.allowCustomKey());
|
||||
|
||||
return props;
|
||||
}
|
||||
|
||||
private static ObjectNode generateComponentProps(FormilyComponentProps props, String componentType) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
|
||||
|
||||
@ -2,6 +2,9 @@ package com.qqchen.deploy.backend.framework.annotation.formily;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class FormilySchemaFactoryTest {
|
||||
@ -80,6 +83,49 @@ public class FormilySchemaFactoryTest {
|
||||
assertTrue(options.get("folding").asBoolean());
|
||||
assertEquals("请输入Pipeline脚本", scriptProps.get("placeholder").asText());
|
||||
|
||||
// 验证Map字段
|
||||
JsonNode mapField = schema.get("properties").get("envVars");
|
||||
assertEquals("map", mapField.get("type").asText());
|
||||
assertEquals("环境变量", mapField.get("title").asText());
|
||||
assertEquals("ArrayItems", mapField.get("x-component").asText());
|
||||
|
||||
// 验证Map项配置
|
||||
JsonNode items = mapField.get("items");
|
||||
assertEquals("object", items.get("type").asText());
|
||||
assertEquals("ArrayItems.Item", items.get("x-component").asText());
|
||||
|
||||
// 验证键值配置
|
||||
JsonNode properties = items.get("properties");
|
||||
|
||||
// 验证键配置
|
||||
JsonNode keySchema = properties.get("key");
|
||||
assertEquals("string", keySchema.get("type").asText());
|
||||
assertEquals("变量名", keySchema.get("title").asText());
|
||||
assertEquals("Select", keySchema.get("x-component").asText());
|
||||
|
||||
// 验证键的组件属性
|
||||
JsonNode keyProps = keySchema.get("x-component-props");
|
||||
assertEquals("/api/v1/env-vars/keys", keyProps.get("api").asText());
|
||||
assertTrue(keyProps.get("showSearch").asBoolean());
|
||||
assertTrue(keyProps.get("allowClear").asBoolean());
|
||||
|
||||
// 验证值配置
|
||||
JsonNode valueSchema = properties.get("value");
|
||||
assertEquals("string", valueSchema.get("type").asText());
|
||||
assertEquals("变量值", valueSchema.get("title").asText());
|
||||
assertEquals("Input", valueSchema.get("x-component").asText());
|
||||
|
||||
// 验证操作按钮
|
||||
JsonNode operations = properties.get("operations");
|
||||
assertEquals("void", operations.get("type").asText());
|
||||
assertEquals("ArrayItems.Remove", operations.get("x-component").asText());
|
||||
|
||||
// 验证添加按钮
|
||||
JsonNode addition = mapField.get("properties").get("addition");
|
||||
assertEquals("void", addition.get("type").asText());
|
||||
assertEquals("添加环境变量", addition.get("title").asText());
|
||||
assertEquals("ArrayItems.Addition", addition.get("x-component").asText());
|
||||
|
||||
// 打印生成的Schema以便查看
|
||||
System.out.println(schema.toPrettyString());
|
||||
}
|
||||
@ -177,4 +223,23 @@ class TestForm {
|
||||
}
|
||||
)
|
||||
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