diff --git a/configuration/parser.go b/configuration/parser.go index 1a9201986..562067683 100644 --- a/configuration/parser.go +++ b/configuration/parser.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "reflect" + "slices" "sort" "strconv" "strings" @@ -205,6 +206,25 @@ func (p *Parser) overwriteStruct(v reflect.Value, fullpath string, path []string byUpperCase := make(map[string]int) for i := 0; i < v.NumField(); i++ { sf := v.Type().Field(i) + + // For fields inlined in the YAML configuration file, the environment variables also need to be inlined + // Example struct tag for inlined field: `yaml:",inline"` + yamlTag := sf.Tag.Get("yaml") + split := strings.Split(yamlTag, ",") + // Skip the first entry, which is the field name + isInlined := slices.Contains(split[1:], "inline") + + if isInlined && sf.Type.Kind() == reflect.Struct { + // Inlined struct, check whether the env variable corresponds to a field inside this struct + // Maps could also be inlined, but since we don't need it right now it is not supported + inlined := v.Field(i) + for j := 0; j < inlined.NumField(); j++ { + if strings.ToUpper(inlined.Type().Field(j).Name) == path[0] { + return p.overwriteFields(inlined, fullpath, path, payload) + } + } + } + upper := strings.ToUpper(sf.Name) if _, present := byUpperCase[upper]; present { panic(fmt.Sprintf("field name collision in configuration object: %s", sf.Name)) diff --git a/configuration/parser_test.go b/configuration/parser_test.go index 636f3291c..637a20075 100644 --- a/configuration/parser_test.go +++ b/configuration/parser_test.go @@ -11,12 +11,18 @@ type localConfiguration struct { Version Version `yaml:"version"` Log *Log `yaml:"log"` Notifications []Notif `yaml:"notifications,omitempty"` + Inlined Inlined `yaml:",inline"` } type Notif struct { Name string `yaml:"name"` } +type Inlined struct { + FirstValue string `yaml:"firstValue"` + SecondValue string `yaml:"secondValue"` +} + var expectedConfig = localConfiguration{ Version: "0.1", Log: &Log{ @@ -89,3 +95,41 @@ func TestParseOverwriteUnininitializedPoiner(t *testing.T) { require.NoError(t, err) require.Equal(t, expectedConfig, config) } + +const testConfig3 = `version: "0.1" +log: + formatter: "text"` + +func TestParseInlinedStruct(t *testing.T) { + config := localConfiguration{} + + expected := localConfiguration{ + Version: "0.1", + Log: &Log{ + Formatter: "text", + }, + Inlined: Inlined{ + FirstValue: "foo", + SecondValue: "bar", + }, + } + + // Test without inlined struct name in the env variable name + t.Setenv("REGISTRY_FIRSTVALUE", "foo") + // Test with the inlined struct name in the env variable name, for backward compatibility + t.Setenv("REGISTRY_INLINED_SECONDVALUE", "bar") + + p := NewParser("registry", []VersionedParseInfo{ + { + Version: "0.1", + ParseAs: reflect.TypeOf(config), + ConversionFunc: func(c interface{}) (interface{}, error) { + return c, nil + }, + }, + }) + + err := p.Parse([]byte(testConfig3), &config) + require.NoError(t, err) + require.Equal(t, expected, config) +}