Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

DecodeHooks not called when decoding struct to map #246

Open
AmberByte opened this issue Apr 30, 2021 · 0 comments
Open

DecodeHooks not called when decoding struct to map #246

AmberByte opened this issue Apr 30, 2021 · 0 comments

Comments

@AmberByte
Copy link

Hello,
When decoding a struct to a map custom DecodeHooks are not called for non struct fields.
This originates from line 975 in mapstructure.go where fields valued are just copied to the map.
In my eyes it would make sense to also call hooks when decoding a struct field to a map as well so it is possible to overwrite what is written to the map.

E.g. I've got the following model definition inside my application:

type Mail struct {
        // ...
	Mission                      primitive.ObjectID `bson:"mission,omitempty" mapstructure:"mission,omitempty"`
        // ...
}

ObjectID is a custom type from mongo-go-driver. It is an array of length 12. Its zero value is also an array of 12. because of that omitempty flag does not work. However my custom decoder could take care of this and decide to decode the ObjectID to nil if it corresponds to its zero value.
Input type and output type given to the hook could be the same.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant