-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
utils.go
289 lines (260 loc) · 8.78 KB
/
utils.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
// Copyright 2020 - 2022 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"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"reflect"
"regexp"
"sort"
"strings"
"unicode"
"unicode/utf8"
)
var (
copyright = "// Code generated by xgen. DO NOT EDIT."
matchFirstCap = regexp.MustCompile("([A-Z])([A-Z][a-z])")
matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
fieldNameCount map[string]int
)
// ToSnakeCase converts the provided string to snake_case.
func ToSnakeCase(input string) string {
output := matchFirstCap.ReplaceAllString(input, "${1}_${2}")
output = matchAllCap.ReplaceAllString(output, "${1}_${2}")
output = strings.ReplaceAll(output, "-", "_")
return strings.ToLower(output)
}
// GetFileList get a list of file by given path.
func GetFileList(path string) (files []string, err error) {
var fi os.FileInfo
fi, err = os.Stat(path)
if err != nil {
return
}
if fi.IsDir() {
err = filepath.Walk(path, func(fp string, info os.FileInfo, err error) error {
files = append(files, fp)
return nil
})
if err != nil {
return
}
}
files = append(files, path)
return
}
// PrepareOutputDir provide a method to create the output directory by given
// path.
func PrepareOutputDir(path string) error {
if path == "" {
return nil
}
_, err := os.Stat(path)
if os.IsNotExist(err) {
if err := os.MkdirAll(path, 0755); err != nil {
return err
}
}
return nil
}
// BuildInTypes defines the correspondence between Go, TypeScript, C, Java,
// Rust languages and data types in XSD.
// https://www.w3.org/TR/xmlschema-2/#datatype
var BuildInTypes = map[string][]string{
"anyType": {"string", "string", "char", "String", "String"},
"ENTITIES": {"[]string", "Array<string>", "char[]", "List<String>", "Vec<String>"},
"ENTITY": {"string", "string", "char", "String", "String"},
"ID": {"string", "string", "char", "String", "String"},
"IDREF": {"string", "string", "char", "String", "String"},
"IDREFS": {"[]string", "Array<string>", "char[]", "List<String>", "Vec<String>"},
"NCName": {"string", "string", "char", "String", "String"},
"NMTOKEN": {"string", "string", "char", "String", "String"},
"NMTOKENS": {"[]string", "Array<string>", "char[]", "List<String>", "Vec<String>"},
"NOTATION": {"[]string", "Array<string>", "char[]", "List<String>", "Vec<String>"},
"Name": {"string", "string", "char", "String", "String"},
"QName": {"xml.Name", "any", "char", "String", "String"},
"anyURI": {"string", "string", "char", "QName", "String"},
"base64Binary": {"string", "Uint8Array", "char[]", "List<Byte>", "String"},
"boolean": {"bool", "boolean", "bool", "Boolean", "bool"},
"byte": {"int8", "any", "char[]", "Byte", "u8"},
"date": {"string", "string", "char", "String", "u8"},
"dateTime": {"string", "string", "char", "String", "u8"},
"decimal": {"float64", "number", "float", "Float", "f64"},
"double": {"float64", "number", "float", "Float", "f64"},
"duration": {"string", "string", "char", "String", "String"},
"float": {"float32", "number", "float", "Float", "f64"},
"gDay": {"string", "string", "char", "String", "String"},
"gMonth": {"string", "string", "char", "String", "String"},
"gMonthDay": {"string", "string", "char", "String", "String"},
"gYear": {"string", "string", "char", "String", "String"},
"gYearMonth": {"string", "string", "char", "String", "String"},
"hexBinary": {"string", "Uint8Array", "char[]", "List<Byte>", "String"},
"int": {"int", "number", "int", "Integer", "i32"},
"integer": {"int", "number", "int", "Integer", "i32"},
"language": {"string", "string", "char", "String", "String"},
"long": {"int64", "number", "int", "Long", "i64"},
"negativeInteger": {"int", "number", "int", "Integer", "i32"},
"nonNegativeInteger": {"int", "number", "int", "Integer", "u32"},
"normalizedString": {"string", "string", "char", "String", "String"},
"nonPositiveInteger": {"int", "number", "int", "Integer", "i32"},
"positiveInteger": {"int", "number", "int", "Integer", "u32"},
"short": {"int16", "number", "int", "Integer", "i16"},
"string": {"string", "string", "char", "String", "String"},
"time": {"time.Time", "string", "char", "String", "String"},
"token": {"string", "string", "char", "String", "String"},
"unsignedByte": {"uint8", "any", "char", "Byte", "u8"},
"unsignedInt": {"uint32", "number", "unsigned int", "Integer", "u32"},
"unsignedLong": {"uint64", "number", "unsigned int", "Long", "u64"},
"unsignedShort": {"uint16", "number", "unsigned int", "Short", "u16"},
"xml:lang": {"string", "string", "char", "String", "String"},
"xml:space": {"string", "string", "char", "String", "String"},
"xml:base": {"string", "string", "char", "String", "String"},
"xml:id": {"string", "string", "char", "String", "String"},
}
func getBuildInTypeByLang(value, lang string) (buildType string, ok bool) {
var supportLang = map[string]int{
"Go": 0,
"TypeScript": 1,
"C": 2,
"Java": 3,
"Rust": 4,
}
var buildInTypes []string
if buildInTypes, ok = BuildInTypes[value]; !ok {
return
}
buildType = buildInTypes[supportLang[lang]]
return
}
func getBasefromSimpleType(name string, XSDSchema []interface{}) string {
for _, ele := range XSDSchema {
switch v := ele.(type) {
case *SimpleType:
if !v.List && !v.Union && v.Name == name {
return v.Base
}
case *Attribute:
if v.Name == name {
return v.Type
}
case *Element:
if v.Name == name {
return v.Type
}
}
}
return name
}
func getNSPrefix(str string) (ns string) {
split := strings.Split(str, ":")
if len(split) == 2 {
ns = split[0]
return
}
return
}
func trimNSPrefix(str string) (name string) {
split := strings.Split(str, ":")
if len(split) == 2 {
name = split[1]
return
}
name = str
return
}
// MakeFirstUpperCase make the first letter of a string uppercase.
func MakeFirstUpperCase(s string) string {
return ToTitle(s)
}
func ToTitle(val string) string {
var buf strings.Builder
buf.Grow(utf8.UTFMax * len(val))
for i, rune := range val {
if i == 0 {
rune = unicode.ToUpper(rune)
}
buf.WriteRune(rune)
}
return buf.String()
}
// callFuncByName calls the no error or only error return function with
// reflect by given receiver, name and parameters.
func callFuncByName(receiver interface{}, name string, params []reflect.Value) (err error) {
function := reflect.ValueOf(receiver).MethodByName(name)
if function.IsValid() {
rt := function.Call(params)
if len(rt) == 0 {
return
}
if !rt[0].IsNil() {
err = rt[0].Interface().(error)
return
}
}
return
}
// isValidUrl tests a string to determine if it is a well-structured url or
// not.
func isValidURL(toTest string) bool {
_, err := url.ParseRequestURI(toTest)
if err != nil {
return false
}
u, err := url.Parse(toTest)
if err != nil || u.Scheme == "" || u.Host == "" {
return false
}
return true
}
func fetchSchema(URL string) ([]byte, error) {
var body []byte
var client http.Client
var err error
resp, err := client.Get(URL)
if err != nil {
return body, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return body, err
}
}
return body, err
}
func genFieldComment(name, doc, prefix string) string {
docReplacer := strings.NewReplacer("\n", fmt.Sprintf("\r\n%s ", prefix), "\t", "")
if doc == "" {
return fmt.Sprintf("\r\n%s %s ...\r\n", prefix, name)
}
return fmt.Sprintf("\r\n%s %s is %s\r\n", prefix, name, docReplacer.Replace(doc))
}
type kvPair struct {
key string
value string
}
// kvPairList adapted from Andrew Gerrand for a similar problem (sorted map): https://groups.google.com/forum/#!topic/golang-nuts/FT7cjmcL7gw
type kvPairList []kvPair
func (k kvPairList) Len() int { return len(k) }
func (k kvPairList) Less(i, j int) bool {
return k[i].value < k[j].value || (k[i].value == k[j].value && strings.Compare(k[i].key, k[j].key) > 0)
}
func (k kvPairList) Swap(i, j int) { k[i], k[j] = k[j], k[i] }
func toSortedPairs(toSort map[string]string) kvPairList {
pl := make(kvPairList, 0, len(toSort))
for k, v := range toSort {
pl = append(pl, kvPair{k, v})
}
sort.Sort(pl)
return pl
}