Skip to content

Commit

Permalink
Update setViperStructDefaults to respect '{}' default tag for struct …
Browse files Browse the repository at this point in the history
…fields (#2633)

Signed-off-by: Vyom-Yadav <[email protected]>
  • Loading branch information
Vyom-Yadav authored Mar 25, 2024
1 parent f856026 commit c571835
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 7 additions & 4 deletions internal/config/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,17 @@ func setViperStructDefaults(v *viper.Viper, prefix string, s any) {
}
valueName := strings.ToLower(prefix + field.Tag.Get("mapstructure"))

// Extract a default value the `default` struct tag
// we don't support all value types yet, but we can add them as needed
value := field.Tag.Get("default")

if field.Type.Kind() == reflect.Struct {
setViperStructDefaults(v, valueName+".", reflect.Zero(field.Type).Interface())
if value != "{}" {
setViperStructDefaults(v, valueName+".", reflect.Zero(field.Type).Interface())
}
continue
}

// Extract a default value the `default` struct tag
// we don't support all value types yet, but we can add them as needed
value := field.Tag.Get("default")
defaultValue := reflect.Zero(field.Type).Interface()
var err error // We handle errors at the end of the switch
fieldType := field.Type.Kind()
Expand Down
2 changes: 2 additions & 0 deletions internal/config/server/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ func TestReadDefaultConfig(t *testing.T) {
cfg := serverconfig.DefaultConfigForTest()
require.Equal(t, "debug", cfg.LoggingConfig.Level)
require.Equal(t, "minder", cfg.Database.Name)
require.Equal(t, int64(0), cfg.Events.Aggregator.LockInterval)
require.Equal(t, "", cfg.Events.SQLPubSub.Connection.Name)
require.Equal(t, "./.ssh/token_key_passphrase", cfg.Auth.TokenKey)
}

Expand Down

0 comments on commit c571835

Please sign in to comment.