Inline env variables if the underlying struct is YAML inlined

Signed-off-by: evanebb <git@evanus.nl>
This commit is contained in:
evanebb 2025-05-20 16:46:17 +02:00
parent 97495e5397
commit 91e3bfec92
No known key found for this signature in database
2 changed files with 64 additions and 0 deletions

View File

@ -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))

View File

@ -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)
}