-
Notifications
You must be signed in to change notification settings - Fork 2
/
cookie.go
55 lines (49 loc) · 1.65 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
package chimera
import (
"net/http"
"reflect"
)
var (
cookieParamUnmarshalerType = reflect.TypeOf((*CookieParamUnmarshaler)(nil)).Elem()
cookieParamMarshalerType = reflect.TypeOf((*CookieParamMarshaler)(nil)).Elem()
)
// CookieParamUnmarshaler is an interface that supports converting an http.Cookie to a user-defined type
type CookieParamUnmarshaler interface {
UnmarshalCookieParam(http.Cookie, ParamStructTag) error
}
// CookieParamMarshaler is an interface that supports converting a user-defined type to an http.Cookie
type CookieParamMarshaler interface {
MarshalCookieParam(ParamStructTag) (http.Cookie, error)
}
// unmarshalCookieParam converts a cookie to a value using the options in tag
func unmarshalCookieParam(param http.Cookie, tag *ParamStructTag, addr reflect.Value) error {
addr = fixPointer(addr)
if tag.schemaType == interfaceType {
return addr.Interface().(CookieParamUnmarshaler).UnmarshalCookieParam(param, *tag)
}
return unmarshalStringParam(param.Value, tag, addr)
}
// unmarshalCookieParam converts a value to a http.Cookie using the options in tag
func marshalCookieParam(tag *ParamStructTag, addr reflect.Value) (http.Cookie, error) {
addr = fixPointer(addr)
switch tag.schemaType {
case interfaceType:
return addr.Interface().(CookieParamMarshaler).MarshalCookieParam(*tag)
case primitiveType:
return http.Cookie{
Name: tag.Name,
Value: marshalPrimitiveToString(addr),
}, nil
case sliceType:
return http.Cookie{
Name: tag.Name,
Value: marshalSliceToString(addr),
}, nil
case structType:
return http.Cookie{
Name: tag.Name,
Value: marshalStructToString(addr, tag),
}, nil
}
return http.Cookie{}, nil
}