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 pathgo-ari-struct-builder.go
175 lines (162 loc) · 3.85 KB
/
go-ari-struct-builder.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"strings"
"unicode"
)
var (
tmpgostructs map[string]GoStruct
gostructs map[string]GoStruct
clientAPIBuf *bytes.Buffer
)
var apiPreamble string = `import (
"fmt"
"bytes"
"encoding/json"
"errors"
)
func buildJSON(params map[string]string) string {
mapsize := len(params)
var counter int = 1
body := bytes.NewBufferString("{")
for key, value := range params {
var s string
if counter < mapsize {
s = fmt.Sprintf("\"%s\":\"%s\",", key, value)
} else {
s = fmt.Sprintf("\"%s\":\"%s\"", key, value)
}
body.WriteString(s)
counter++
}
body.WriteString("}")
return body.String()
}
`
type Swagger struct {
APIVersion string `json:"apiVersion"`
BasePath string `json:"basePath"`
APIs []API `json:"apis"`
Models interface{} `json:"models"`
}
type API struct {
Path string `json:"path"`
Description string `json:"description"`
Operations []Operation `json:"operations"`
}
type Operation struct {
HTTPMethod string `json:"httpMethod"`
Summary string `json:"summary"`
Notes string `json:"notes"`
Nickname string `json:"nickname"`
ResponseClass string `json:"responseClass"`
Parameters []Parameter `json:"parameters"`
ErrorResponses []ErrorReponse `json:"errorResponses"`
}
type ErrorReponse struct {
Code int `json:"code"`
Reason string `json:"reason"`
}
type Parameter struct {
Name string `json:"name"`
Description string `json:"description"`
ParamType string `json:"paramType"`
Required bool `json:"required"`
AllowMultiple bool `json:"allowMultiple"`
DataType string `json:"dataType"`
}
type Models struct {
JSON interface{} `json:"models"`
}
type Field struct {
Name string
Type string
JSONName string
}
type GoStruct struct {
Name string
Fields []Field
SubTypes []string
Parent string
}
func Canonicalize(s string) string {
a := []rune(s)
a[0] = unicode.ToUpper(a[0])
for index, _ := range a {
if index == 0 {
a[0] = unicode.ToUpper(a[0])
continue
}
if a[index-1] == '_' {
a[index] = unicode.ToUpper(a[index])
}
if a[index] == 'I' && a[index+1] == 'd' {
a[index+1] = unicode.ToUpper(a[index+1])
}
}
return string(a)
}
func convertType(s string) string {
if strings.HasPrefix(s, "List[") {
typestring := strings.TrimPrefix(s, "List[")
typestring = strings.TrimSuffix(typestring, "]")
typestring = strings.Join([]string{"[]", typestring}, "")
return typestring
} else if s == "object" {
return "string"
} else if s == "long" {
return "uint64"
} else if s == "double" {
return "float64"
} else if s == "Date" {
return "string"
} else if s == "boolean" {
return "bool"
} else {
return s
}
}
func init() {
gostructs = make(map[string]GoStruct)
tmpgostructs = make(map[string]GoStruct)
clientAPIBuf = bytes.NewBufferString("")
}
func main() {
swaggerDir := flag.String("path", "", "Path to model files")
buildStructs := flag.Bool("structs", true, "Whether or not to build structs")
buildAPI := flag.Bool("api", true, "Whether or not to build the API")
flag.Parse()
files, err := ioutil.ReadDir(*swaggerDir)
if err != nil {
log.Fatal(err)
}
for _, swaggerFile := range files {
if !swaggerFile.IsDir() && strings.HasSuffix(swaggerFile.Name(), ".json") {
apiBase := strings.TrimSuffix(swaggerFile.Name(), ".json")
swaggerPath := strings.Join([]string{*swaggerDir, swaggerFile.Name()}, "/")
swaggerString, err := ioutil.ReadFile(swaggerPath)
if err != nil {
continue
}
var s Swagger
json.Unmarshal(swaggerString, &s)
ParseModels(s.Models.(map[string]interface{}))
BuildAPIs(apiBase, s)
}
}
fmt.Println("package ari")
if *buildAPI {
fmt.Println(apiPreamble)
}
if *buildStructs {
OutputStructs()
}
if *buildAPI {
fmt.Print(clientAPIBuf.String())
}
}