feiyu02
2025-04-11 635d762aef37b5de6cd2e34f4a076ab56d9a239d
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
package com.flightfeather.uav.common.api2word.utils;
 
 
import com.flightfeather.uav.common.api2word.model.ModelAttr;
import com.github.jknack.handlebars.internal.lang3.StringUtils;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
 
/**
 * @author ivenhan
 * @Date: 2020/10/15
 */
 
public class ModelAttrUtils {
 
    // 封装schema - properties下某个具体property对象
    public static ModelAttr propertyModelAttr(Map<String, Map<String, Object>> property) {
        ModelAttr modeAttr = new ModelAttr();
 
        Map<String, Object> modeProperties = (Map<String, Object>) property.get("properties");
        ArrayList modeRequired = (ArrayList) property.get("required");
        List<ModelAttr> attrList = new ArrayList<>();
 
        if (modeProperties != null) {
            Iterator<Entry<String, Object>> mIt = modeProperties.entrySet().iterator();
 
            //解析属性
            while (mIt.hasNext()) {
                Entry<String, Object> mEntry = mIt.next();
                Map<String, Object> attrInfoMap = (Map<String, Object>) mEntry.getValue();
                ModelAttr child = new ModelAttr();
                child.setName(mEntry.getKey());
                child.setType((String) attrInfoMap.get("type"));
                if (attrInfoMap.get("format") != null) {
                    child.setType(child.getType() + "(" + attrInfoMap.get("format") + ")");
                }
                child.setType(StringUtils.defaultIfBlank(child.getType(), "object"));
 
                Object ref = attrInfoMap.get("$ref");
                Object items = attrInfoMap.get("items");
 
                if (items != null && ((Map) items).get("$ref") == null) {
                    ModelAttr refModel = propertyModelAttr((Map<String, Map<String, Object>>)items);
                    if (refModel != null) {
                        child.setProperties(refModel.getProperties());
                    }
                    child.setType(child.getType());
                }
 
                child.setDescription((String) attrInfoMap.get("description"));
 
                child.setRequire(false);
                if (modeRequired != null && modeRequired.contains(mEntry.getKey())) {
                    child.setRequire(true);
                }
 
                attrList.add(child);
            }
        }
 
        Object title = property.get("title");
        Object description = property.get("description");
        modeAttr.setClassName(title == null ? "" : title.toString());
        modeAttr.setDescription(description == null ? "" : description.toString());
        modeAttr.setProperties(attrList);
        return modeAttr;
    }
}