query: honor Encoder on interface fields
reflectValue checked sv.Type().Implements(encoderType) against the static
field type, so a value stored in an interface{} field never used its
EncodeValues method and fell back to default formatting. Unwrap the interface
first so the concrete type's Encoder is seen.
Fixes #121
Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
diff --git a/query/encode.go b/query/encode.go
index c936954..3b058bb 100644
--- a/query/encode.go
+++ b/query/encode.go
@@ -186,6 +186,11 @@
continue
}
+ // unwrap interface values so the concrete type's Encoder is used
+ if sv.Kind() == reflect.Interface && !sv.IsNil() {
+ sv = sv.Elem()
+ }
+
if sv.Type().Implements(encoderType) {
// if sv is a nil pointer and the custom encoder is defined on a non-pointer
// method receiver, set sv to the zero value of the underlying type
diff --git a/query/encode_test.go b/query/encode_test.go
index a94b44d..241e4f1 100644
--- a/query/encode_test.go
+++ b/query/encode_test.go
@@ -477,6 +477,14 @@
}{(*customEncodedStrings)(&[]string{"a", "b"})},
url.Values{"v.0": {"a"}, "v.1": {"b"}},
},
+
+ // custom encoded type held in an interface field
+ {
+ struct {
+ V interface{} `url:"v"`
+ }{customEncodedStrings{"a", "b"}},
+ url.Values{"v.0": {"a"}, "v.1": {"b"}},
+ },
}
for _, tt := range tests {