package com.qianwen.core.tool.metadata.types;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Optional;
|
import java.util.function.BiFunction;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import com.qianwen.core.tool.metadata.Converter;
|
import com.qianwen.core.tool.metadata.DataType;
|
import com.qianwen.core.tool.metadata.PropertyMetadata;
|
import com.qianwen.core.tool.metadata.SimplePropertyMetadata;
|
import com.qianwen.core.tool.metadata.ValidateResult;
|
import com.qianwen.core.tool.utils.BeanUtil;
|
import com.qianwen.core.tool.utils.StringPool;
|
import org.springframework.util.CollectionUtils;
|
|
/* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/metadata/types/ObjectType.class */
|
public class ObjectType extends AbstractType<ObjectType> implements DataType, Converter<Map<String, Object>> {
|
private static final Logger log = LoggerFactory.getLogger(ObjectType.class);
|
public static final String ID = "object";
|
private List<PropertyMetadata> properties;
|
|
public void setProperties(final List<PropertyMetadata> properties) {
|
this.properties = properties;
|
}
|
|
@Override // org.springblade.core.tool.metadata.Metadata
|
public String getId() {
|
return ID;
|
}
|
|
@Override // org.springblade.core.tool.metadata.Metadata
|
public String getName() {
|
return "对象类型";
|
}
|
|
public ObjectType addPropertyMetadata(PropertyMetadata property) {
|
if (this.properties == null) {
|
this.properties = new ArrayList<>();
|
}
|
this.properties.add(property);
|
return this;
|
}
|
|
public List<PropertyMetadata> getProperties() {
|
if (this.properties == null) {
|
return Collections.emptyList();
|
}
|
return this.properties;
|
}
|
|
public ObjectType addProperty(String property, DataType type) {
|
return addProperty(property, property, type);
|
}
|
|
public ObjectType addProperty(String property, String name, DataType type) {
|
SimplePropertyMetadata metadata = new SimplePropertyMetadata();
|
metadata.setId(property);
|
metadata.setName(name);
|
metadata.setValueType(type);
|
return addPropertyMetadata(metadata);
|
}
|
|
@Override // org.springblade.core.tool.metadata.DataType
|
public ValidateResult validate(Object value) {
|
if (this.properties == null || this.properties.isEmpty()) {
|
return ValidateResult.success(value);
|
}
|
Map<String, Object> mapValue = convert(value);
|
for (PropertyMetadata property : this.properties) {
|
Object data = mapValue.get(property.getId());
|
if (data != null) {
|
ValidateResult result = property.getValueType().validate(data);
|
if (!result.isSuccess()) {
|
return result;
|
}
|
}
|
}
|
return ValidateResult.success(mapValue);
|
}
|
|
@Override // org.springblade.core.tool.metadata.FormatSupport
|
public JSONObject format(Object value) {
|
return new JSONObject(handle(value, (v0, v1) -> {
|
return v0.format(v1);
|
}));
|
}
|
|
public Map<String, Object> handle(Object value, BiFunction<DataType, Object, Object> mapping) {
|
if (value == null) {
|
return null;
|
}
|
if ((value instanceof String) && ((String) value).startsWith(StringPool.LEFT_BRACE)) {
|
value = JSON.parseObject(String.valueOf(value));
|
}
|
if (!(value instanceof Map)) {
|
//value = BeanUtil.copy(value, (Class<Object>) HashMap.class);
|
value = BeanUtil.copy(value, HashMap.class);
|
}
|
if (value instanceof Map) {
|
Map<String, Object> mapValue = new HashMap<>((Map<String,Object>) value);
|
//Map<String, Object> mapValue = new HashMap<>((Map<? extends String, ?>)value);
|
if (this.properties != null) {
|
for (PropertyMetadata property : this.properties) {
|
Object data = mapValue.get(property.getId());
|
DataType type = property.getValueType();
|
if (data != null) {
|
mapValue.put(property.getId(), mapping.apply(type, data));
|
}
|
}
|
}
|
return mapValue;
|
}
|
return null;
|
}
|
|
/* JADX WARN: Can't rename method to resolve collision */
|
@Override // org.springblade.core.tool.metadata.Converter
|
public Map<String, Object> convert(Object value) {
|
return handle(value, (type, data) -> (type instanceof Converter) ? ((Converter)type).convert(data) : data);
|
/*
|
return handle(value, type, data -> {
|
if (type instanceof Converter) {
|
return ((Converter) type).convert(data);
|
}
|
return data;
|
});
|
*/
|
}
|
|
public Optional<PropertyMetadata> getProperty(String key) {
|
if (CollectionUtils.isEmpty(this.properties)) {
|
return Optional.empty();
|
}
|
return this.properties.stream().filter(prop -> {
|
return prop.getId().equals(key);
|
}).findAny();
|
}
|
}
|