-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.go
209 lines (173 loc) · 3.86 KB
/
path.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package inflate
import (
"fmt"
"reflect"
"strings"
"github.com/go-chi/chi/v5"
)
// NewPathDecoder creates a path decoder
func NewPathDecoder(r *chi.RouteParams) *Decoder {
return &Decoder{
TagName: "path",
Converter: &Converter{
TagName: "path",
},
Provider: &PathProvider{
Param: r,
},
}
}
var _ ValueProvider = &PathProvider{}
// PathProvider represents a parameter provider that fetches values from
// incoming request's header
type PathProvider struct {
Param *chi.RouteParams
}
// Value returns a primitive value
func (p *PathProvider) Value(ctx *Context) (interface{}, error) {
if ctx.Tag.Name == "" {
return nil, nil
}
if len(ctx.Tag.Options) == 0 {
ctx.Tag.AddOption(OptionSimple)
}
if convertable(ctx.Type) {
return p.valueOf(ctx)
}
switch ctx.Type.Kind() {
case reflect.Map, reflect.Struct:
return p.mapOf(ctx)
case reflect.Array, reflect.Slice:
values, err := p.arrayOf(ctx)
if err != nil {
return nil, err
}
return convertValue(values), nil
default:
return p.valueOf(ctx)
}
}
func (p *PathProvider) valueOf(ctx *Context) (interface{}, error) {
param := p.param(ctx.Tag.Name)
if param == nil {
return nil, nil
}
switch {
case ctx.Tag.HasOption(OptionSimple):
return *param, nil
case ctx.Tag.HasOption(OptionLabel):
prefix := "."
return strings.TrimPrefix(*param, prefix), nil
case ctx.Tag.HasOption(OptionMatrix):
prefix := fmt.Sprintf(";%s=", ctx.Tag.Name)
return strings.TrimPrefix(*param, prefix), nil
default:
return nil, p.notProvided(ctx,
OptionSimple,
OptionLabel,
OptionMatrix,
)
}
}
func (p *PathProvider) arrayOf(ctx *Context) ([]interface{}, error) {
param := p.param(ctx.Tag.Name)
if param == nil {
return nil, nil
}
var (
prefix = ""
separator = ""
)
switch {
case ctx.Tag.HasOption(OptionSimple):
separator = ","
case ctx.Tag.HasOption(OptionLabel):
prefix = "."
separator = ","
if ctx.Tag.HasOption(OptionExplode) {
separator = prefix
}
case ctx.Tag.HasOption(OptionMatrix):
separator = ","
prefix = fmt.Sprintf(";%s=", ctx.Tag.Name)
if ctx.Tag.HasOption(OptionExplode) {
separator = prefix
}
default:
return nil, p.notProvided(ctx,
OptionSimple,
OptionLabel,
OptionMatrix,
)
}
var (
value = strings.TrimPrefix(*param, prefix)
parts = strings.Split(value, separator)
result = make([]interface{}, len(parts))
)
for index, part := range parts {
result[index] = part
}
return result, nil
}
func (p *PathProvider) mapOf(ctx *Context) (m map[string]interface{}, err error) {
param := p.param(ctx.Tag.Name)
if param == nil {
return nil, nil
}
var (
separator string
prefix string
)
switch {
case ctx.Tag.HasOption(OptionSimple):
separator = ","
case ctx.Tag.HasOption(OptionLabel):
prefix = "."
separator = ","
if ctx.Tag.HasOption(OptionExplode) {
separator = prefix
}
case ctx.Tag.HasOption(OptionMatrix):
separator = ","
prefix = fmt.Sprintf(";%s=", ctx.Tag.Name)
if ctx.Tag.HasOption(OptionExplode) {
separator = ";"
prefix = ";"
}
default:
return nil, p.notProvided(ctx,
OptionSimple,
OptionLabel,
OptionMatrix,
)
}
var (
value = strings.TrimPrefix(*param, prefix)
parts = strings.Split(value, separator)
)
if ctx.Tag.HasOption(OptionExplode) {
m, err = explodeMap(parts)
} else {
m, err = convertMap(parts)
}
if err != nil {
return nil, p.errorf(err.Error())
}
return m, nil
}
func (p *PathProvider) param(name string) *string {
for index, k := range p.Param.Keys {
if strings.EqualFold(k, name) {
return &p.Param.Values[index]
}
}
return nil
}
func (p *PathProvider) notProvided(ctx *Context, opts ...string) error {
return p.errorf("field: %v option: %v not provided", ctx.Tag.Name, opts)
}
func (p *PathProvider) errorf(msg string, values ...interface{}) error {
msg = fmt.Sprintf(msg, values...)
return fmt.Errorf("path: %s", msg)
}