-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
genTypeScript.go
248 lines (227 loc) · 9.31 KB
/
genTypeScript.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
// 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"
"os"
"reflect"
"strings"
)
var typeScriptBuildInType = map[string]bool{
"boolean": true,
"number": true,
"string": true,
"void": true,
"null": true,
"undefined": true,
"Uint8Array": true,
}
// GenTypeScript generate TypeScript programming language source code for XML
// schema definition files.
func (gen *CodeGenerator) GenTypeScript() error {
fieldNameCount = make(map[string]int)
for _, ele := range gen.ProtoTree {
if ele == nil {
continue
}
funcName := fmt.Sprintf("TypeScript%s", reflect.TypeOf(ele).String()[6:])
callFuncByName(gen, funcName, []reflect.Value{reflect.ValueOf(ele)})
}
f, err := os.Create(gen.FileWithExtension(".ts"))
if err != nil {
return err
}
defer f.Close()
source := []byte(fmt.Sprintf("%s\n%s", copyright, gen.Field))
f.Write(source)
return err
}
func genTypeScriptFieldName(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(fieldName, "-", "", -1)
if unique {
fieldNameCount[fieldName]
if count := fieldNameCount[fieldName]; count != 1 {
fieldName = fmt.Sprintf("%s%d", fieldName, count)
}
}
return
}
func genTypeScriptFieldType(name string, plural bool) (fieldType string) {
if _, ok := typeScriptBuildInType[name]; ok {
fieldType = name
return
}
for _, str := range strings.Split(name, ".") {
fieldType = MakeFirstUpperCase(str)
}
fieldType = MakeFirstUpperCase(strings.Replace(fieldType, "-", "", -1))
if fieldType == "" || fieldType == "Any" {
fieldType = "any"
}
if plural {
fieldType = fmt.Sprintf("Array<%s>", fieldType)
}
return
}
// TypeScriptSimpleType generates code for simple type XML schema in TypeScript language
// syntax.
func (gen *CodeGenerator) TypeScriptSimpleType(v *SimpleType) {
if v.List {
if _, ok := gen.StructAST[v.Name]; !ok {
fieldType := genTypeScriptFieldType(getBasefromSimpleType(trimNSPrefix(v.Base), gen.ProtoTree), true)
content := fmt.Sprintf(" = %s;\n", fieldType)
gen.StructAST[v.Name] = content
fieldName := genTypeScriptFieldName(v.Name, true)
gen.Field = fmt.Sprintf("%sexport type %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 := " {\n"
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: %s;\n", genTypeScriptFieldName(memberName, false), genTypeScriptFieldType(memberType, false))
}
content = "}\n"
gen.StructAST[v.Name] = content
fieldName := genTypeScriptFieldName(v.Name, true)
gen.Field = fmt.Sprintf("%sexport class %s%s", genFieldComment(fieldName, v.Doc, "//"), fieldName, gen.StructAST[v.Name])
}
return
}
if len(v.Restriction.Enum) > 0 {
var content string
baseType := genTypeScriptFieldType(getBasefromSimpleType(trimNSPrefix(v.Base), gen.ProtoTree), false)
for _, enum := range v.Restriction.Enum {
switch baseType {
case "string":
content = fmt.Sprintf("\t%s = '%s',\n", enum, enum)
case "number":
content = fmt.Sprintf("\tEnum%s = %s,\n", enum, enum)
default:
content = fmt.Sprintf("\tEnum%s = '%s',\n", enum, enum)
}
}
fieldName := genTypeScriptFieldName(v.Name, true)
gen.Field = fmt.Sprintf("%sexport enum %s {\n%s}\n", genFieldComment(fieldName, v.Doc, "//"), fieldName, content)
return
}
if _, ok := gen.StructAST[v.Name]; !ok {
content := fmt.Sprintf(" %s;\n", genTypeScriptFieldType(getBasefromSimpleType(trimNSPrefix(v.Base), gen.ProtoTree), false))
gen.StructAST[v.Name] = content
fieldName := genTypeScriptFieldName(v.Name, true)
gen.Field = fmt.Sprintf("%sexport type %s =%s", genFieldComment(fieldName, v.Doc, "//"), fieldName, gen.StructAST[v.Name])
}
}
// TypeScriptComplexType generates code for complex type XML schema in TypeScript language
// syntax.
func (gen *CodeGenerator) TypeScriptComplexType(v *ComplexType) {
if _, ok := gen.StructAST[v.Name]; !ok {
content := " {\n"
for _, attrGroup := range v.AttributeGroup {
fieldType := getBasefromSimpleType(trimNSPrefix(attrGroup.Ref), gen.ProtoTree)
content = fmt.Sprintf("\t%s: %s;\n", genTypeScriptFieldName(attrGroup.Name, false), genTypeScriptFieldType(fieldType, false))
}
for _, attribute := range v.Attributes {
var optional string
if attribute.Optional {
optional = ` | null`
}
fieldType := genTypeScriptFieldType(getBasefromSimpleType(trimNSPrefix(attribute.Type), gen.ProtoTree), attribute.Plural)
content = fmt.Sprintf("\t%sAttr: %s%s;\n", genTypeScriptFieldName(attribute.Name, false), fieldType, optional)
}
for _, group := range v.Groups {
content = fmt.Sprintf("\t%s: %s;\n", genTypeScriptFieldName(group.Name, false), genTypeScriptFieldType(getBasefromSimpleType(trimNSPrefix(group.Ref), gen.ProtoTree), group.Plural))
}
for _, element := range v.Elements {
fieldType := genTypeScriptFieldType(getBasefromSimpleType(trimNSPrefix(element.Type), gen.ProtoTree), element.Plural)
content = fmt.Sprintf("\t%s: %s;\n", genTypeScriptFieldName(element.Name, false), fieldType)
}
if len(v.Base) > 0 && isBuiltInTypeScriptType(v.Base) {
fieldType := genTypeScriptFieldType(getBasefromSimpleType(trimNSPrefix(v.Base), gen.ProtoTree), false)
content = fmt.Sprintf("\tValue: %s;\n", fieldType)
}
content = "}\n"
gen.StructAST[v.Name] = content
fieldName := genTypeScriptFieldName(v.Name, true)
typeExtension := ""
if len(v.Base) > 0 && !isBuiltInTypeScriptType(v.Base) {
fieldType := genTypeScriptFieldType(getBasefromSimpleType(trimNSPrefix(v.Base), gen.ProtoTree), false)
content = fmt.Sprintf("\tValue: %s;\n", fieldType)
typeExtension = fmt.Sprintf(" extends %s ", fieldType)
}
gen.Field = fmt.Sprintf("%sexport class %s%s%s", genFieldComment(fieldName, v.Doc, "//"), fieldName, typeExtension, gen.StructAST[v.Name])
}
}
func isBuiltInTypeScriptType(typeName string) bool {
_, builtIn := typeScriptBuildInType[typeName]
return builtIn
}
// TypeScriptGroup generates code for group XML schema in TypeScript language syntax.
func (gen *CodeGenerator) TypeScriptGroup(v *Group) {
if _, ok := gen.StructAST[v.Name]; !ok {
content := " {\n"
for _, element := range v.Elements {
content = fmt.Sprintf("\t%s: %s;\n", genTypeScriptFieldName(element.Name, false), genTypeScriptFieldType(getBasefromSimpleType(trimNSPrefix(element.Type), gen.ProtoTree), element.Plural))
}
for _, group := range v.Groups {
content = fmt.Sprintf("\t%s: %s;\n", genTypeScriptFieldName(group.Name, false), genTypeScriptFieldType(getBasefromSimpleType(trimNSPrefix(group.Ref), gen.ProtoTree), group.Plural))
}
content = "}\n"
gen.StructAST[v.Name] = content
fieldName := genTypeScriptFieldName(v.Name, true)
gen.Field = fmt.Sprintf("%sexport class %s%s", genFieldComment(fieldName, v.Doc, "//"), fieldName, gen.StructAST[v.Name])
}
}
// TypeScriptAttributeGroup generates code for attribute group XML schema in TypeScript language
// syntax.
func (gen *CodeGenerator) TypeScriptAttributeGroup(v *AttributeGroup) {
if _, ok := gen.StructAST[v.Name]; !ok {
content := " {\n"
for _, attribute := range v.Attributes {
var optional string
if attribute.Optional {
optional = ` | null`
}
content = fmt.Sprintf("\t%sAttr: %s%s;\n", genTypeScriptFieldName(attribute.Name, false), genTypeScriptFieldType(getBasefromSimpleType(trimNSPrefix(attribute.Type), gen.ProtoTree), attribute.Plural), optional)
}
content = "}\n"
gen.StructAST[v.Name] = content
fieldName := genTypeScriptFieldName(v.Name, true)
gen.Field = fmt.Sprintf("%sexport class %s%s", genFieldComment(fieldName, v.Doc, "//"), fieldName, gen.StructAST[v.Name])
}
}
// TypeScriptElement generates code for element XML schema in TypeScript language syntax.
func (gen *CodeGenerator) TypeScriptElement(v *Element) {
if _, ok := gen.StructAST[v.Name]; !ok {
gen.StructAST[v.Name] = fmt.Sprintf(" %s;\n", genTypeScriptFieldType(getBasefromSimpleType(trimNSPrefix(v.Type), gen.ProtoTree), v.Plural))
fieldName := genTypeScriptFieldName(v.Name, true)
gen.Field = fmt.Sprintf("%sexport type %s =%s", genFieldComment(fieldName, v.Doc, "//"), fieldName, gen.StructAST[v.Name])
}
}
// TypeScriptAttribute generates code for attribute XML schema in TypeScript language syntax.
func (gen *CodeGenerator) TypeScriptAttribute(v *Attribute) {
if _, ok := gen.StructAST[v.Name]; !ok {
gen.StructAST[v.Name] = fmt.Sprintf(" %s;\n", genTypeScriptFieldType(getBasefromSimpleType(trimNSPrefix(v.Type), gen.ProtoTree), v.Plural))
fieldName := genTypeScriptFieldName(v.Name, true)
gen.Field = fmt.Sprintf("%sexport type %s =%s", genFieldComment(fieldName, v.Doc, "//"), fieldName, gen.StructAST[v.Name])
}
}