yangys
2024-04-07 a87afa1be0961724a05cdf14321f0d8fd55efb98
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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();
    }
}