-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
genGo.go
338 lines (318 loc) · 10.9 KB
/
genGo.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright 2020 - 2024 The xgen Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
//
// Package xgen written in pure Go providing a set of functions that allow you
// to parse XSD (XML schema files). This library needs Go version 1.10 or
// later.
package xgen
import (
"fmt"
"go/format"
"os"
"reflect"
"strings"
)
// CodeGenerator holds code generator overrides and runtime data that are used
// when generate code from proto tree.
type CodeGenerator struct {
Lang string
File string
Field string
Package string
ImportTime bool // For Go language
ImportEncodingXML bool // For Go language
ProtoTree []interface{}
StructAST map[string]string
}
var goBuildinType = map[string]bool{
"xml.Name": true,
"byte": true,
"[]byte": true,
"bool": true,
"[]bool": true,
"complex64": true,
"complex128": true,
"float32": true,
"float64": true,
"int": true,
"int8": true,
"int16": true,
"int32": true,
"int64": true,
"interface": true,
"[]interface{}": true,
"string": true,
"[]string": true,
"time.Time": true,
"uint": true,
"uint8": true,
"uint16": true,
"uint32": true,
"uint64": true,
}
// GenGo generate Go programming language source code for XML schema
// definition files.
func (gen *CodeGenerator) GenGo() error {
fieldNameCount = make(map[string]int)
for _, ele := range gen.ProtoTree {
if ele == nil {
continue
}
funcName := fmt.Sprintf("Go%s", reflect.TypeOf(ele).String()[6:])
callFuncByName(gen, funcName, []reflect.Value{reflect.ValueOf(ele)})
}
f, err := os.Create(gen.FileWithExtension(".go"))
if err != nil {
return err
}
defer f.Close()
var importPackage, packages string
if gen.ImportTime {
packages += "\t\"time\"\n"
}
if gen.ImportEncodingXML {
packages += "\t\"encoding/xml\"\n"
}
if packages != "" {
importPackage = fmt.Sprintf("import (\n%s)", packages)
}
packageName := gen.Package
if packageName == "" {
packageName = "schema"
}
source, err := format.Source([]byte(fmt.Sprintf("%s\n\npackage %s\n%s%s", copyright, packageName, importPackage, gen.Field)))
if err != nil {
f.WriteString(fmt.Sprintf("package %s\n%s%s", packageName, importPackage, gen.Field))
return err
}
f.Write(source)
return err
}
func genGoFieldName(name string, unique bool) (fieldName string) {
for _, str := range strings.Split(name, ":") {
fieldName += MakeFirstUpperCase(str)
}
var tmp string
for _, str := range strings.Split(fieldName, ".") {
tmp += MakeFirstUpperCase(str)
}
fieldName = tmp
fieldName = strings.Replace(strings.Replace(fieldName, "-", "", -1), "_", "", -1)
if unique {
fieldNameCount[fieldName]++
if count := fieldNameCount[fieldName]; count != 1 {
fieldName = fmt.Sprintf("%s%d", fieldName, count)
}
}
return
}
func genGoFieldType(name string) string {
if _, ok := goBuildinType[name]; ok {
return name
}
var fieldType string
for _, str := range strings.Split(name, ".") {
fieldType += MakeFirstUpperCase(str)
}
fieldType = strings.Replace(MakeFirstUpperCase(strings.Replace(fieldType, "-", "", -1)), "_", "", -1)
if fieldType != "" {
return "*" + fieldType
}
return "interface{}"
}
// GoSimpleType generates code for simple type XML schema in Go language
// syntax.
func (gen *CodeGenerator) GoSimpleType(v *SimpleType) {
if v.List {
if _, ok := gen.StructAST[v.Name]; !ok {
fieldType := genGoFieldType(getBasefromSimpleType(trimNSPrefix(v.Base), gen.ProtoTree))
if fieldType == "time.Time" {
gen.ImportTime = true
}
content := fmt.Sprintf(" []%s\n", genGoFieldType(fieldType))
gen.StructAST[v.Name] = content
fieldName := genGoFieldName(v.Name, true)
gen.Field += fmt.Sprintf("%stype %s%s", genFieldComment(fieldName, v.Doc, "//"), fieldName, gen.StructAST[v.Name])
return
}
}
if v.Union && len(v.MemberTypes) > 0 {
if _, ok := gen.StructAST[v.Name]; !ok {
content := " struct {\n"
fieldName := genGoFieldName(v.Name, true)
if fieldName != v.Name {
gen.ImportEncodingXML = true
content += fmt.Sprintf("\tXMLName\txml.Name\t`xml:\"%s\"`\n", v.Name)
}
for _, member := range toSortedPairs(v.MemberTypes) {
memberName := member.key
memberType := member.value
if memberType == "" { // fix order issue
memberType = getBasefromSimpleType(memberName, gen.ProtoTree)
}
content += fmt.Sprintf("\t%s\t%s\n", genGoFieldName(memberName, false), genGoFieldType(memberType))
}
content += "}\n"
gen.StructAST[v.Name] = content
gen.Field += fmt.Sprintf("%stype %s%s", genFieldComment(fieldName, v.Doc, "//"), fieldName, gen.StructAST[v.Name])
}
return
}
if _, ok := gen.StructAST[v.Name]; !ok {
content := fmt.Sprintf(" %s\n", genGoFieldType(getBasefromSimpleType(trimNSPrefix(v.Base), gen.ProtoTree)))
gen.StructAST[v.Name] = content
fieldName := genGoFieldName(v.Name, true)
gen.Field += fmt.Sprintf("%stype %s%s", genFieldComment(fieldName, v.Doc, "//"), fieldName, gen.StructAST[v.Name])
}
}
// GoComplexType generates code for complex type XML schema in Go language
// syntax.
func (gen *CodeGenerator) GoComplexType(v *ComplexType) {
if _, ok := gen.StructAST[v.Name]; !ok {
content := " struct {\n"
fieldName := genGoFieldName(v.Name, true)
if fieldName != v.Name {
gen.ImportEncodingXML = true
content += fmt.Sprintf("\tXMLName\txml.Name\t`xml:\"%s\"`\n", v.Name)
}
for _, attrGroup := range v.AttributeGroup {
fieldType := getBasefromSimpleType(trimNSPrefix(attrGroup.Ref), gen.ProtoTree)
if fieldType == "time.Time" {
gen.ImportTime = true
}
content += fmt.Sprintf("\t%s\t%s\n", genGoFieldName(attrGroup.Name, false), genGoFieldType(fieldType))
}
for _, attribute := range v.Attributes {
var optional string
if attribute.Optional {
optional = `,omitempty`
}
fieldType := genGoFieldType(getBasefromSimpleType(trimNSPrefix(attribute.Type), gen.ProtoTree))
if fieldType == "time.Time" {
gen.ImportTime = true
}
content += fmt.Sprintf("\t%sAttr\t%s\t`xml:\"%s,attr%s\"`\n", genGoFieldName(attribute.Name, false), fieldType, attribute.Name, optional)
}
for _, group := range v.Groups {
var plural string
if group.Plural {
plural = "[]"
}
content += fmt.Sprintf("\t%s\t%s%s\n", genGoFieldName(group.Name, false), plural, genGoFieldType(getBasefromSimpleType(trimNSPrefix(group.Ref), gen.ProtoTree)))
}
for _, element := range v.Elements {
var plural string
if element.Plural {
plural = "[]"
}
fieldType := genGoFieldType(getBasefromSimpleType(trimNSPrefix(element.Type), gen.ProtoTree))
if fieldType == "time.Time" {
gen.ImportTime = true
}
content += fmt.Sprintf("\t%s\t%s%s\t`xml:\"%s\"`\n", genGoFieldName(element.Name, false), plural, fieldType, element.Name)
}
if len(v.Base) > 0 {
// If the type is a built-in type, generate a Value field as chardata.
// If it's not built-in one, embed the base type in the struct for the child type
// to effectively inherit all of the base type's fields
if isGoBuiltInType(v.Base) {
content += fmt.Sprintf("\tValue\t%s\t`xml:\",chardata\"`\n", genGoFieldType(v.Base))
} else {
content += fmt.Sprintf("\t%s\n", genGoFieldType(v.Base))
}
}
content += "}\n"
gen.StructAST[v.Name] = content
gen.Field += fmt.Sprintf("%stype %s%s", genFieldComment(fieldName, v.Doc, "//"), fieldName, gen.StructAST[v.Name])
}
}
func isGoBuiltInType(typeName string) bool {
_, builtIn := goBuildinType[typeName]
return builtIn
}
// GoGroup generates code for group XML schema in Go language syntax.
func (gen *CodeGenerator) GoGroup(v *Group) {
if _, ok := gen.StructAST[v.Name]; !ok {
content := " struct {\n"
fieldName := genGoFieldName(v.Name, true)
if fieldName != v.Name {
gen.ImportEncodingXML = true
content += fmt.Sprintf("\tXMLName\txml.Name\t`xml:\"%s\"`\n", v.Name)
}
for _, element := range v.Elements {
var plural string
if element.Plural {
plural = "[]"
}
content += fmt.Sprintf("\t%s\t%s%s\n", genGoFieldName(element.Name, false), plural, genGoFieldType(getBasefromSimpleType(trimNSPrefix(element.Type), gen.ProtoTree)))
}
for _, group := range v.Groups {
var plural string
if group.Plural {
plural = "[]"
}
content += fmt.Sprintf("\t%s\t%s%s\n", genGoFieldName(group.Name, false), plural, genGoFieldType(getBasefromSimpleType(trimNSPrefix(group.Ref), gen.ProtoTree)))
}
content += "}\n"
gen.StructAST[v.Name] = content
gen.Field += fmt.Sprintf("%stype %s%s", genFieldComment(fieldName, v.Doc, "//"), fieldName, gen.StructAST[v.Name])
}
}
// GoAttributeGroup generates code for attribute group XML schema in Go language
// syntax.
func (gen *CodeGenerator) GoAttributeGroup(v *AttributeGroup) {
if _, ok := gen.StructAST[v.Name]; !ok {
content := " struct {\n"
fieldName := genGoFieldName(v.Name, true)
if fieldName != v.Name {
gen.ImportEncodingXML = true
content += fmt.Sprintf("\tXMLName\txml.Name\t`xml:\"%s\"`\n", v.Name)
}
for _, attribute := range v.Attributes {
var optional string
if attribute.Optional {
optional = `,omitempty`
}
content += fmt.Sprintf("\t%sAttr\t%s\t`xml:\"%s,attr%s\"`\n", genGoFieldName(attribute.Name, false), genGoFieldType(getBasefromSimpleType(trimNSPrefix(attribute.Type), gen.ProtoTree)), attribute.Name, optional)
}
content += "}\n"
gen.StructAST[v.Name] = content
gen.Field += fmt.Sprintf("%stype %s%s", genFieldComment(fieldName, v.Doc, "//"), fieldName, gen.StructAST[v.Name])
}
}
// GoElement generates code for element XML schema in Go language syntax.
func (gen *CodeGenerator) GoElement(v *Element) {
if _, ok := gen.StructAST[v.Name]; !ok {
var plural string
if v.Plural {
plural = "[]"
}
content := fmt.Sprintf("\t%s%s\n", plural, genGoFieldType(getBasefromSimpleType(trimNSPrefix(v.Type), gen.ProtoTree)))
gen.StructAST[v.Name] = content
fieldName := genGoFieldName(v.Name, false)
gen.Field += fmt.Sprintf("%stype %s%s", genFieldComment(fieldName, v.Doc, "//"), fieldName, gen.StructAST[v.Name])
}
}
// GoAttribute generates code for attribute XML schema in Go language syntax.
func (gen *CodeGenerator) GoAttribute(v *Attribute) {
if _, ok := gen.StructAST[v.Name]; !ok {
var plural string
if v.Plural {
plural = "[]"
}
content := fmt.Sprintf("\t%s%s\n", plural, genGoFieldType(getBasefromSimpleType(trimNSPrefix(v.Type), gen.ProtoTree)))
gen.StructAST[v.Name] = content
fieldName := genGoFieldName(v.Name, true)
gen.Field += fmt.Sprintf("%stype %s%s", genFieldComment(fieldName, v.Doc, "//"), fieldName, gen.StructAST[v.Name])
}
}
func (gen *CodeGenerator) FileWithExtension(extension string) string {
if !strings.HasPrefix(extension, ".") {
extension = "." + extension
}
if strings.HasSuffix(gen.File, extension) {
return gen.File
}
return gen.File + extension
}