mirror of
https://github.com/gin-gonic/gin.git
synced 2026-01-16 04:42:49 +08:00
Merge 513a86e17fa3ffed325c7f4f41fa3a919ce32afd into ecb3f7b5e2f3915bf1db240ed5eee572f8dbea36
This commit is contained in:
commit
a32d73d2f5
@ -186,7 +186,10 @@ type BindUnmarshaler interface {
|
||||
func trySetCustom(val string, value reflect.Value) (isSet bool, err error) {
|
||||
switch v := value.Addr().Interface().(type) {
|
||||
case BindUnmarshaler:
|
||||
return true, v.UnmarshalParam(val)
|
||||
if err := v.UnmarshalParam(val); err != nil {
|
||||
return true, fmt.Errorf("invalid value %q: %w", val, err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
@ -245,7 +248,10 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
|
||||
}
|
||||
|
||||
if ok, err = trySetCustom(vs[0], value); ok {
|
||||
return ok, err
|
||||
if err != nil {
|
||||
return true, fmt.Errorf("field %q: %w", field.Name, err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
if vs, err = trySplit(vs, field); err != nil {
|
||||
@ -268,7 +274,10 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
|
||||
}
|
||||
|
||||
if ok, err = trySetCustom(vs[0], value); ok {
|
||||
return ok, err
|
||||
if err != nil {
|
||||
return true, fmt.Errorf("field %q: %w", field.Name, err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
if vs, err = trySplit(vs, field); err != nil {
|
||||
@ -293,7 +302,10 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
|
||||
}
|
||||
}
|
||||
if ok, err := trySetCustom(val, value); ok {
|
||||
return ok, err
|
||||
if err != nil {
|
||||
return true, fmt.Errorf("field %q: %w", field.Name, err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
return true, setWithProperType(val, value, field)
|
||||
}
|
||||
|
||||
@ -715,3 +715,22 @@ func TestMappingEmptyValues(t *testing.T) {
|
||||
assert.Equal(t, []int{1, 2, 3}, s.SliceCsv)
|
||||
})
|
||||
}
|
||||
|
||||
type testCustom struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
func (t *testCustom) UnmarshalParam(param string) error {
|
||||
t.Value = "prefix_" + param
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestTrySetCustomIntegration(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)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user