-
Notifications
You must be signed in to change notification settings - Fork 2
/
endpoint.go
183 lines (170 loc) · 5.61 KB
/
endpoint.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
package internal
import (
"sort"
"strings"
)
type endpoint struct {
Id string
Game string
Url string
Name string
Parameters []*goType
OptionsType *goType
DataType *goType
// has to be added via patches, not retrievable from the api documentation
MetaType *goType
AllowedRealms []string
Documentation string
Deprecated bool
HttpMethods []string
// additional imports required by the endpoint method
OtherImports map[string]struct{}
// post-processing code has to include return
PostProcessing string
}
func newEndpointFromDoc(doc *wgMethodDoc, game string, id string) (*endpoint, error) {
ep := endpoint{
Id: id,
Game: snakeToCamelLower(game),
Url: doc.Url,
Name: snakeToCamel(id),
MetaType: nil,
Documentation: strings.TrimPrefix(cleanDocumentation(doc.Description), "Method "),
Deprecated: doc.Deprecated,
HttpMethods: doc.AllowedHttpMethods,
OtherImports: map[string]struct{}{"context": {}},
OptionsType: nil,
}
if !strings.HasPrefix(ep.Url, "/") {
ep.Url = "/" + ep.Url
}
if strings.HasPrefix(id, game+"_") {
ep.Name = snakeToCamel(id[len(game)+1:])
} else {
ep.Name = snakeToCamel(id)
}
ep.AllowedRealms = doc.AvailableDisplayIndices
sort.Strings(ep.AllowedRealms)
ep.DataType = wgDocToGoDataType(strings.TrimPrefix(ep.Id, ep.Game+"_"), doc.Fields)
// created required Parameters
// create optional struct from not required once
optionsStruct := &goType{
Name: ep.Name + "Options",
TypeStr: "struct",
Imports: make(map[string]struct{}),
}
for _, wgParam := range doc.Parameters {
if wgParam.Name == "application_id" {
continue
}
if wgParam.Required {
param := wgToGoType(wgParam.Type)
param.Name = snakeToCamelLower(wgParam.Name)
param.Description = cleanDocumentation(wgParam.Description)
param.JsonTag = wgParam.Name
ep.Parameters = append(ep.Parameters, param)
} else {
field := wgToGoType(wgParam.Type)
field.TypeStr = toPointerType(field.TypeStr)
field.Name = snakeToCamel(wgParam.Name)
field.Description = cleanDocumentation(wgParam.Description)
field.JsonTag = wgParam.Name
optionsStruct.Fields = append(optionsStruct.Fields, field)
}
}
sortParameters(ep.Parameters)
if len(optionsStruct.Fields) > 0 {
sortFields(optionsStruct)
ep.OptionsType = optionsStruct
}
return &ep, nil
}
func (ep *endpoint) MethodImports() []string {
tmp := map[string]struct{}{}
for _, param := range ep.Parameters {
for imp := range param.Imports {
tmp[imp] = struct{}{}
}
imps := valueConvImports(param.TypeStr)
for _, imp := range imps {
tmp[imp] = struct{}{}
}
}
if ep.OptionsType != nil {
for _, field := range ep.OptionsType.Fields {
imps := valueConvImports(field.TypeStr)
for _, imp := range imps {
tmp[imp] = struct{}{}
}
}
}
for imp := range ep.OtherImports {
tmp[imp] = struct{}{}
}
imports := make([]string, 0, len(tmp)+1)
for imp := range tmp {
imports = append(imports, imp)
}
if ep.OptionsType != nil || ep.DataType != nil && ep.DataType.IsStruct() {
imports = append(imports, wgModuleName+"/wargaming/"+ep.Game)
}
sort.Strings(imports)
return imports
}
func wgToGoType(name string) *goType {
return map[string]*goType{
"associative array": {TypeStr: "map[string]string", Imports: make(map[string]struct{})},
"block_header": {TypeStr: "struct", Imports: make(map[string]struct{})},
"boolean": {TypeStr: "bool", Imports: make(map[string]struct{})},
"float": {TypeStr: "float32", Imports: make(map[string]struct{})},
"list of integers": {TypeStr: "[]int", Imports: make(map[string]struct{})},
"numeric": {TypeStr: "int", Imports: make(map[string]struct{})},
"string": {TypeStr: "string", Imports: make(map[string]struct{})},
"timestamp": {TypeStr: "wgnTime.UnixTime", Imports: map[string]struct{}{wgModuleName + "/wargaming/wgnTime": {}}},
"list of strings": {TypeStr: "[]string", Imports: make(map[string]struct{})},
"list of timestamps": {TypeStr: "[]wgnTime.UnixTime", Imports: map[string]struct{}{wgModuleName + "/wargaming/wgnTime": {}}},
"list of dicts": {TypeStr: "map[string]string", Imports: make(map[string]struct{})},
"object": {TypeStr: "struct", Imports: make(map[string]struct{})},
"timestamp/date": {TypeStr: "wgnTime.UnixTime", Imports: map[string]struct{}{wgModuleName + "/wargaming/wgnTime": {}}},
// used in Parameters
"numeric, list": {TypeStr: "[]int", Imports: make(map[string]struct{})},
"string, list": {TypeStr: "[]string", Imports: make(map[string]struct{})},
}[name]
}
func wgDocToGoDataType(idWoPkg string, docFields []*wgFieldDoc) *goType {
if len(docFields) == 0 {
return nil
}
root := &goType{
Name: snakeToCamel(idWoPkg),
TypeStr: "*struct",
JsonTag: idWoPkg,
Imports: map[string]struct{}{},
}
for _, dField := range docFields {
typ := dField.Type
if typ == "empty_line" {
continue
}
// get field on root struct, create missing fields on the way down
nestedField := root
for _, curName := range dField.Name {
curName = snakeToCamel(curName)
curField := nestedField.F(curName)
if curField == nil {
curField = &goType{Name: curName}
nestedField.Fields = append(nestedField.Fields, curField)
}
nestedField = curField
}
// add field values
tmpType := wgToGoType(dField.Type)
nestedField.TypeStr = toPointerType(tmpType.TypeStr)
nestedField.Name = snakeToCamel(dField.Name[len(dField.Name)-1])
nestedField.Description = cleanDocumentation(dField.Description)
nestedField.JsonTag = dField.Name[len(dField.Name)-1]
nestedField.Imports = tmpType.Imports
}
sortFields(root)
return root
}