-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom decoder accepts slice of string but expected to return single item #39
Comments
Hey @vearutop apologies in advance for formatting, answering from my phone :) so each value that is to be decoded is really a URL.Value which is type map[string][]string, that’s where the array of strings comes from that’s passed to the customer decoder functions. As for not passing idx is because it’s not really needed because the custom decoding function is looked up by namespace, so there are only 2 possibilities:
Please let me know if that makes sense, sorry again about the formatting and lack of links |
Here is an example:
I would expect such test to pass, but it fails with:
If I return
Given that we have a loop over |
ah now I understand, thanks @vearutop it is indeed a bug, fixing ASAP |
As a side note here is an alternative that will also give you what you want: package main
import (
"net/url"
"testing"
"github.com/go-playground/form"
. "gopkg.in/go-playground/assert.v1"
)
type customString string
func TestDecoder_RegisterCustomTypeFunc(t *testing.T) {
type TestStruct struct {
Slice []customString `form:"slice"`
}
d := form.NewDecoder()
d.RegisterCustomTypeFunc(func(vals []string) (i interface{}, e error) {
custom := make([]customString, 0, len(vals))
for i := 0; i < len(vals); i++ {
custom = append(custom, customString("custom"+vals[i]))
}
return custom, nil
}, []customString{})
var v TestStruct
err := d.Decode(&v, url.Values{"slice": []string{"v1", "v2"}})
Equal(t, err, nil)
Equal(t, v.Slice, []customString{"customv1", "customv2"})
} |
What is the reason to feed custom decoder with slice of strings?
Decoder function is called at
https://github.com/go-playground/form/blob/master/decoder.go#L191
If target field is a slice,
setFieldByType
is called in a loop withidx
iterating through[]string
. Internally custom decoder is called with that full[]string
and without any mention of currentidx
.How decoder function is expected to deal with multi-element input?
To me it seems correct code should be
but then I'm not sure why decoder function needs to accept a slice.
The text was updated successfully, but these errors were encountered: