Support customising mapstructure.DecoderConfig for Unmarshal#521
Merged
bep merged 1 commit intospf13:masterfrom Aug 3, 2018
Merged
Support customising mapstructure.DecoderConfig for Unmarshal#521bep merged 1 commit intospf13:masterfrom
bep merged 1 commit intospf13:masterfrom
Conversation
* Added a new `DecoderConfigOption` type allowing the user to write custom functions that can override the default mapstructure.DecoderConfig settings * Added a new `DecodeHook` function which returns a `DecoderConfigOption`. This allows the user to easily set their own Decode hooks when Unmarshaling * Updated Unmarshal, UnmarshalKey and defaultDecoderConfig to support variadic trailing `DecoderConfigOption` functions to allow for customisation of the default mapstructure.DecoderConfig * Added a test case with example usage
Contributor
Author
|
Also I believe this may also be a solution to #449 . Since the user could instead customise the default existing configuration instead, for example: func ViperStructTag() viper.DecoderConfigOption {
return func(c *mapstructure.DecoderConfig) {
c.TagName = "viper"
}
}And then calling type config struct {
Foo string `viper:"foo"`
}
var C config
viper.Unmarshal(&C, ViperStructTag())Edit: Referenced wrong PR |
Contributor
Author
Contributor
Author
|
😭 |
Contributor
Author
|
yey! thank you @bep |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi
I recently ran into an issue when using
viper.Unmarshalin-conjunction with Environment variables. I wished to pass ajsonobject as a value to an Environment variable for user names and passwords that I didn't want to write to a config file, for example:Whilst
viper.GetStringMapStringwould work perfectly,Unmarshalwould fail with:I did some digging and saw this was failing in
mapstructure. I saw it supported customDecodeHookfunctions so I could write my own function to parse the value correctly, howeverviperdoes not support the ability to customise themapstructure.DecoderConfig.So this PR adds the following (it's worth noting this is 100% backwards compatible thanks to variadic arguments):
Added a new
DecoderConfigOptiontype allowing the user to write customfunctions that can override the default mapstructure.DecoderConfig
settings
Added a new
DecodeHookfunction which returnsa
DecoderConfigOption. This allows the user to easily set their ownDecode hooks when Unmarshaling
Updated Unmarshal, UnmarshalKey and defaultDecoderConfig to support variadic
trailing
DecoderConfigOptionfunctions to allow for customisation ofthe default mapstructure.DecoderConfig
Added a test case with example usage
I look forward to your review and feedback 😄
p.s and thank you for this wonderful library.
Edits: spelling