-
Notifications
You must be signed in to change notification settings - Fork 681
Implement JSON like unmarshaling interface #115
Comments
I would also like mapstructure to support |
Looking at https://github.com/mitchellh/mapstructure/blob/master/mapstructure_test.go#L530 and the Update I have the feeling this cannot be done through the existing hooks:
This works up to the point where Furthermore |
If you are in a real pinch and are starting out with a map, you can marshal that back to JSON bytes then get it into your known structure. But if you're ingesting JSON, which has a field mapped to arbitrary JSON, you can specify that as a
|
For reference, here's an example of a custom unmarshaler interface via the new type Unmarshaler interface {
CustomUnmarshalMethod(interface{}) error
}
func UnmarshalerHook() mapstructure.DecodeHookFunc {
return func(from reflect.Value, to reflect.Value) (interface{}, error) {
// If the destination implements the unmarshaling interface
u, ok := to.Interface().(Unmarshaler)
if !ok {
return from.Interface(), nil
}
// If it is nil and a pointer, create and assign the target value first
if to.IsNil() && to.Type().Kind() == reflect.Ptr {
to.Set(reflect.New(to.Type().Elem()))
u = to.Interface().(Unmarshaler)
}
// Call the custom unmarshaling method
if err := u.CustomUnmarshalMethod(from.Interface()); err != nil {
return to.Interface(), err
}
return to.Interface(), nil
}
} |
Or use stdlib TextUnmarshaler interface func UnmarshalerHook() mapstructure.DecodeHookFunc {
return func(from reflect.Value, to reflect.Value) (interface{}, error) {
if to.CanAddr() {
to = to.Addr()
}
// If the destination implements the unmarshaling interface
u, ok := to.Interface().(encoding.TextUnmarshaler)
if !ok {
return from.Interface(), nil
}
// If it is nil and a pointer, create and assign the target value first
if to.IsNil() && to.Type().Kind() == reflect.Ptr {
to.Set(reflect.New(to.Type().Elem()))
u = to.Interface().(encoding.TextUnmarshaler)
}
var text []byte
switch v := from.Interface().(type) {
case string:
text = []byte(v)
case []byte:
text = v
default:
return v, nil
}
if err := u.UnmarshalText(text); err != nil {
return to.Interface(), err
}
return to.Interface(), nil
}
} |
I have tripped over mapstructure one too many times so this commit removes it. When/if the extra round-trip through JSON adds measurable overhead to Zed query times, we can revisit this and hopefully mapstructure will be more mature. For now, I wanted to add order.Which to proc.Sort and similar but mapstructure cannot handle custom JSON unmarshalers so this caused unpack to fail for apparently no reason. See mitchellh/mapstructure#115
I have tripped over mapstructure one too many times so this commit removes it. When/if the extra round-trip through JSON adds measurable overhead to Zed query times, we can revisit this and hopefully mapstructure will be more mature. For now, I wanted to add order.Which to proc.Sort and similar but mapstructure cannot handle custom JSON unmarshalers so this caused unpack to fail for apparently no reason. See mitchellh/mapstructure#115
I have tripped over mapstructure one too many times so this commit removes it. When/if the extra round-trip through JSON adds measurable overhead to Zed query times, we can revisit this and hopefully mapstructure will be more mature. For now, I wanted to add order.Which to proc.Sort and similar but mapstructure cannot handle custom JSON unmarshalers so this caused unpack to fail for apparently no reason. See mitchellh/mapstructure#115
This is an auto-generated commit with a Zed dependency update. The Zed PR brimdata/super#2703, authored by @mccanne, has been merged. remove mapstructure I have tripped over mapstructure one too many times so this commit removes it. When/if the extra round-trip through JSON adds measurable overhead to Zed query times, we can revisit this and hopefully mapstructure will be more mature. For now, I wanted to add order.Which to proc.Sort and similar but mapstructure cannot handle custom JSON unmarshalers so this caused unpack to fail for apparently no reason. See mitchellh/mapstructure#115
This is an auto-generated commit with a Zed dependency update. The Zed PR brimdata/super#2703, authored by @mccanne, has been merged. remove mapstructure I have tripped over mapstructure one too many times so this commit removes it. When/if the extra round-trip through JSON adds measurable overhead to Zed query times, we can revisit this and hopefully mapstructure will be more mature. For now, I wanted to add order.Which to proc.Sort and similar but mapstructure cannot handle custom JSON unmarshalers so this caused unpack to fail for apparently no reason. See mitchellh/mapstructure#115
Is anyone still interested in this? |
Hey folks ;)
I think it would be nice if mapstructure would support an unmarshaling interface like for the json module, so custom marshalers can be defined on a per-type basis easily:
Specifically I tried to do custom marshaling inside viper, which internally uses mapstructure to unmarshal from its internal map-representation of config-key-values to my config-struct. But there are no possibilities there to "override" marshaling behaviour for my types and I have to write quite some boilerplate to circumvent that^^
The text was updated successfully, but these errors were encountered: