From a6e52d9d3d6b009530c94381797f33debd096738 Mon Sep 17 00:00:00 2001 From: Torben Nehmer Date: Tue, 24 Aug 2021 17:03:59 +0200 Subject: [PATCH] Fix: viper.UnmarshalKey ignores defaults This seems to be a bug in viper, as soon as we're working with subkeys, things fall apart --- service/config.go | 8 +++++++- webapi/config.go | 6 +++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/service/config.go b/service/config.go index 7662e9e..7c23ca1 100644 --- a/service/config.go +++ b/service/config.go @@ -34,7 +34,13 @@ func SetConfigDefaults() { } func LoadConfig() { - viper.UnmarshalKey("Service", &C) + // Does not work with partially overrides, only GetXXX does resolve the subkeys correctly + // https://github.com/spf13/viper/issues/798 + // https://github.com/spf13/viper/issues/309 + // viper.UnmarshalKey("Service", &C) + C.DNS.Server = viper.GetString("Service.DNS.Server") + C.DNS.DefaultTTL = viper.GetUint32("Service.DNS.DefaultTTL") + C.Users.ConfigDir = viper.GetString("Service.Users.ConfigDir") } func (obj *config) PrettyPrint() string { diff --git a/webapi/config.go b/webapi/config.go index f5d5871..93cd1a8 100644 --- a/webapi/config.go +++ b/webapi/config.go @@ -22,7 +22,11 @@ func SetConfigDefaults() { } func LoadConfig() { - viper.UnmarshalKey("WebAPI", &C) + // Does not work with partially overrides, only GetXXX does resolve the subkeys correctly + // https://github.com/spf13/viper/issues/798 + // https://github.com/spf13/viper/issues/309 + // viper.UnmarshalKey("WebAPI", &C) + C.ListenAddress = viper.GetString("WebAPI.ListenAddress") } func (obj *config) PrettyPrint() string {