@@ -33,6 +33,14 @@ import (
33
33
34
34
var timeType = reflect .TypeOf (time.Time {})
35
35
36
+ var encoderType = reflect .TypeOf (new (Encoder )).Elem ()
37
+
38
+ // Encoder is an interface implemented by any type that wishes to encode
39
+ // itself into URL values in a non-standard way.
40
+ type Encoder interface {
41
+ EncodeValues (v * url.Values ) error
42
+ }
43
+
36
44
// Values returns the url.Values encoding of v.
37
45
//
38
46
// Values expects to be passed a struct, and traverses it recursively using the
@@ -107,14 +115,14 @@ func Values(v interface{}) (url.Values, error) {
107
115
}
108
116
109
117
values := make (url.Values )
110
- reflectValue (values , val )
111
- return values , nil
118
+ err := reflectValue (values , val )
119
+ return values , err
112
120
}
113
121
114
122
// reflectValue populates the values parameter from the struct fields in val.
115
123
// Embedded structs are followed recursively (using the rules defined in the
116
124
// Values function documentation) breadth-first.
117
- func reflectValue (values url.Values , val reflect.Value ) {
125
+ func reflectValue (values url.Values , val reflect.Value ) error {
118
126
var embedded []reflect.Value
119
127
120
128
typ := val .Type ()
@@ -144,6 +152,14 @@ func reflectValue(values url.Values, val reflect.Value) {
144
152
continue
145
153
}
146
154
155
+ if sv .Type ().Implements (encoderType ) {
156
+ m := sv .Interface ().(Encoder )
157
+ if err := m .EncodeValues (& values ); err != nil {
158
+ return err
159
+ }
160
+ continue
161
+ }
162
+
147
163
if sv .Kind () == reflect .Slice || sv .Kind () == reflect .Array {
148
164
var del byte
149
165
if opts .Contains ("comma" ) {
@@ -176,8 +192,12 @@ func reflectValue(values url.Values, val reflect.Value) {
176
192
}
177
193
178
194
for _ , f := range embedded {
179
- reflectValue (values , f )
195
+ if err := reflectValue (values , f ); err != nil {
196
+ return err
197
+ }
180
198
}
199
+
200
+ return nil
181
201
}
182
202
183
203
// valueString returns the string representation of a value.
0 commit comments