This repository has been archived by the owner on Sep 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.go
115 lines (107 loc) · 2.27 KB
/
models.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
package main
import (
"fmt"
)
func ParseModels(m map[string]interface{}) {
for key, value := range m {
s := GoStruct{Name: key, Parent: ""}
v := value.(map[string]interface{})
tmpgostructs[key] = BuildStruct(s, v)
}
for _, t := range tmpgostructs {
for _, st := range t.SubTypes {
sub := tmpgostructs[st]
sub.Parent = t.Name
tmpgostructs[st] = sub
}
}
for ParentsExist() {
ProcessSubTypes()
}
for _, t := range tmpgostructs {
gostructs[t.Name] = t
}
}
func ParentsExist() bool {
for _, t := range tmpgostructs {
if t.Parent != "" {
return true
}
}
return false
}
func ProcessSubTypes() {
for _, t := range tmpgostructs {
if t.Parent == "" {
for _, subtype := range t.SubTypes {
st := tmpgostructs[subtype]
parent := tmpgostructs[st.Parent]
for _, f := range parent.Fields {
st.Fields = append(st.Fields, f)
}
st.Parent = ""
tmpgostructs[subtype] = st
}
gostructs[t.Name] = t
delete(tmpgostructs, t.Name)
}
}
}
func BuildStruct(s GoStruct, m map[string]interface{}) GoStruct {
for key, value := range m {
switch value.(type) {
case string:
continue
case []interface{}:
switch key {
case "subTypes":
v := value.([]interface{})
s.SubTypes = BuildSubTypes(v)
}
case interface{}:
v := value.(map[string]interface{})
switch key {
case "properties":
s.Fields = BuildFields(v)
}
}
}
return s
}
func BuildSubTypes(st []interface{}) []string {
subtypes := make([]string, 0, 50)
for _, value := range st {
subtypes = append(subtypes, value.(string))
}
return subtypes
}
func BuildFields(m map[string]interface{}) []Field {
fields := make([]Field, 0, 5)
for key, value := range m {
f := Field{Name: Canonicalize(key)}
v := value.(map[string]interface{})
f = BuildField(f, v)
f.JSONName = key
fields = append(fields, f)
}
return fields
}
func BuildField(f Field, m map[string]interface{}) Field {
for key, value := range m {
switch key {
case "type":
v := value.(string)
f.Type = convertType(v)
}
}
return f
}
func OutputStructs() {
for _, s := range gostructs {
fmt.Printf("type %s struct {\n", s.Name)
for _, field := range s.Fields {
fmt.Printf(" %s %s `json:\"%s\"`\n", field.Name, field.Type, field.JSONName)
}
fmt.Println("}\n")
}
}