-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathjsonutils.go
459 lines (409 loc) · 9.69 KB
/
jsonutils.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package jsonutils
import (
"bytes"
"encoding/json"
"fmt"
"go/format"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"sort"
"strconv"
"strings"
"text/template"
"time"
"unicode"
"unicode/utf8"
)
type Model struct {
Writer io.Writer
WithExample bool
Data interface{}
Name string
Format bool
Convert bool
}
func New(data interface{}, name string) *Model {
if name == "" {
name = "Data"
}
name = replaceName(name)
return &Model{
Writer: os.Stdout,
WithExample: true,
Data: data,
Name: name,
Format: true,
Convert: true,
}
}
func FromBytes(bytes []byte, name string) (*Model, error) {
f, err := ParseJson(bytes)
if err != nil {
return nil, err
}
return New(f, name), nil
}
func GetModel(url string) (*Model, error) {
b, name, err := Get(url)
if err != nil {
return nil, err
}
return FromBytes(b, name)
}
func Get(url string) ([]byte, string, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, "", err
}
req.Header.Add("Accept", "application/json")
r, err := http.DefaultClient.Do(req)
if err != nil {
return nil, "", err
}
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, "", err
}
return b, getName(url), err
}
func ParseJson(b []byte) (interface{}, error) {
var f interface{}
err := json.Unmarshal(b, &f)
return f, err
}
func PrintGo(f interface{}, name string) {
WriteGo(os.Stdout, f, name)
}
func WriteGo(w io.Writer, f interface{}, name string) {
m := &Model{
Writer: w,
WithExample: true,
Data: f,
Name: name,
Format: true,
Convert: true,
}
m.WriteGo()
}
func (m *Model) WriteGo() {
if m.Format {
org := m.Writer
var buf bytes.Buffer
m.Writer = &buf
m.writeGo()
b, err := format.Source(buf.Bytes())
if err == nil {
org.Write(b)
} else {
io.Copy(org, &buf)
}
m.Writer = org
} else {
m.writeGo()
}
}
func (m *Model) writeGo() {
fu := func(ms map[string]interface{}) { m.parseMap(ms) }
m.print(fu, goTempl)
}
func (m *Model) WriteJava() {
fu := func(ms map[string]interface{}) {
v, n := m.parseMapJava(ms)
if v != nil {
m.parseArrayJava(v, n)
}
}
m.print(fu, gsonTempl)
}
func printTempl(w io.Writer, templData string, name string, isArray bool) {
tmpl, err := template.New("test").Parse(templData)
if err != nil {
panic(err)
}
data := struct {
Name string
IsArray bool
}{
name,
isArray,
}
err = tmpl.Execute(w, data)
if err != nil {
panic(err)
}
}
func (m *Model) print(fu func(map[string]interface{}), templData string) {
var ma map[string]interface{}
switch v := m.Data.(type) {
case []interface{}:
ma = v[0].(map[string]interface{})
printTempl(m.Writer, templData, m.Name, true)
//fmt.Fprintf(m.Writer, array, m.Name)
default:
ma = m.Data.(map[string]interface{})
printTempl(m.Writer, templData, m.Name, false)
//fmt.Fprintf(m.Writer, object, m.Name)
}
fu(ma)
fmt.Fprintln(m.Writer, "}")
}
func (m *Model) parseMap(ms map[string]interface{}) {
keys := getSortedKeys(ms)
for _, k := range keys {
m.parse(ms[k], k)
}
}
func (m *Model) parse(data interface{}, k string) {
switch vv := data.(type) {
case string:
if m.Convert {
t, converted := parseType(vv)
m.printType(k, vv, t, converted)
} else {
m.printType(k, vv, "string", false)
}
case bool:
m.printType(k, vv, "bool", false)
case float64:
//json parser always returns a float for number values, check if it is an int value
if float64(int64(vv)) == vv {
m.printType(k, vv, "int64", false)
} else {
m.printType(k, vv, "float64", false)
}
case int64:
m.printType(k, vv, "int64", false)
case []interface{}:
if len(vv) > 0 {
switch vvv := vv[0].(type) {
case string:
m.printType(k, vv[0], "[]string", false)
case float64:
//json parser always returns a float for number values, check if it is an int value
if float64(int64(vv[0].(float64))) == vv[0].(float64) {
m.printType(k, vv[0], "[]int64", false)
} else {
m.printType(k, vv[0], "[]float64", false)
}
case bool:
m.printType(k, vv[0], "[]bool", false)
case []interface{}:
m.parse(vvv[0], k)
//m.printObject(k, "[]struct", func() { m.parse(vvv[0], k) })
case map[string]interface{}:
m.printObject(k, "[]struct", func() { m.parseMap(vvv) })
default:
//fmt.Printf("unknown type: %T", vvv)
m.printType(k, nil, "interface{}", false)
}
} else {
// empty array
m.printType(k, nil, "[]interface{}", false)
}
case map[string]interface{}:
m.printObject(k, "struct", func() { m.parseMap(vv) })
default:
//fmt.Printf("unknown type: %T", vv)
m.printType(k, nil, "interface{}", false)
}
}
func (m *Model) parseMapJava(ms map[string]interface{}) ([]map[string]interface{}, []string) {
var data []map[string]interface{}
var names []string
for k, v := range ms {
name := replaceName(k)
switch vv := v.(type) {
case string:
m.printValuesJava("String", k)
case float64:
if float64(int(vv)) == vv {
m.printValuesJava("int", k)
} else {
m.printValuesJava("double", k)
}
case bool:
m.printValuesJava("boolean", k)
case []interface{}:
if len(vv) > 0 {
switch vvv := vv[0].(type) {
case string:
m.printValuesJava("String[]", k)
case float64:
m.printValuesJava("float[]", k)
case []interface{}:
m.printValuesJava(name+"[]", k)
data = append(data, vvv[0].(map[string]interface{}))
names = append(names, k)
case map[string]interface{}:
m.printValuesJava(name+"[]", k)
data = append(data, vvv)
names = append(names, k)
default:
//fmt.Printf("unknown type: %T", vvv)
m.printValuesJava("Object", k)
}
} else {
// empty array
m.printValuesJava("Object[]", k)
}
case map[string]interface{}:
m.printValuesJava(name, k)
data = append(data, vv)
names = append(names, k)
default:
m.printValuesJava("Object", k)
}
}
return data, names
}
func parseType(value string) (string, bool) {
if _, err := time.Parse(time.RFC3339, value); err == nil {
return "time.Time", false
} else if ip := net.ParseIP(value); ip != nil {
return "net.IP", false
} else if _, err := strconv.ParseInt(value, 10, 64); err == nil {
return "int64", true
} else if _, err := strconv.ParseFloat(value, 64); err == nil {
return "float64", true
} else if _, err := strconv.ParseBool(value); err == nil {
return "bool", true
} else {
return "string", false
}
}
func (m *Model) printType(key string, value interface{}, t string, converted bool) {
name := replaceName(key)
if converted {
key += ",string"
}
if m.WithExample {
fmt.Fprintf(m.Writer, "%s %s `json:\"%s\"` // %v\n", name, t, key, value)
} else {
fmt.Fprintf(m.Writer, "%s %s `json:\"%s\"`\n", name, t, key)
}
}
func (m *Model) printObject(n string, t string, f func()) {
name := replaceName(n)
fmt.Fprintf(m.Writer, "%s %s {\n", name, t)
f()
fmt.Fprintf(m.Writer, "} `json:\"%s\"`\n", n)
}
func (m *Model) parseArrayJava(ms []map[string]interface{}, s []string) {
for i, v := range ms {
fmt.Fprintln(m.Writer, "public class", replaceName(s[i]), "{")
v, n := m.parseMapJava(v)
fmt.Fprintln(m.Writer, "}")
if v != nil {
m.parseArrayJava(v, n)
}
}
}
func (m *Model) printValuesJava(javaType, key string) {
const tmpl = `
@SerializedName("{{.Key}}")
private {{.Type}} {{.LowerName}};
public {{.Type}} get{{.Name}}() {
return {{.LowerName}};
}
public void set{{.Name}}({{.Type}} {{.LowerName}}) {
this.{{.LowerName}} = {{.LowerName}};
}
`
tmpName := replaceName(key)
data := struct {
Type string
Key string
Name string
LowerName string
}{
javaType,
key,
tmpName,
strings.ToLower(tmpName),
}
t := template.Must(template.New("type").Parse(tmpl))
t.Execute(m.Writer, data)
}
func replaceName(name string) string {
newString := ""
for _, r := range name {
if unicode.IsLetter(r) || unicode.IsNumber(r) {
newString += string(r)
} else {
newString += " "
}
}
newString = strings.Title(newString)
newString = strings.Replace(newString, " ", "", -1)
newString = strings.Replace(newString, "Url", "URL", -1)
newString = strings.Replace(newString, "Uri", "URI", -1)
newString = strings.Replace(newString, "Id", "ID", -1)
r, _ := utf8.DecodeRuneInString(name)
if !unicode.IsLetter(r) && !(r == '_') {
newString = "_" + newString
}
return newString
}
func Mock(b []byte, i interface{}) ([]byte, error) {
err := json.Unmarshal(b, &i)
if err != nil {
return nil, err
}
var buf bytes.Buffer
fmt.Fprintf(&buf, "package main\nvar test = %#v", i)
form, err := format.Source(buf.Bytes())
if err != nil {
return nil, err
}
//err = ioutil.WriteFile("test_mock.go", buf.Bytes(), 0644)
//if err != nil {
// return nil, err
//}
return form, nil
}
func getSortedKeys(m map[string]interface{}) (keys []string) {
for key := range m {
keys = append(keys, key)
}
sort.Sort(ByIDFirst(keys))
return
}
type ByIDFirst []string
func (p ByIDFirst) Len() int { return len(p) }
func (p ByIDFirst) Less(i, j int) bool {
if p[i] == "id" {
return true
} else if p[j] == "id" {
return false
}
return p[i] < p[j]
}
func (p ByIDFirst) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func getName(u string) string {
p, err := url.Parse(u)
if err != nil {
return "Data"
}
s := strings.Split(p.Path, "/")
if len(s) < 1 {
return "Data"
}
return strings.Title(s[len(s)-1])
}
const gsonTempl = `
package com.example.gson
import com.google.gson.annotations.SerializedName;
{{if .IsArray}}//NOTE: use as an array{{end}}
public class {{.Name}} {
private static final Gson gson = new Gson();
public static {{.Name}} fromJsonString(String s) {
return gson.fromJson(s, {{.Name}}.class);
}
`
const goTempl = `type {{.Name}} {{if .IsArray}}[]{{end}}struct {
`