mirror of
https://github.com/labstack/echo.git
synced 2025-07-17 01:43:02 +02:00
Fix binding of untagged struct fields (#812)
* Add failing test A BindUnmarshaler struct with no tag is not decoded properly. * Fix binding of untagged structs
This commit is contained in:
committed by
Vishal Rana
parent
80d5c96212
commit
ed7353cf60
18
bind.go
18
bind.go
@ -95,7 +95,7 @@ func (b *DefaultBinder) bindData(ptr interface{}, data map[string][]string, tag
|
||||
if inputFieldName == "" {
|
||||
inputFieldName = typeField.Name
|
||||
// If tag is nil, we inspect if the field is a struct.
|
||||
if structFieldKind == reflect.Struct {
|
||||
if _, ok := bindUnmarshaler(structField); !ok && structFieldKind == reflect.Struct {
|
||||
err := b.bindData(structField.Addr().Interface(), data, tag)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -185,16 +185,24 @@ func unmarshalField(valueKind reflect.Kind, val string, field reflect.Value) (bo
|
||||
}
|
||||
}
|
||||
|
||||
func unmarshalFieldNonPtr(value string, field reflect.Value) (bool, error) {
|
||||
// bindUnmarshaler attempts to unmarshal a reflect.Value into a BindUnmarshaler
|
||||
func bindUnmarshaler(field reflect.Value) (BindUnmarshaler, bool) {
|
||||
ptr := reflect.New(field.Type())
|
||||
if ptr.CanInterface() {
|
||||
iface := ptr.Interface()
|
||||
if unmarshaler, ok := iface.(BindUnmarshaler); ok {
|
||||
err := unmarshaler.UnmarshalParam(value)
|
||||
field.Set(ptr.Elem())
|
||||
return true, err
|
||||
return unmarshaler, ok
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func unmarshalFieldNonPtr(value string, field reflect.Value) (bool, error) {
|
||||
if unmarshaler, ok := bindUnmarshaler(field); ok {
|
||||
err := unmarshaler.UnmarshalParam(value)
|
||||
field.Set(reflect.ValueOf(unmarshaler).Elem())
|
||||
return true, err
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user