-
Notifications
You must be signed in to change notification settings - Fork 5
/
group.go
executable file
·147 lines (129 loc) · 3.51 KB
/
group.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
package swaggos
import (
"strings"
"github.com/go-openapi/spec"
)
// Group is a path group for same prefix
type Group struct {
prefix string
swaggos *Swaggos
params []spec.Parameter
tag string
}
// NewGroup returns a new Group
func NewGroup(prefix string, swaggos *Swaggos) *Group {
group := new(Group)
prefix = "/" + strings.Trim(prefix, "/")
group.prefix = prefix
group.swaggos = swaggos
return group
}
// Group returns a new group based on current group
func (g *Group) Group(prefix string) *Group {
group := new(Group)
if g.prefix == "/" {
prefix = g.prefix + strings.Trim(prefix, "/")
} else {
prefix = g.prefix + "/" + strings.Trim(prefix, "/")
}
group.prefix = prefix
group.swaggos = g.swaggos
group.params = make([]spec.Parameter, 0)
group.tag = g.tag
copy(group.params, g.params)
return group
}
func (g *Group) Tag(t string) *Group {
g.tag = t
return g
}
// Header defines a header param
func (g *Group) Header(name string, desc string, required bool) *Group {
param := spec.Parameter{
SimpleSchema: spec.SimpleSchema{
Type: _String,
},
ParamProps: spec.ParamProps{
Description: desc,
Name: name,
In: _InHeader,
Required: required,
},
}
g.params = append(g.params, param)
return g
}
// Query add query param to the group
func (g *Group) Query(name string, desc string, required bool) *Group {
param := spec.Parameter{
SimpleSchema: spec.SimpleSchema{
Type: _String,
},
ParamProps: spec.ParamProps{
Description: desc,
Name: name,
In: _InQuery,
Required: required,
},
}
g.params = append(g.params, param)
return g
}
// Form add form param to the group
func (g *Group) Form(name string, desc string, required bool) *Group {
param := spec.Parameter{
SimpleSchema: spec.SimpleSchema{
Type: _String,
},
ParamProps: spec.ParamProps{
Description: desc,
Name: name,
In: _InForm,
Required: required,
},
}
g.params = append(g.params, param)
return g
}
// Get create a path with group's prefix and given path of Get method
func (g *Group) Get(path string) *Path {
p := g.swaggos.Get(g.trimPath(path))
return p.addParam(g.params...).Tag(g.tag)
}
// Post create a path with group's prefix and given path of Post method
func (g *Group) Post(path string) *Path {
p := g.swaggos.Post(g.trimPath(path))
return p.addParam(g.params...).Tag(g.tag)
}
// Put create a path with group's prefix and given path of Put method
func (g *Group) Put(path string) *Path {
p := g.swaggos.Put(g.trimPath(path))
return p.addParam(g.params...).Tag(g.tag)
}
// Patch create a path with group's prefix and given path of Patch method
func (g *Group) Patch(path string) *Path {
p := g.swaggos.Patch(g.trimPath(path))
return p.addParam(g.params...).Tag(g.tag)
}
// Options create a path with group's prefix and given path of Options method
func (g *Group) Options(path string) *Path {
p := g.swaggos.Options(g.trimPath(path))
return p.addParam(g.params...).Tag(g.tag)
}
// Delete create a path with group's prefix and given path of Delete method
func (g *Group) Delete(path string) *Path {
p := g.swaggos.Delete(g.trimPath(path))
return p.addParam(g.params...).Tag(g.tag)
}
func (g *Group) trimPath(path string) string {
path = "/" + strings.Trim(path, "/")
return "/" + strings.Trim(g.prefix+path, "/")
}
// Swaggos returns instance of Swaggos
func (g *Group) Swaggos() *Swaggos {
return g.swaggos
}
// Group setup the group of swaggos
func (swaggos *Swaggos) Group(path string) *Group {
return NewGroup(path, swaggos)
}