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
|
package builder
import (
"net/http"
"strconv"
"github.com/invopop/jsonschema"
)
type PathOption func(map[string]interface{})
type Builder interface {
AddPath(string) PathBuilder
Build() *Document
}
type PathBuilder interface {
Get(string, string, ...PathOption)
}
type builder struct {
doc *Document
}
type pathBuilder struct {
root string
builder *builder
}
func (b *pathBuilder) Get(description string, summary string, options ...PathOption) {
get := map[string]interface{}{
"description": description,
"summary": summary,
"parameters": []interface{}{},
"responses": map[string]interface{}{},
}
for _, option := range options {
option(get)
}
paths := b.builder.doc.Paths
paths[b.root] = map[string]interface{}{
"get": get,
}
}
func New(title, version string) Builder {
return &builder{
doc: &Document{
Version: "3.1.0",
Info: &Info{
Title: title,
Version: version,
},
Paths: Paths{},
},
}
}
func (b *builder) AddPath(path string) PathBuilder {
return &pathBuilder{
root: path,
builder: b,
}
}
func (b *builder) Build() *Document {
return b.doc
}
func WithResponse(status int, mediaType string, schema *jsonschema.Schema) PathOption {
return func(paths map[string]interface{}) {
responses := paths["responses"].(map[string]interface{})
if value, ok := responses[strconv.Itoa(status)]; ok {
response := value.(map[string]interface{})
response["content"].(map[string]interface{})[mediaType] = map[string]*jsonschema.Schema{
"schema": schema,
}
} else {
responses[strconv.Itoa(status)] = map[string]interface{}{
"description": http.StatusText(status),
"content": map[string]interface{}{
mediaType: map[string]*jsonschema.Schema{
"schema": schema,
},
},
}
}
}
}
func WithParameter(in string, name string, schema *jsonschema.Schema) PathOption {
return func(paths map[string]interface{}) {
params := paths["parameters"].([]interface{})
params = append(params, map[string]interface{}{
"in": in,
"name": name,
"schema": schema,
})
paths["parameters"] = params
}
}
|