feat: 提供诸多简化配置,更轻松实现功能

This commit is contained in:
wangyu 2021-09-05 00:32:01 +08:00
parent 2722a86949
commit 4909c17f90
4 changed files with 47 additions and 2 deletions

View File

@ -0,0 +1,15 @@
package com.flyfish.framework.annotations;
import java.lang.annotation.*;
/**
* 排序用
* @author wangyu
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Order {
int value() default 0;
}

View File

@ -21,4 +21,12 @@ public @interface PropertyGroup {
* @return 结果
*/
String code();
/**
* 是否默认
* 指定为true默认将未指定分组的项归到该类
*
* @return 结果
*/
boolean initial() default false;
}

View File

@ -1,5 +1,6 @@
package com.flyfish.framework.beans.meta;
import com.flyfish.framework.annotations.PropertyGroup;
import com.flyfish.framework.annotations.PropertyGroups;
import com.flyfish.framework.bean.Result;
import com.flyfish.framework.beans.resolver.DynamicRestBeanResolver;
@ -90,12 +91,21 @@ public class BeanController {
} else {
info.setCode(StringFormats.camel2Line(ClassUtils.getShortClassName(clazz)));
}
// 解析属性
info.setProperties(BeanProperty.from(clazz));
// 设置分组信息
if (clazz.isAnnotationPresent(PropertyGroups.class)) {
PropertyGroups groups = clazz.getAnnotation(PropertyGroups.class);
info.setGroups(Arrays.stream(groups.value()).map(group -> new BeanPropertyGroup(group.name(), group.code()))
.collect(Collectors.toList()));
// 尝试补全分组
Arrays.stream(groups.value()).filter(PropertyGroup::initial).findFirst()
.ifPresent(initial -> info.getProperties()
.stream()
.filter(property -> StringUtils.isBlank(property.getGroup()))
.forEach(property -> property.setGroup(initial.code()))
);
}
info.setProperties(BeanProperty.from(clazz));
return info;
}

View File

@ -9,6 +9,7 @@ import com.flyfish.framework.utils.ReflectionUtils;
import com.flyfish.framework.utils.StringFormats;
import lombok.Data;
import lombok.val;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils;
@ -90,7 +91,13 @@ public class BeanProperty {
property.setTitle(props.inherited() ? parentName + props.title() : props.title());
property.setDescription(props.description());
property.setReadonly(props.readonly());
property.setOrder(props.order());
property.setGroup(props.group());
Order order = AnnotationUtils.findAnnotation(field, Order.class);
if (null != order) {
property.setOrder(order.value());
} else {
property.setOrder(props.order());
}
} else if (strict) {
property.setReadonly(true);
return property;
@ -201,7 +208,9 @@ public class BeanProperty {
.map(descriptor -> BeanProperty.form(descriptor, clazz))
.filter(property -> !property.isReadonly())
.collect(Collectors.toList());
// 这里进行一些修复和初始化
List<BeanProperty> result = ListUtils.union(parseExtras(clazz, properties), properties);
// 排序
result.sort(Comparator.comparingInt(a -> a.order));
return result;
}
@ -241,6 +250,9 @@ public class BeanProperty {
if (prop.order() != 0) {
property.order = prop.order();
}
if (StringUtils.isNotBlank(prop.group())) {
property.group = prop.group();
}
} else {
BeanProperty property = new BeanProperty();
property.setType(BeanPropertyType.STRING);