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 }}
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
I've just had the requirement, to provide specific unmarshaling of some structures without modifying the unmarshaler configuration. I already have WeaklyTypedInput and DecodeHook/ComposeDecodeHookFunc.
I'd like to propose implementing an UnmarshallerHookFuncDecodeHookFunc. UnmarshallerHookFunc would be called whenever the target implements mapstructure.Unmarshaler:
type Unmarshaler interface {
UnmarshalMapStructure(f reflect.Type, data interface{}) error
}
Here's an example usage case. ValueDefinition can be supplied as simple string or as map and both will be decoded into an ValueDefinition.
type ValueDefinition struct {
Value string
Scale float64
}
var _ util.MapStructureUnmarshaler = (*ValueDefinition)(nil)
func (v *ValueDefinition) UnmarshalMapStructure(f reflect.Type, data interface{}) error {
switch f.Kind() {
case reflect.String:
*v = ValueDefinition{Value: data.(string)}
return nil
case reflect.Map:
var cc struct {
Value string
Scale float64
}
err := util.DecodeOther(data, &cc)
if err == nil {
*v = ValueDefinition{
Value: cc.Value,
Scale: cc.Scale,
}
}
return err
default:
return fmt.Errorf("invalid kind: %s", f.Kind())
}
}
If that sounds interesting I'd be happy to provide a PR.
The text was updated successfully, but these errors were encountered:
I've just had the requirement, to provide specific unmarshaling of some structures without modifying the unmarshaler configuration. I already have
WeaklyTypedInput
andDecodeHook
/ComposeDecodeHookFunc
.I'd like to propose implementing an
UnmarshallerHookFunc
DecodeHookFunc
.UnmarshallerHookFunc
would be called whenever the target implementsmapstructure.Unmarshaler
:Here's an example usage case.
ValueDefinition
can be supplied as simple string or as map and both will be decoded into anValueDefinition
.If that sounds interesting I'd be happy to provide a PR.
The text was updated successfully, but these errors were encountered: