-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparams.go
141 lines (113 loc) · 2.55 KB
/
params.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
package hyper
import (
"log"
"github.com/vaniila/hyper/router"
)
type param struct {
typ router.ParamType
custom router.CustomFunc
format int
name string
summary string
documentation string
defaults []byte
deps []router.Param
oneof []router.Param
require bool
}
type paramconfig struct {
*param
}
func (v *param) Custom(c router.CustomFunc) router.Param {
v.custom = c
return v
}
func (v *param) Format(f int) router.Param {
v.format = f
return v
}
func (v *param) Summary(s string) router.Param {
v.summary = s
return v
}
func (v *param) Doc(s string) router.Param {
v.documentation = s
return v
}
func (v *param) Default(b []byte) router.Param {
v.defaults = b
return v
}
func (v *param) Require(b bool) router.Param {
v.require = b
return v
}
func (v *param) DependsOn(deps ...router.Param) router.Param {
v.deps = deps
return v
}
func (v *param) Config() router.ParamConfig {
return ¶mconfig{v}
}
func (v *paramconfig) Name() string {
return v.name
}
func (v *paramconfig) Type() router.ParamType {
return v.typ
}
func (v *paramconfig) Custom() router.CustomFunc {
return v.custom
}
func (v *paramconfig) Format() int {
return v.format
}
func (v *paramconfig) Summary() string {
return v.summary
}
func (v *paramconfig) Doc() string {
return v.documentation
}
func (v *paramconfig) Default() []byte {
return v.defaults
}
func (v *paramconfig) OneOf() []router.Param {
return v.oneof
}
func (v *paramconfig) Require() bool {
return v.require
}
func (v *paramconfig) DependsOn() []router.Param {
return v.deps
}
// Query func
func Query(name string) router.Param {
return ¶m{typ: router.ParamQuery, name: name}
}
// Body func
func Body(name string) router.Param {
return ¶m{typ: router.ParamBody, name: name}
}
// Param func
func Param(name string) router.Param {
return ¶m{typ: router.ParamParam, name: name}
}
// Header func
func Header(name string) router.Param {
return ¶m{typ: router.ParamHeader, name: name}
}
// Cookie func
func Cookie(name string) router.Param {
return ¶m{typ: router.ParamCookie, name: name}
}
// OneOf group func
func OneOf(ps ...router.Param) router.Param {
for _, param := range ps {
if param.Config().Require() {
log.Fatalf("cannot set %v field as required, [oneof] parameters do not support required checking", param.Config().Name())
}
if param.Config().Type() == router.ParamOneOf {
log.Fatal("cannot add oneof field within a oneof group")
}
}
return ¶m{typ: router.ParamOneOf, oneof: ps}
}