-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathutils.go
189 lines (175 loc) · 6.03 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
// Copyright 2020 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 (
"bytes"
"os"
"path/filepath"
"reflect"
"strings"
)
// 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
// languages and data types in XSD.
// https://www.w3.org/TR/xmlschema-2/#datatype
var BuildInTypes = map[string][]string{
"anyType": {"string", "string", "char", "String"},
"ENTITIES": {"[]string", "Array<string>", "char[]", "List<String>"},
"ENTITY": {"string", "string", "char", "String"},
"ID": {"string", "string", "char", "String"},
"IDREF": {"string", "string", "char", "String"},
"IDREFS": {"[]string", "Array<string>", "char[]", "List<String>"},
"NCName": {"string", "string", "char", "String"},
"NMTOKEN": {"string", "string", "char", "String"},
"NMTOKENS": {"[]string", "Array<string>", "char[]", "List<String>"},
"NOTATION": {"[]string", "Array<string>", "char[]", "List<String>"},
"Name": {"string", "string", "char", "String"},
"QName": {"xml.Name", "any", "char", "String"},
"anyURI": {"string", "string", "char", "QName"},
"base64Binary": {"[]byte", "Array<any>", "char[]", "List<Byte>"},
"boolean": {"bool", "boolean", "bool", "Boolean"},
"byte": {"byte", "any", "char[]", "Byte"},
"date": {"time.Time", "string", "char", "Byte"},
"dateTime": {"time.Time", "string", "char", "Byte"},
"decimal": {"float64", "number", "float", "Float"},
"double": {"float64", "number", "float", "Float"},
"duration": {"string", "string", "char", "String"},
"float": {"float32", "number", "float", "Float"},
"gDay": {"time.Time", "string", "char", "String"},
"gMonth": {"time.Time", "string", "char", "String"},
"gMonthDay": {"time.Time", "string", "char", "String"},
"gYear": {"time.Time", "string", "char", "String"},
"gYearMonth": {"time.Time", "string", "char", "String"},
"hexBinary": {"[]byte", "Array<any>", "char[]", "List<Byte>"},
"int": {"int", "number", "int", "Integer"},
"integer": {"int", "number", "int", "Integer"},
"language": {"string", "string", "char", "String"},
"long": {"int64", "number", "int", "Long"},
"negativeInteger": {"int", "number", "int", "Integer"},
"nonNegativeInteger": {"int", "number", "int", "Integer"},
"normalizedString": {"string", "string", "char", "String"},
"nonPositiveInteger": {"int", "number", "int", "Integer"},
"positiveInteger": {"int", "number", "int", "Integer"},
"short": {"int", "number", "int", "Integer"},
"string": {"string", "string", "char", "String"},
"time": {"time.Time", "string", "char", "String"},
"token": {"string", "string", "char", "String"},
"unsignedByte": {"byte", "any", "char", "Byte"},
"unsignedInt": {"uint", "number", "unsigned int", "Integer"},
"unsignedLong": {"uint64", "number", "unsigned int", "Long"},
"unsignedShort": {"uint", "number", "unsigned int", "Short"},
"xml:lang": {"string", "string", "char", "String"},
"xml:space": {"string", "string", "char", "String"},
"xml:base": {"string", "string", "char", "String"},
"xml:id": {"string", "string", "char", "String"},
}
func getBuildInTypeByLang(value, lang string) (buildType string, ok bool) {
var supportLang = map[string]int{
"Go": 0,
"TypeScript": 1,
"C": 2,
"Java": 3,
}
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 {
if len(s) < 2 {
return strings.ToUpper(s)
}
bts := []byte(s)
lc := bytes.ToUpper([]byte{bts[0]})
rest := bts[1:]
return string(bytes.Join([][]byte{lc, rest}, nil))
}
// callFuncByName calls the 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 !rt[0].IsNil() {
err = rt[0].Interface().(error)
return
}
}
return
}