You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BindStringToObject ignores Binder behind an optional pointer, and TextUnmarshaler on non-array types #196
Found while working on #193. Reproduced on main at 9b7381c.
BindStringToObjectWithOptions only finds a type's own parsing logic in two narrow cases, so custom types (typically brought in with x-go-type) fail to bind with can not bind to destination of type: slice / struct:
Binder is only checked on dst itself. The check happens before any pointer is dereferenced. For an optional field the destination is a **T, which does not implement Binder even when *T does, so the code falls through to the kind switch.
encoding.TextUnmarshaler is only honored for reflect.Array kinds (the uuid.UUID case). Slice and struct types that implement it, such as net.IP, are rejected.
time.Time and types.Date are unaffected, since they are special-cased by convertibility.
Repro
typeTags []stringfunc (t*Tags) Bind(srcstring) error { *t=strings.Split(src, "|"); returnnil }
typeBStructstruct{ Vstring }
func (b*BStruct) Bind(srcstring) error { b.V=src; returnnil }
typePointstruct{ X, Ystring }
func (p*Point) UnmarshalText(b []byte) error { /* parse "x:y" */returnnil }
varaTagsruntime.BindStringToObject("a|b", &a) // ok: [a b]varap*Tagsruntime.BindStringToObject("a|b", &ap) // error: can not bind to destination of type: slicevarb*BStructruntime.BindStringToObject("x", &b) // error: can not bind to destination of type: structvarpPointruntime.BindStringToObject("1:2", &p) // error: can not bind to destination of type: struct
In the pointer cases the destination pointer is also left allocated (pointing at a zero value) after the error.
Where it shows up
Properties of an exploded form-style object query parameter are bound through this function, one field at a time, with v.Field(i).Addr().Interface() as the destination:
Field type
Query
Result
Tags (Binder, slice)
tags=a|b
binds
*Tags
tags=a|b
error
net.IP / *net.IP (TextUnmarshaler, slice)
ip=192.0.2.1
error
Point / *Point (TextUnmarshaler, struct)
point=1:2
error
Before #195 these errors were swallowed and the whole parameter was silently dropped; since #195 they are returned to the caller.
Possible fix
After dereferencing pointers (and allocating a nil optional pointer), check the addressable value for Binder, then for encoding.TextUnmarshaler, before the kind switch. Care is needed to keep the time.Time / types.Date special cases ahead of TextUnmarshaler, since time.Time implements it with stricter RFC 3339 parsing than the current date-or-datetime fallback.
Activity
fix: let self-parsing types bind as a last resort (#197)
fix: honor Binder behind an optional pointer (#198)