modifying setArray and setSlice

This commit is contained in:
Pratham Gadkari 2025-11-30 16:10:58 +05:30
parent 6b6ac46ae3
commit 822cf17bf9
2 changed files with 38 additions and 0 deletions

View File

@ -468,6 +468,12 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val
func setArray(vals []string, value reflect.Value, field reflect.StructField) error {
for i, s := range vals {
if ok, err := trySetCustom(s, value.Index(i)); ok {
if err != nil {
return fmt.Errorf("field %q: %w", field.Name, err)
}
continue
}
err := setWithProperType(s, value.Index(i), field)
if err != nil {
return err

View File

@ -760,3 +760,35 @@ func TestTrySetCustomNotApplicable(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, 42, s.N)
}
func TestTrySetCustomIntegrationSuccess(t *testing.T) {
var s struct {
F testCustom `form:"f"`
}
err := mappingByPtr(&s, formSource{"f": {"hello"}}, "form")
require.NoError(t, err)
assert.Equal(t, "prefix_hello", s.F.Value)
}
func TestTrySetCustomSlice(t *testing.T) {
var s struct {
F []testCustom `form:"f"`
}
err := mappingByPtr(&s, formSource{"f": {"one", "two"}}, "form")
require.NoError(t, err)
assert.Equal(t, "prefix_one", s.F[0].Value)
assert.Equal(t, "prefix_two", s.F[1].Value)
}
func TestTrySetCustomArray(t *testing.T) {
var s struct {
F [2]testCustom `form:"f"`
}
err := mappingByPtr(&s, formSource{"f": {"hello", "world"}}, "form")
require.NoError(t, err)
assert.Equal(t, "prefix_hello", s.F[0].Value)
assert.Equal(t, "prefix_world", s.F[1].Value)
}