-
Notifications
You must be signed in to change notification settings - Fork 6
/
jsonObject.go
323 lines (299 loc) · 7.05 KB
/
jsonObject.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
package gojson
import (
"bytes"
"reflect"
"strings"
"sort"
)
type JsonObject struct {
Attributes map[string]*JsonObject
Value interface{}
VType reflect.Kind
}
// 获取属性对象
func (j *JsonObject) GetJsonObject(key string) *JsonObject {
if j == nil {
return nil
}
return j.Attributes[key]
}
// 获取string值
func (j *JsonObject) GetString() string {
if j == nil && j.VType != reflect.String {
return ""
}
return j.Value.(string)
}
// 获取数组
func (j *JsonObject) GetJsonArray() []*JsonObject {
if j == nil && j.VType != reflect.Slice {
return nil
}
return j.Value.([]*JsonObject)
}
// 获取bool值
func (j *JsonObject) GetBool() bool {
if j == nil && j.VType != reflect.Bool {
return false
}
return j.Value.(bool)
}
// 获取浮点值
func (j *JsonObject) GetFloat64() float64 {
if j == nil {
return 0
}
switch j.VType {
case reflect.Int64:
return float64(j.Value.(int64))
case reflect.Float64:
return j.Value.(float64)
}
return 0
}
// 获取整型值
func (j *JsonObject) GetInt64() int64 {
if j == nil {
return 0
}
switch j.VType {
case reflect.Int64:
return j.Value.(int64)
case reflect.Float64:
return int64(j.Value.(float64))
}
return 0
}
// 获取整型值
func (j *JsonObject) GetInterface() interface{} {
if j == nil && j.VType != reflect.Struct {
return nil
}
return j.Value
}
// 获取整型值
func (j *JsonObject) GetDefaultCoding() string {
if j == nil {
return ""
}
buffer := bytes.Buffer{}
buildCoding("AutoGenerated", j, &buffer)
return buffer.String()
}
// 获取整型值
func (j *JsonObject) GetCoding(strcutName string) string {
if j == nil {
return ""
}
buffer := bytes.Buffer{}
buildCoding(strcutName, j, &buffer)
return buffer.String()
}
func buildCoding(key string, root *JsonObject, buffer *bytes.Buffer) {
structsExist := map[string]bool{}
switch root.VType {
case reflect.Struct:
if len(root.Attributes) > 0 {
for subKey, subObject := range root.Attributes {
subKey = key + "_" + subKey
buildCoding(subKey, subObject, buffer)
}
}
if !structsExist[key] {
getObjectCoding(key, root, buffer)
structsExist[key]=true
}
case reflect.Slice:
object := root.Value.([]*JsonObject)[0]
buildCoding(key, object, buffer)
default:
//fmt.Println(root.VType,root.Value)
}
}
type attribute struct {
name string `json:"name"`
jsonName string
attrType string
origType reflect.Kind
}
type attributeSlice []*attribute
func (c attributeSlice) Len() int {
return len(c)
}
func (c attributeSlice) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
func (c attributeSlice) Less(i, j int) bool {
return c[i].name < c[j].name
}
func getObjectCoding(structName string, object *JsonObject, buffer *bytes.Buffer) {
buffer.WriteString("\ntype "+camelString(structName)+" struct{\n")
attrs := attributeSlice{}
nameMaxLength := 0
attrTypeMaxLength := 0
for key,attrObject:=range object.Attributes{
attrName := camelString(key)
attr := &attribute{
name:attrName,
jsonName:key,
origType:attrObject.VType,
attrType: func()string {
switch attrObject.VType {
case reflect.Struct:
if len(attrObject.Attributes) >0 {
return "*"+camelString(getSubAtrrName(structName,key))
}else {
return reflect.Interface.String()+"{}"
}
case reflect.Slice:
subObject := attrObject.Value.([]*JsonObject)[0]
switch subObject.VType {
case reflect.Struct:
if len(subObject.Attributes) >0 {
return "[]*"+camelString(getSubAtrrName(structName,key))
}else {
return "[]"+reflect.Interface.String()+"{}"
}
case reflect.Slice:
subObject := attrObject.Value.([]*JsonObject)[0]
switch subObject.VType {
case reflect.Struct:
if len(subObject.Attributes) >0 {
return "[][]*"+camelString(getSubAtrrName(structName,key))
}else {
return "[][]"+reflect.Interface.String()+"{}"
}
case reflect.Slice:
return "[][][]*"
default:
return "[][]"+subObject.VType.String()
}
default:
return "[]"+subObject.VType.String()
}
case reflect.Interface:
return reflect.Interface.String()+"{}"
default:
return attrObject.VType.String()
}
}(),
}
if len(attr.name) >nameMaxLength{
nameMaxLength = len(attr.name)
}
if len(attr.attrType) > attrTypeMaxLength{
attrTypeMaxLength = len(attr.attrType)
}
attrs = append(attrs,attr)
}
sort.Sort(attrs)
for _,attr :=range attrs{
buffer.WriteString("\t"+attr.name+getblanks(attr.name,nameMaxLength)+" "+
attr.attrType+getblanks(attr.attrType,attrTypeMaxLength)+" "+
"`json:\""+attr.jsonName+"\"`"+" \n")
}
buffer.WriteString("}\n")
/*
func (this *NihaoComponents) GetCityName() string {
if this == nil {
return ""
}
return this.CityName
}
*/
for _,attr :=range attrs{
// get方法
buffer.WriteString("func (this *"+camelString(structName)+") Get"+attr.name+"() "+attr.attrType+" {\n")
buffer.WriteString("\tif this == nil {\n")
switch attr.origType {
case reflect.Int32,reflect.Int64,reflect.Float64:
buffer.WriteString("\t\treturn 0\n")
case reflect.Bool:
buffer.WriteString("\t\treturn false\n")
case reflect.String:
buffer.WriteString("\t\treturn \"\"\n")
default:
buffer.WriteString("\t\treturn nil\n")
}
buffer.WriteString("\t}\n")
buffer.WriteString("\treturn this."+attr.name+"\n")
buffer.WriteString("}\n")
// set方法
paramName:=getParamName(attr.name)
buffer.WriteString("func (this *"+camelString(structName)+") Set"+attr.name+"("+paramName+" "+attr.attrType+") {\n")
buffer.WriteString("\tif this == nil {\n")
buffer.WriteString("\t\treturn\n")
buffer.WriteString("\t}\n")
buffer.WriteString("\tthis."+attr.name+" = "+paramName+"\n")
buffer.WriteString("}\n")
}
/*
func (this *NihaoComponents) SetCityName(cityName string) {
if this == nil {
return
}
this.CityName = cityName
}
*/
}
func getParamName(name string) string {
if len(name)>1{
return strings.ToLower(string(name[0]))+name[1:]
}
return name
}
func getSubAtrrName(father ,sub string) string {
return father+"_"+sub
}
func getblanks(key string,length int) string {
length = length-len(key)
if length<= 0{
return ""
}
buffer := bytes.Buffer{}
for i:=0 ;i<length;i++{
buffer.WriteString(" ")
}
return buffer.String()
}
// snake string, XxYy to xx_yy , XxYY to xx_yy
func snakeString(s string) string {
data := make([]byte, 0, len(s)*2)
j := false
num := len(s)
for i := 0; i < num; i++ {
d := s[i]
if i > 0 && d >= 'A' && d <= 'Z' && j {
data = append(data, '_')
}
if d != '_' {
j = true
}
data = append(data, d)
}
return strings.ToLower(string(data[:]))
}
// camel string, xx_yy to XxYy
func camelString(s string) string {
data := make([]byte, 0, len(s))
j := false
k := false
num := len(s) - 1
for i := 0; i <= num; i++ {
d := s[i]
if k == false && d >= 'A' && d <= 'Z' {
k = true
}
if d >= 'a' && d <= 'z' && (j || k == false) {
d = d - 32
j = false
k = true
}
if k && d == '_' && num > i && s[i+1] >= 'a' && s[i+1] <= 'z' {
j = true
continue
}
data = append(data, d)
}
return string(data[:])
}