mapstructure
mapstructure copied to clipboard
DecodeHooks not called when decoding struct to map
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.