struct_decoder.go
2.5 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
package urlstruct
import (
"context"
"net/url"
"reflect"
"strings"
)
type structDecoder struct {
v reflect.Value
sinfo *StructInfo
decMap map[string]*structDecoder
paramUnmarshaler ParamUnmarshaler
}
func newStructDecoder(v reflect.Value) *structDecoder {
v = reflect.Indirect(v)
return &structDecoder{
v: v,
sinfo: DescribeStruct(v.Type()),
}
}
func (d *structDecoder) Decode(ctx context.Context, values url.Values) error {
var maps map[string][]string
for name, values := range values {
name = strings.TrimPrefix(name, ":")
name = strings.TrimSuffix(name, "[]")
if name, key, ok := mapKey(name); ok {
if mdec := d.mapDecoder(name); mdec != nil {
if err := mdec.decodeParam(ctx, key, values); err != nil {
return err
}
continue
}
if maps == nil {
maps = make(map[string][]string)
}
maps[name] = append(maps[name], key, values[0])
continue
}
if err := d.decodeParam(ctx, name, values); err != nil {
return err
}
}
for name, values := range maps {
if err := d.decodeParam(ctx, name, values); err != nil {
return nil
}
}
for _, idx := range d.sinfo.unmarshalerIndexes {
fv := d.v.FieldByIndex(idx)
if fv.Kind() == reflect.Struct {
fv = fv.Addr()
} else if fv.IsNil() {
fv.Set(reflect.New(fv.Type().Elem()))
}
u := fv.Interface().(Unmarshaler)
if err := u.UnmarshalValues(ctx, values); err != nil {
return err
}
}
if d.sinfo.isUnmarshaler {
return d.v.Addr().Interface().(Unmarshaler).UnmarshalValues(ctx, values)
}
return nil
}
func (d *structDecoder) mapDecoder(name string) *structDecoder {
if dec, ok := d.decMap[name]; ok {
return dec
}
if idx, ok := d.sinfo.structs[name]; ok {
dec := newStructDecoder(d.v.FieldByIndex(idx))
if d.decMap == nil {
d.decMap = make(map[string]*structDecoder)
d.decMap[name] = dec
}
return dec
}
return nil
}
func (d *structDecoder) decodeParam(ctx context.Context, name string, values []string) error {
if field := d.sinfo.Field(name); field != nil && !field.noDecode {
return field.scanValue(field.Value(d.v), values)
}
if d.sinfo.isParamUnmarshaler {
if d.paramUnmarshaler == nil {
d.paramUnmarshaler = d.v.Addr().Interface().(ParamUnmarshaler)
}
return d.paramUnmarshaler.UnmarshalParam(ctx, name, values)
}
return nil
}
func mapKey(s string) (name string, key string, ok bool) {
ind := strings.IndexByte(s, '[')
if ind == -1 || s[len(s)-1] != ']' {
return "", "", false
}
key = s[ind+1 : len(s)-1]
if key == "" {
return "", "", false
}
name = s[:ind]
return name, key, true
}