mapstructure icon indicating copy to clipboard operation
mapstructure copied to clipboard

DecodeHooks not called when decoding struct to map

Open tobiasbeck opened this issue 3 years ago • 0 comments

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.

tobiasbeck avatar Apr 30 '21 16:04 tobiasbeck