-
Notifications
You must be signed in to change notification settings - Fork 5
/
marshal_values.go
226 lines (197 loc) · 5.43 KB
/
marshal_values.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package qs
import (
"fmt"
"net/url"
"reflect"
)
// structMarshaler implements ValuesMarshaler.
type structMarshaler struct {
Type reflect.Type
EmbeddedFields []embeddedFieldMarshaler
Fields []*fieldMarshaler
}
type embeddedFieldMarshaler struct {
FieldIndex int
ValuesMarshaler ValuesMarshaler
}
type fieldMarshaler struct {
FieldIndex int
Marshaler Marshaler
Tag parsedTag
}
// newStructMarshaler creates a struct marshaler for a specific struct type.
func newStructMarshaler(t reflect.Type, opts *MarshalOptions) (ValuesMarshaler, error) {
if t.Kind() != reflect.Struct {
return nil, &wrongKindError{Expected: reflect.Struct, Actual: t}
}
sm := &structMarshaler{
Type: t,
}
for i, numField := 0, t.NumField(); i < numField; i++ {
sf := t.Field(i)
vm, fm, err := newFieldMarshaler(sf, opts)
if err != nil {
return nil, fmt.Errorf("error creating marshaler for field %v of struct %v :: %v",
sf.Name, t, err)
}
if vm != nil {
sm.EmbeddedFields = append(sm.EmbeddedFields, embeddedFieldMarshaler{
FieldIndex: i,
ValuesMarshaler: vm,
})
}
if fm != nil {
fm.FieldIndex = i
sm.Fields = append(sm.Fields, fm)
}
}
return sm, nil
}
func newFieldMarshaler(sf reflect.StructField, opts *MarshalOptions) (vm ValuesMarshaler, fm *fieldMarshaler, err error) {
skip, tag, err := getStructFieldInfo(sf, opts.NameTransformer, opts.DefaultMarshalPresence, UPUnspecified)
if skip || err != nil {
return
}
t := sf.Type
if sf.Anonymous {
vm, err = opts.ValuesMarshalerFactory.ValuesMarshaler(t, opts)
if err == nil {
// We can end up here for example in case of an embedded struct.
return
}
}
m, err := opts.MarshalerFactory.Marshaler(t, opts)
if err != nil {
return
}
fm = &fieldMarshaler{
Marshaler: m,
Tag: tag,
}
return
}
func (p *structMarshaler) MarshalValues(v reflect.Value, opts *MarshalOptions) (url.Values, error) {
t := v.Type()
if t != p.Type {
return nil, &wrongTypeError{Actual: t, Expected: p.Type}
}
// TODO: use a StructError error type in the function to generate
// error messages prefixed with the name of the struct type.
vs := make(url.Values, len(p.Fields))
for _, fm := range p.Fields {
fv := v.Field(fm.FieldIndex)
if fm.Tag.MarshalPresence == OmitEmpty && isEmpty(fv) {
continue
}
a, err := fm.Marshaler.Marshal(fv, opts)
if err != nil {
return nil, fmt.Errorf("error marshaling url.Values entry %q :: %v", fm.Tag.Name, err)
}
if len(a) != 0 {
vs[fm.Tag.Name] = a
}
}
for _, ef := range p.EmbeddedFields {
evs, err := ef.ValuesMarshaler.MarshalValues(v.Field(ef.FieldIndex), opts)
if err != nil {
return nil, fmt.Errorf("error marshaling embedded field %q :: %v", v.Type().Field(ef.FieldIndex).Name, err)
}
for k, a := range evs {
vs[k] = a
}
}
return vs, nil
}
func isEmpty(v reflect.Value) bool {
switch v.Kind() {
case reflect.Ptr:
return v.IsNil()
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0.0
case reflect.String, reflect.Array, reflect.Slice, reflect.Map:
return v.Len() == 0
default:
return false
}
}
type mapMarshaler struct {
Type reflect.Type
ElemMarshaler Marshaler
}
func newMapMarshaler(t reflect.Type, opts *MarshalOptions) (ValuesMarshaler, error) {
if t.Kind() != reflect.Map {
return nil, &wrongKindError{Expected: reflect.Map, Actual: t}
}
if t.Key() != stringType {
return nil, fmt.Errorf("map key type is expected to be string: %v", t)
}
et := t.Elem()
m, err := opts.MarshalerFactory.Marshaler(et, opts)
if err != nil {
// TODO: use a MapError error type in the function to generate
// error messages prefixed with the name of the struct type.
return nil, fmt.Errorf("error getting marshaler for map value type %v :: %v", et, err)
}
return &mapMarshaler{
Type: t,
ElemMarshaler: m,
}, nil
}
func (p *mapMarshaler) MarshalValues(v reflect.Value, opts *MarshalOptions) (url.Values, error) {
t := v.Type()
if t != p.Type {
return nil, &wrongTypeError{Actual: t, Expected: p.Type}
}
vlen := v.Len()
if vlen == 0 {
return nil, nil
}
vs := make(url.Values, vlen)
for _, key := range v.MapKeys() {
val := v.MapIndex(key)
if opts.DefaultMarshalPresence == OmitEmpty && isEmpty(val) {
continue
}
keyStr := key.String()
a, err := p.ElemMarshaler.Marshal(val, opts)
if err != nil {
return nil, fmt.Errorf("error marshaling key %q :: %v", keyStr, err)
}
vs[keyStr] = a
}
return vs, nil
}
type ptrValuesMarshaler struct {
Type reflect.Type
ElemMarshaler ValuesMarshaler
}
func newPtrValuesMarshaler(t reflect.Type, opts *MarshalOptions) (ValuesMarshaler, error) {
if t.Kind() != reflect.Ptr {
return nil, &wrongKindError{Expected: reflect.Ptr, Actual: t}
}
et := t.Elem()
em, err := opts.ValuesMarshalerFactory.ValuesMarshaler(et, opts)
if err != nil {
return nil, err
}
return &ptrValuesMarshaler{
Type: t,
ElemMarshaler: em,
}, nil
}
func (p *ptrValuesMarshaler) MarshalValues(v reflect.Value, opts *MarshalOptions) (url.Values, error) {
t := v.Type()
if t != p.Type {
return nil, &wrongTypeError{Actual: t, Expected: p.Type}
}
if v.IsNil() {
return nil, nil
}
return p.ElemMarshaler.MarshalValues(v.Elem(), opts)
}