-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparam.go
More file actions
134 lines (116 loc) · 3.02 KB
/
param.go
File metadata and controls
134 lines (116 loc) · 3.02 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
package echopen
import (
"net/http"
"reflect"
v310 "github.com/richjyoung/echopen/openapi/v3.1.0"
)
type PathParameterConfig struct {
Name string
Description string
Examples []*v310.Example
Schema *v310.Schema
}
type HeaderParameterConfig struct {
Name string
Description string
Required bool
Examples []*v310.Example
Style string
Explode bool
Schema *v310.Schema
AllowMultiple bool
}
type CookieParameterConfig struct {
Name string
Description string
Required bool
Schema *v310.Schema
}
func WithParameter(param *v310.Parameter) RouteConfigFunc {
return func(rw *RouteWrapper) *RouteWrapper {
rw.Operation.AddParameter(param)
return rw
}
}
func WithPathParameterConfig(c *PathParameterConfig) RouteConfigFunc {
return WithParameter(&v310.Parameter{
Name: c.Name,
In: "path",
Description: c.Description,
Required: true,
Examples: c.Examples,
Schema: c.Schema,
})
}
func WithHeaderParameterConfig(c *HeaderParameterConfig) RouteConfigFunc {
return WithParameter(&v310.Parameter{
Name: http.CanonicalHeaderKey(c.Name),
In: "header",
Description: c.Description,
Required: c.Required,
Examples: c.Examples,
Schema: c.Schema,
Explode: c.Explode,
Style: c.Style,
})
}
func WithCookieParameterConfig(c *CookieParameterConfig) RouteConfigFunc {
return WithParameter(&v310.Parameter{
Name: c.Name,
In: "cookie",
Description: c.Description,
Required: c.Required,
Schema: c.Schema,
})
}
func WithPathParameter(name string, description string, example interface{}) RouteConfigFunc {
return func(rw *RouteWrapper) *RouteWrapper {
pathParam := &PathParameterConfig{
Name: name,
Description: description,
}
if example != nil {
t := reflect.TypeOf(example)
zero := reflect.New(t).Elem().Interface()
pathParam.Schema = rw.API.TypeToSchema(t)
if example != zero {
pathParam.Examples = []*v310.Example{
{Value: example},
}
}
}
return WithPathParameterConfig(pathParam)(rw)
}
}
func WithHeaderParameter(name string, description string, example interface{}) RouteConfigFunc {
return func(rw *RouteWrapper) *RouteWrapper {
hdrParam := &HeaderParameterConfig{
Name: name,
Description: description,
}
if example != nil {
t := reflect.TypeOf(example)
zero := reflect.New(t).Elem().Interface()
hdrParam.Schema = rw.API.TypeToSchema(t)
if example != zero {
hdrParam.Examples = []*v310.Example{
{Value: example},
}
}
}
return WithHeaderParameterConfig(hdrParam)(rw)
}
}
func WithCookieParameter(name string, description string, example interface{}) RouteConfigFunc {
return func(rw *RouteWrapper) *RouteWrapper {
cookieParam := &CookieParameterConfig{
Name: name,
Description: description,
}
if example != nil {
t := reflect.TypeOf(example)
cookieParam.Schema = rw.API.TypeToSchema(t)
}
return WithCookieParameterConfig(cookieParam)(rw)
}
}