Skip to content

Support customising mapstructure.DecoderConfig for Unmarshal#521

Merged
bep merged 1 commit intospf13:masterfrom
krak3n:master
Aug 3, 2018
Merged

Support customising mapstructure.DecoderConfig for Unmarshal#521
bep merged 1 commit intospf13:masterfrom
krak3n:master

Conversation

@krak3n
Copy link
Copy Markdown
Contributor

@krak3n krak3n commented Jun 28, 2018

Hi

I recently ran into an issue when using viper.Unmarshal in-conjunction with Environment variables. I wished to pass a json object 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:

export MY_CONFIG_VAL={\"foo\":\"bar\",\"fizz\":\"buzz\"}

Whilst viper.GetStringMapString would work perfectly, Unmarshal would fail with:

* 'my_config_val' expected a map, got 'string'

I did some digging and saw this was failing in mapstructure. I saw it supported custom DecodeHook functions so I could write my own function to parse the value correctly, however viper does not support the ability to customise the mapstructure.DecoderConfig.

So this PR adds the following (it's worth noting this is 100% backwards compatible thanks to variadic arguments):

  • 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

I look forward to your review and feedback 😄

p.s and thank you for this wonderful library.

Edits: spelling

* 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
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Jun 28, 2018

CLA assistant check
All committers have signed the CLA.

@krak3n
Copy link
Copy Markdown
Contributor Author

krak3n commented Jun 28, 2018

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 Unmarshal with the function above as an Option:

type config struct {
	Foo string `viper:"foo"`
}
var C config
viper.Unmarshal(&C, ViperStructTag())

Edit: Referenced wrong PR

@krak3n
Copy link
Copy Markdown
Contributor Author

krak3n commented Jul 13, 2018

@spf13 @bep any feedback on this one?

@krak3n
Copy link
Copy Markdown
Contributor Author

krak3n commented Aug 3, 2018

😭

@bep bep merged commit 907c19d into spf13:master Aug 3, 2018
@krak3n
Copy link
Copy Markdown
Contributor Author

krak3n commented Aug 3, 2018

yey! thank you @bep

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants