-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathprintGoStructVisitor.go
196 lines (159 loc) · 5.52 KB
/
printGoStructVisitor.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"fmt"
"io"
"sort"
//"strconv"
)
type PrintGoStructVisitor struct {
alreadyVisited map[string]bool
alreadyVisitedNodes map[string]*Node
globalTagAttributes map[string]([]*FQN)
maxDepth int
depth int
nameSpaceTagMap map[string]string
useType bool
nameSpaceInJsonName bool
writer io.Writer
}
func (v *PrintGoStructVisitor) init(writer io.Writer, maxDepth int, globalTagAttributes map[string]([]*FQN), nameSpaceTagMap map[string]string, useType bool, nameSpaceInJsonName bool) {
v.alreadyVisited = make(map[string]bool)
v.alreadyVisitedNodes = make(map[string]*Node)
v.globalTagAttributes = make(map[string]([]*FQN))
v.globalTagAttributes = globalTagAttributes
v.writer = writer
v.maxDepth = maxDepth
v.depth = 0
v.nameSpaceTagMap = nameSpaceTagMap
v.useType = useType
v.nameSpaceInJsonName = nameSpaceInJsonName
}
func (v *PrintGoStructVisitor) Visit(node *Node) bool {
v.depth += 1
if v.AlreadyVisited(node) || node.ignoredTag {
v.depth += 1
return false
}
v.SetAlreadyVisited(node)
for _, child := range node.children {
v.Visit(child)
}
v.depth += 1
return true
}
func print(v *PrintGoStructVisitor, node *Node) error {
if node.ignoredTag || node.name == "" {
return nil
}
if flattenStrings && isStringOnlyField(node, len(v.globalTagAttributes[nk(node)])) {
//v.lineChannel <- "//type " + node.makeType(namePrefix, nameSuffix)
return nil
}
attributes := v.globalTagAttributes[nk(node)]
//v.lineChannel <- "type " + node.makeType(namePrefix, nameSuffix) + " struct {"
fmt.Fprintln(v.writer, "type "+node.makeType(namePrefix, nameSuffix)+" struct {")
// fmt.Fprintln(v.writer, "\tXMLName xml.Name`"+makeXmlAnnotation(node.space, false, node.name)+" "+makeJsonAnnotation(node.spaceTag, false, node.name)+"`")
fmt.Fprintln(v.writer, "\tXMLName xml.Name `"+makeAnnotation("xml", node.space, false, false, node.name)+" "+makeJsonAnnotation(node.spaceTag, false, node.name)+"`")
//return makeAnnotation("xml", spaceTag, true, false, name)
makeAttributes(v.writer, attributes, v.nameSpaceTagMap)
err := v.printInternalFields(len(attributes), node)
if err != nil {
return err
}
//v.lineChannel <- "}\n"
fmt.Fprintln(v.writer, "}")
fmt.Fprintln(v.writer, "")
return nil
}
func (v *PrintGoStructVisitor) AlreadyVisited(n *Node) bool {
_, ok := v.alreadyVisited[nk(n)]
return ok
}
func (v *PrintGoStructVisitor) SetAlreadyVisited(n *Node) {
v.alreadyVisited[nk(n)] = true
v.alreadyVisitedNodes[nk(n)] = n
}
func (v *PrintGoStructVisitor) printInternalFields(nattributes int, n *Node) error {
var fields []string
// Fields in this struct
for i, _ := range n.children {
child := n.children[i]
if child.ignoredTag {
continue
}
var def FieldDef
if flattenStrings && isStringOnlyField(child, len(v.globalTagAttributes[nk(child)])) {
//field = "\t" + child.spaceTag + child.makeType(namePrefix, nameSuffix) + " string `" + makeXmlAnnotation(child.space, false, child.name) + "`" //+ " // ********* " + lengthTagName + ":\"" + lengthTagAttribute + lengthTagSeparator + strconv.FormatInt(child.nodeTypeInfo.maxLength+lengthTagPadding, 10) + "\""
def.GoName = child.makeType(namePrefix, nameSuffix)
//def.GoType = "string"
def.GoType = findType(child.nodeTypeInfo, useType)
def.XMLName = child.name
def.XMLNameSpace = child.space
} else {
// Field name and type are the same: i.e. Person *Person or Persons []Persons
nameAndType := child.makeType(namePrefix, nameSuffix)
def.GoName = nameAndType
def.GoType = nameAndType
def.XMLName = child.name
def.XMLNameSpace = child.space
if child.repeats {
def.GoTypeArrayOrPointer = "[]*"
} else {
def.GoTypeArrayOrPointer = "*"
}
}
if flattenStrings {
def.Length = child.nodeTypeInfo.maxLength
}
fieldDefString, err := render(def)
if err != nil {
return err
}
fields = append(fields, fieldDefString)
}
// Is this chardata Field (string)
if n.hasCharData {
xmlString := " `xml:\",chardata\" " + makeJsonAnnotation("", false, "") + "`"
thisType := findType(n.nodeTypeInfo, useType)
thisVariableName := findFieldNameFromTypeInfo(thisType)
charField := "\t" + thisVariableName + " " + thisType + xmlString
if flattenStrings {
//charField += "// maxLength=" + strconv.FormatInt(n.nodeTypeInfo.maxLength, 10)
if len(n.children) == 0 && nattributes == 0 {
charField += "// *******************"
}
}
//GOOD
//charField += " // maxLength=" + strconv.FormatInt(n.nodeTypeInfo.maxLength, 10)
fields = append(fields, charField)
}
sort.Strings(fields)
for i := 0; i < len(fields); i++ {
//v.lineChannel <- fields[i]
fmt.Fprintln(v.writer, fields[i])
}
return nil
}
func makeJsonAnnotation(spaceTag string, useSpaceTagInName bool, name string) string {
return makeAnnotation("json", spaceTag, false, useSpaceTagInName, name)
}
func makeXmlAnnotation(spaceTag string, useSpaceTag bool, name string) string {
return makeAnnotation("xml", spaceTag, true, false, name)
}
func makeDbAnnotation(spaceTag string, useSpaceTag bool, name string) string {
return makeAnnotation("db", spaceTag, true, false, name)
}
func makeAnnotation(annotationId string, spaceTag string, useSpaceTag bool, useSpaceTagInName bool, name string) (annotation string) {
annotation = annotationId + ":\""
if useSpaceTag {
annotation = annotation + spaceTag
annotation = annotation + " "
}
if useSpaceTagInName {
if spaceTag != "" {
annotation = annotation + spaceTag
}
}
annotation = annotation + name + ",omitempty\""
return annotation
}