-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswagger.go
120 lines (105 loc) · 2.92 KB
/
swagger.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
package main
type APIService struct {
ServiceInterfaces []ServiceInterface
}
func NewAPIService(api *API) *APIService {
var l []ServiceInterface
for url, paths := range api.Paths {
serviceInterface := NewServiceInterface()
if paths.Get != nil {
serviceInterface.loadData(url, "Get", paths.Get, api.Definitions)
}
if paths.Post != nil {
serviceInterface.loadData(url, "Post", paths.Post, api.Definitions)
}
if paths.Put != nil {
serviceInterface.loadData(url, "Put", paths.Put, api.Definitions)
}
if paths.Patch != nil {
serviceInterface.loadData(url, "Patch", paths.Patch, api.Definitions)
}
if paths.Delete != nil {
serviceInterface.loadData(url, "Delete", paths.Delete, api.Definitions)
}
if paths.Head != nil {
serviceInterface.loadData(url, "Head", paths.Head, api.Definitions)
}
l = append(l, *serviceInterface)
}
return &APIService{
ServiceInterfaces: l,
}
}
type ServiceInterface struct {
OperationID string
Description string
Tag string
Method string
URI string
Params []Parameter
Body []Parameter
Definitions map[string]Definition
}
func NewServiceInterface() *ServiceInterface {
return &ServiceInterface{
Definitions: map[string]Definition{},
}
}
func (s *ServiceInterface) loadData(uri, method string, p *Operation, d map[string]Definition) {
s.OperationID = p.OperationID
s.Description = p.Description
s.Tag = p.Tags[0]
s.Method = method
s.URI = uri
if len(p.Parameters) > 0 {
for _, parameter := range p.Parameters {
s.Params = append(s.Params, parameter)
}
}
for _, resp := range p.Responses {
if ref, ok := resp.Schema["myRef"]; ok {
for k, v := range d[ref.(string)].Properties {
if v.MyRef != "" {
v.Type = d[v.MyRef].Type + " (" + d[v.MyRef].Title + ")"
s.Definitions[d[v.MyRef].Title] = d[v.MyRef]
}
if v.Type == "array" {
if _, ok := v.Items["myRef"]; ok {
v.Type = "array (" + d[v.Items["myRef"]].Title + ")"
s.Definitions[d[v.Items["myRef"]].Title] = d[v.Items["myRef"]]
}
}
param := Parameter{
Name: k,
Type: v.Type,
Description: v.Description,
}
s.Body = append(s.Body, param)
}
}
if typ, ok := resp.Schema["type"]; ok {
if typ.(string) == "array" {
if items, ok := resp.Schema["items"]; ok {
for k, v := range d[items.(map[string]interface{})["myRef"].(string)].Properties {
if v.MyRef != "" {
v.Type = d[v.MyRef].Type + " (" + d[v.MyRef].Title + ")"
s.Definitions[d[v.MyRef].Title] = d[v.MyRef]
}
if v.Type == "array" {
if _, ok := v.Items["myRef"]; ok {
v.Type = "array (" + d[v.Items["myRef"]].Title + ")"
s.Definitions[d[v.Items["myRef"]].Title] = d[v.Items["myRef"]]
}
}
param := Parameter{
Name: k,
Type: v.Type,
Description: v.Description,
}
s.Body = append(s.Body, param)
}
}
}
}
}
}