mirror of
https://github.com/distribution/distribution.git
synced 2025-09-09 02:40:00 +00:00
Merge fa24405ad9
into da404778ed
This commit is contained in:
@@ -456,6 +456,21 @@ func (suite *ConfigSuite) TestParseEnvMany() {
|
||||
suite.Require().NoError(err)
|
||||
}
|
||||
|
||||
// TestParseEnvInlinedStruct tests whether environment variables are properly matched to fields in inlined structs.
|
||||
func (suite *ConfigSuite) TestParseEnvInlinedStruct() {
|
||||
suite.expectedConfig.Redis.Options.Username = "bob"
|
||||
suite.expectedConfig.Redis.Options.Password = "password123"
|
||||
|
||||
// Test without inlined struct name in the env variable name
|
||||
suite.T().Setenv("REGISTRY_REDIS_USERNAME", "bob")
|
||||
// Test with the inlined struct name in the env variable name, for backward compatibility
|
||||
suite.T().Setenv("REGISTRY_REDIS_OPTIONS_PASSWORD", "password123")
|
||||
|
||||
config, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
||||
suite.Require().NoError(err)
|
||||
suite.Require().Equal(suite.expectedConfig, config)
|
||||
}
|
||||
|
||||
func checkStructs(tt *testing.T, t reflect.Type, structsChecked map[string]struct{}) {
|
||||
tt.Helper()
|
||||
|
||||
|
@@ -205,6 +205,21 @@ 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"`
|
||||
_, yamlOpts, _ := strings.Cut(sf.Tag.Get("yaml"), ",")
|
||||
if yamlOpts == "inline" && 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 := range inlined.NumField() {
|
||||
if strings.EqualFold(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))
|
||||
|
@@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user