-
Notifications
You must be signed in to change notification settings - Fork 19
/
map.go
60 lines (48 loc) · 1.22 KB
/
map.go
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
package json2go
import "strings"
func convertViableObjectsToMaps(root *node, minAttributes uint) {
for _, c := range root.children {
if c.t.id() != nodeTypeObject.id() {
continue
}
convertViableObjectsToMaps(c, minAttributes)
if tryConvertToMap(c, minAttributes) {
continue
}
}
tryConvertToMap(root, minAttributes)
}
func tryConvertToMap(n *node, minAttributes uint) bool {
if len(n.children) < int(minAttributes) {
return false
}
if len(n.children) < 1 {
return false
}
// Children has to have same type and structure.
t := n.children[0].t
sid := mergeNumsStructureID(n.children[0], false)
for _, c := range n.children {
if !t.expands(c.t) && !c.t.expands(t) {
return false
}
if mergeNumsStructureID(c, false) != sid {
return false
}
}
// Convert this node to map.
n.t = nodeTypeMap
// Add child as map value type node
newNode := mergeNodes(n.children)
newNode.key = ""
newNode.name = ""
newNode.required = true
n.children = []*node{newNode}
return true
}
func mergeNumsStructureID(n *node, withKey bool) string {
sid := structureID(n, withKey)
sid = strings.Replace(sid, nodeTypeInt.id(), "number", -1)
sid = strings.Replace(sid, nodeTypeFloat.id(), "number", -1)
return sid
}