-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathgoogleapi.go
More file actions
143 lines (130 loc) · 3.51 KB
/
Copy pathgoogleapi.go
File metadata and controls
143 lines (130 loc) · 3.51 KB
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
// Package gendoc generates documentation for REST APIs.
package gendoc
import (
"encoding/json"
"io"
"path"
"sort"
"strings"
)
func ParseGoogleAPI(r io.Reader) (Document, error) {
var d doc
if err := json.NewDecoder(r).Decode(&d); err != nil {
return Document{}, err
}
return d.toDocument(), nil
}
// doc represents a Google API specification document. It is NOT intended to encompass all
// options provided by the spec, only the minimal fields needed to convert dex's API
// definitions into documentation.
type doc struct {
Name string `json:"name"`
Version string `json:"version"`
Title string `json:"title"`
Description string `json:"description"`
DocumentationLink string `json:"documentationLink"`
Protocol string `json:"protocol"`
BasePath string `json:"basePath"`
Schemas map[string]schema `json:"schemas"`
Resources map[string]methods `json:"resources"`
}
type methods struct {
Methods map[string]resource `json:"methods"`
}
type param struct {
Type string `json:"type"`
Required bool `json:"required"`
Location string `json:"location"`
}
type schema struct {
ID string `json:"id"`
Type string `json:"type"`
Description string `json:"description"`
Items *schema `json:"items"`
Format string `json:"format"`
Properties map[string]schema
Ref string `json:"$ref"`
}
type resource struct {
Description string `json:"description"`
Method string `json:"httpMethod"`
Path string `json:"path"`
Parameters map[string]param `json:"parameters"`
Request *ref `json:"request"`
Response *ref `json:"response"`
}
type ref struct {
Ref string `json:"$ref"`
}
func (d doc) toDocument() Document {
gDoc := Document{
Title: d.Title,
Description: d.Description,
Version: d.Version,
}
for name, s := range d.Schemas {
s.ID = name
gDoc.Models = append(gDoc.Models, s.toSchema())
}
for object, methods := range d.Resources {
for action, r := range methods.Methods {
gDoc.Paths = append(gDoc.Paths, r.toPath(d, object, action))
}
}
sort.Sort(byPath(gDoc.Paths))
sort.Sort(byName(gDoc.Models))
return gDoc
}
func (s schema) toSchema() Schema {
sch := Schema{
Name: s.ID,
Type: s.Type,
Description: s.Description,
Ref: s.Ref,
}
for name, prop := range s.Properties {
c := prop.toSchema()
c.Name = name
sch.Children = append(sch.Children, c)
}
if s.Items != nil {
sch.Children = []Schema{s.Items.toSchema()}
}
sort.Sort(byName(sch.Children))
return sch
}
func (r resource) toPath(d doc, object, action string) Path {
p := Path{
Method: r.Method,
Path: path.Join("/", r.Path),
Summary: strings.TrimSpace(action + " " + object),
Description: r.Description,
}
for name, param := range r.Parameters {
p.Parameters = append(p.Parameters, Parameter{
Name: name,
LocatedIn: param.Location,
Required: param.Required,
Type: param.Type,
})
}
if r.Request != nil {
ref := r.Request.Ref
p.Parameters = append(p.Parameters, Parameter{
LocatedIn: "body",
Required: true,
Type: ref,
})
}
if r.Response != nil {
p.Responses = append(p.Responses, Response{
Code: 200,
Type: r.Response.Ref,
})
}
p.Responses = append(p.Responses, Response{
Code: CodeDefault,
Description: "Unexpected error",
})
return p
}