-
Notifications
You must be signed in to change notification settings - Fork 677
Allow types to determine how to decode themselves #294
base: main
Are you sure you want to change the base?
Conversation
I'd like to see this merged, though to offer one comment, the interface function - Decode() might be a bit too generic within the go-ecosystem? |
+1 for allowing objects to decode themselves. |
Similar to the `UnarshalX` family of functions seen throughout `encoding` and `encoding/json`, include the name of what is being decoded into the function.
@wrouesnel I've applied your suggestion although I did swap the order. Like any of the standard-libraries |
@mitchellh any interest in this being merged? |
Would it be possible to make this work in both directions, so that e.g. decoding a There would be some ambiguity, I guess, since you could conceivably call |
if outVal.CanAddr() { | ||
v := outVal.Addr() | ||
if u, ok := v.Interface().(decoder); ok { | ||
return u.DecodeMapstructure(input) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might make more sense to put this after the DecodeHook
, so that any custom preprocessing can still happen.
Bumping this b/c this would probably simplify my code a lot to use stdlib marshal interfaces and only 1 hook for it all. |
@pcfreak30 @mitchellh any updates on this one ? |
When the destination of a decode implements the custom decoder interface, use said interface. Custom decoders take precedence over the default decoding rules.
This should resolve #115
Renamed the master -> main which closed the pull-request, sorry 😅