-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookie.go
151 lines (120 loc) · 2.98 KB
/
cookie.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
package inflate
import (
"fmt"
"net/http"
"reflect"
"strings"
)
// CookieProvider represents a parameter provider that fetches values from
// incoming request's cookies
type CookieProvider struct {
Cookies []*http.Cookie
}
// NewCookieDecoder creates a cookie decoder
func NewCookieDecoder(cookies []*http.Cookie) *Decoder {
return &Decoder{
TagName: "cookie",
Converter: &Converter{
TagName: "cookie",
},
Provider: &CookieProvider{
Cookies: cookies,
},
}
}
// Value returns a primitive value
func (p *CookieProvider) Value(ctx *Context) (interface{}, error) {
if ctx.Tag.Name == "" {
return nil, nil
}
if len(ctx.Tag.Options) == 0 {
ctx.Tag.AddOption(OptionForm)
switch ctx.Type.Kind() {
case reflect.Map, reflect.Struct:
case reflect.Array, reflect.Slice:
default:
ctx.Tag.AddOption(OptionExplode)
}
}
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:
return p.arrayOf(ctx)
default:
return p.valueOf(ctx)
}
}
func (p *CookieProvider) valueOf(ctx *Context) (interface{}, error) {
cookie := p.cookie(ctx.Tag.Name)
if cookie == nil {
return nil, nil
}
if !ctx.Tag.HasOption(OptionForm) {
return nil, p.notProvided(ctx, OptionForm)
}
return cookie.Value, nil
}
func (p *CookieProvider) arrayOf(ctx *Context) ([]interface{}, error) {
cookie := p.cookie(ctx.Tag.Name)
if cookie == nil {
return nil, nil
}
if !ctx.Tag.HasOption(OptionForm) {
return nil, p.notProvided(ctx, OptionForm)
}
if ctx.Tag.HasOption(OptionExplode) {
return nil, p.notSupported(ctx, OptionExplode)
}
var (
separator = ","
parts = strings.Split(cookie.Value, separator)
result = make([]interface{}, len(parts))
)
for index, part := range parts {
result[index] = part
}
return result, nil
}
func (p *CookieProvider) mapOf(ctx *Context) (map[string]interface{}, error) {
cookie := p.cookie(ctx.Tag.Name)
if cookie == nil {
return nil, nil
}
if !ctx.Tag.HasOption(OptionForm) {
return nil, p.notProvided(ctx, OptionForm)
}
if ctx.Tag.HasOption(OptionExplode) {
return nil, p.notSupported(ctx, OptionExplode)
}
var (
separator = ","
parts = strings.Split(cookie.Value, separator)
)
m, err := convertMap(parts)
if err != nil {
return nil, p.errorf(err.Error())
}
return m, nil
}
func (p *CookieProvider) cookie(name string) *http.Cookie {
for _, cookie := range p.Cookies {
if strings.EqualFold(cookie.Name, name) {
return cookie
}
}
return nil
}
func (p *CookieProvider) notProvided(ctx *Context, opts ...string) error {
return p.errorf("field: '%v' option: %v not provided", ctx.Tag.Name, opts)
}
func (p *CookieProvider) notSupported(ctx *Context, opt string) error {
return p.errorf("field: '%v' option: [%v] not supported", ctx.Tag.Name, opt)
}
func (p *CookieProvider) errorf(msg string, values ...interface{}) error {
msg = fmt.Sprintf(msg, values...)
return fmt.Errorf("cookie: %s", msg)
}