In times when you’re trying to unmarshal a config file using viper
, check whether your config file has nested yaml structures.
service:
base_url: https://api.yourwebsite.com/v1
api_key: some_api_key
providers:
payment:
dwolla:
base_url: https://sandbox.api.dwolla.com
api_key: some_api_key
As shown in the example above, there are multiple layers of structs under providers
(i.e. payment contains dwolla). The different with service
is that its fields are set only in one level.
If your config file is a single level struct
, then there won’t be any problems using the yaml
Golang structural flag. However, if your config file uses multiple-level structs as in the case of providers
, then you need to use mapstructure
instead of yaml
when defining your structs.
See this stack overflow resource for more information.
I’ll build out a small reproducible repository to demonstrate this later.