-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpath.go
145 lines (127 loc) · 3.44 KB
/
path.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
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 xj2go
import (
"regexp"
"strings"
)
type strctNode struct {
Name string
Type string
Tag string
}
// sort.Interface implementation
type byName []strctNode
func (a byName) Len() int { return len(a) }
func (a byName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byName) Less(i, j int) bool { return strings.Compare(a[i].Name, a[j].Name) < 0 }
type strctMap map[string][]strctNode
func leafNodesToStrcts(typ string, nodes []leafNode) []strctMap {
n := max(nodes)
root := strings.Split((nodes)[0].path, ".")[0]
exist := make(map[string]bool)
strct := leafNodesToStruct(typ, root, root, nodes, &exist)
strcts := []strctMap{}
strcts = append(strcts, strct)
es := []string{}
for i := 0; i < n; i++ {
for e := range exist {
es = strings.Split(e, ".")
root = es[len(es)-1]
strct = leafNodesToStruct(typ, e, root, nodes, &exist)
appendStrctNode(&strct, &strcts)
}
}
return strcts
}
var leafNodesToStructRE = regexp.MustCompile(`\[\d+\]`)
func leafNodesToStruct(typ, e, root string, nodes []leafNode, exist *map[string]bool) strctMap {
strct := make(strctMap)
var spath string
re := leafNodesToStructRE
for _, node := range nodes {
if eld := strings.LastIndex(e, "."); eld > 0 {
elp := e[eld:] // with .
if pos := strings.Index(node.path, elp); pos > 0 {
pre := node.path[:pos]
rest := strings.TrimPrefix(node.path, pre)
pre = re.ReplaceAllString(pre, "") // replace [1] with ""
spath = pre + rest
}
} else {
spath = node.path
}
chkpath := strings.TrimPrefix(spath, e)
if !(chkpath == "" || chkpath[:1] == "[" || chkpath[:1] == ".") {
continue
}
if len(spath) > len(e) && spath[:len(e)] == e {
node.path = strings.TrimPrefix(spath, e)
if node.path == "" {
continue
}
node.path = strings.TrimPrefix(node.path, ".")
if node.path[:1] == "[" {
loc := re.FindStringIndex(node.path)
node.path = strings.Replace(node.path, node.path[loc[0]:loc[1]], "", 1)
node.path = strings.TrimPrefix(node.path, ".")
}
if node.path != "" {
leafNodeToStruct(typ, e, root, &node, &strct, exist, re)
}
}
}
return strct
}
func leafNodeToStruct(typ, e, root string, node *leafNode, strct *strctMap, exist *map[string]bool, re *regexp.Regexp) {
s := strings.Split(node.path, ".")
if len(s) >= 1 {
name := re.ReplaceAllString(s[0], "")
ek := string(append(append([]rune(e), []rune(".")...), []rune(name)...))
sname := toProperCase(name)
if !(*exist)[ek] {
sn := strctNode{
Name: sname,
}
if re.MatchString(s[0]) {
if len(s) > 1 {
sn.Type = "[]" + sname
} else {
sn.Type = "[]" + toProperType(node.value)
}
} else {
if len(s) > 1 {
sn.Type = sname
} else {
sn.Type = toProperType(node.value)
}
}
switch node.value.(type) {
case xmlVal:
sn.Tag = "`" + typ + ":\"" + name + ",attr\"`"
default:
sn.Tag = "`" + typ + ":\"" + name + "\"`"
}
/*
if len(s) > 1 {
if re.MatchString(s[0]) {
sn.Type = "[]" + sname
} else {
sn.Type = sname
}
sn.Tag = "`" + typ + ":\"" + name + "\"`"
} else {
sn.Type = toProperType(node.value)
switch node.value.(type) {
case xmlVal:
sn.Tag = "`" + typ + ":\"" + name + ",attr\"`"
// case string:
// sn.Tag = "`" + typ + ":\"" + name + "\"`"
default:
sn.Tag = "`" + typ + ":\"" + name + "\"`"
}
}
*/
(*strct)[root] = append((*strct)[root], sn)
(*exist)[ek] = true
}
}
}