mirror of
https://github.com/distribution/distribution.git
synced 2025-06-29 16:57:44 +00:00
Inline env variables if the underlying struct is YAML inlined
Signed-off-by: evanebb <git@evanus.nl>
This commit is contained in:
parent
97495e5397
commit
91e3bfec92
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -205,6 +206,25 @@ func (p *Parser) overwriteStruct(v reflect.Value, fullpath string, path []string
|
|||||||
byUpperCase := make(map[string]int)
|
byUpperCase := make(map[string]int)
|
||||||
for i := 0; i < v.NumField(); i++ {
|
for i := 0; i < v.NumField(); i++ {
|
||||||
sf := v.Type().Field(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)
|
upper := strings.ToUpper(sf.Name)
|
||||||
if _, present := byUpperCase[upper]; present {
|
if _, present := byUpperCase[upper]; present {
|
||||||
panic(fmt.Sprintf("field name collision in configuration object: %s", sf.Name))
|
panic(fmt.Sprintf("field name collision in configuration object: %s", sf.Name))
|
||||||
|
@ -11,12 +11,18 @@ type localConfiguration struct {
|
|||||||
Version Version `yaml:"version"`
|
Version Version `yaml:"version"`
|
||||||
Log *Log `yaml:"log"`
|
Log *Log `yaml:"log"`
|
||||||
Notifications []Notif `yaml:"notifications,omitempty"`
|
Notifications []Notif `yaml:"notifications,omitempty"`
|
||||||
|
Inlined Inlined `yaml:",inline"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Notif struct {
|
type Notif struct {
|
||||||
Name string `yaml:"name"`
|
Name string `yaml:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Inlined struct {
|
||||||
|
FirstValue string `yaml:"firstValue"`
|
||||||
|
SecondValue string `yaml:"secondValue"`
|
||||||
|
}
|
||||||
|
|
||||||
var expectedConfig = localConfiguration{
|
var expectedConfig = localConfiguration{
|
||||||
Version: "0.1",
|
Version: "0.1",
|
||||||
Log: &Log{
|
Log: &Log{
|
||||||
@ -89,3 +95,41 @@ func TestParseOverwriteUnininitializedPoiner(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, expectedConfig, config)
|
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)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user