Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mx 16067 overridable configs array support #6599

Open
wants to merge 5 commits into
base: rc/v1.7.next1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions common/reflectcommon/structFieldsUpdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func trySetTheNewValue(value *reflect.Value, newValue interface{}) error {
case reflect.Map:
mapValue := reflect.ValueOf(newValue)

return tryUpdateMapValue(value, mapValue)
return trySetMapValue(value, mapValue)
default:
return fmt.Errorf("unsupported type <%s> when trying to set the value '%v' of type <%s>", valueKind, newValue, reflect.TypeOf(newValue))
}
Expand All @@ -141,7 +141,7 @@ func trySetSliceValue(value *reflect.Value, newValue interface{}) error {
item := sliceVal.Index(i)
newItem := reflect.New(value.Type().Elem()).Elem()

err := trySetStructValue(&newItem, item)
err := trySetTheNewValue(&newItem, item.Interface())
if err != nil {
return err
}
Expand All @@ -167,7 +167,7 @@ func trySetStructValue(value *reflect.Value, newValue reflect.Value) error {
}
}

func tryUpdateMapValue(value *reflect.Value, newValue reflect.Value) error {
func trySetMapValue(value *reflect.Value, newValue reflect.Value) error {
if value.IsNil() {
value.Set(reflect.MakeMap(value.Type()))
}
Expand Down
73 changes: 73 additions & 0 deletions common/reflectcommon/structFieldsUpdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,79 @@ func TestAdaptStructureValueBasedOnPath(t *testing.T) {
require.Equal(t, "unsupported type <int> when trying to add value in type <map>", err.Error())
})

t.Run("should work and override string array", func(t *testing.T) {
t.Parallel()

testConfig, err := loadTestConfig("../../testscommon/toml/config.toml")
require.NoError(t, err)

expectedArray := []string{"x", "y", "z"}

err = AdaptStructureValueBasedOnPath(testConfig, "TestArray.Strings", expectedArray)
require.NoError(t, err)
require.Equal(t, expectedArray, testConfig.TestArray.Strings)
})

t.Run("should work and override int array", func(t *testing.T) {
t.Parallel()

testConfig, err := loadTestConfig("../../testscommon/toml/config.toml")
require.NoError(t, err)

expectedArray := []int{10, 20, 30}

err = AdaptStructureValueBasedOnPath(testConfig, "TestArray.Ints", expectedArray)
require.NoError(t, err)
require.Equal(t, expectedArray, testConfig.TestArray.Ints)
})

t.Run("should work and override string array from toml", func(t *testing.T) {
t.Parallel()

testConfig, err := loadTestConfig("../../testscommon/toml/config.toml")
require.NoError(t, err)

overrideConfig, err := loadOverrideConfig("../../testscommon/toml/overwrite.toml")
require.NoError(t, err)

err = AdaptStructureValueBasedOnPath(testConfig, overrideConfig.OverridableConfigTomlValues[38].Path, overrideConfig.OverridableConfigTomlValues[38].Value)
require.NoError(t, err)
expectedArray := []string{"x", "y", "z"}
require.Equal(t, expectedArray, testConfig.TestArray.Strings)
})

t.Run("should work and override int array from toml", func(t *testing.T) {
t.Parallel()

testConfig, err := loadTestConfig("../../testscommon/toml/config.toml")
require.NoError(t, err)

overrideConfig, err := loadOverrideConfig("../../testscommon/toml/overwrite.toml")
require.NoError(t, err)

err = AdaptStructureValueBasedOnPath(testConfig, overrideConfig.OverridableConfigTomlValues[39].Path, overrideConfig.OverridableConfigTomlValues[39].Value)
require.NoError(t, err)
expectedArray := []int{10, 20, 30}
require.Equal(t, expectedArray, testConfig.TestArray.Ints)
})

t.Run("should work and override struct of arrays from toml", func(t *testing.T) {
t.Parallel()

testConfig, err := loadTestConfig("../../testscommon/toml/config.toml")
require.NoError(t, err)

overrideConfig, err := loadOverrideConfig("../../testscommon/toml/overwrite.toml")
require.NoError(t, err)
expectedStringsArray := []string{"x", "y", "z"}
expectedIntsArray := []int{10, 20, 30}

err = AdaptStructureValueBasedOnPath(testConfig, overrideConfig.OverridableConfigTomlValues[40].Path, overrideConfig.OverridableConfigTomlValues[40].Value)
require.NoError(t, err)
require.Equal(t, expectedStringsArray, testConfig.TestArray.Strings)
require.Equal(t, expectedIntsArray, testConfig.TestArray.Ints)
})

}

func loadTestConfig(filepath string) (*toml.Config, error) {
Expand Down
7 changes: 7 additions & 0 deletions testscommon/toml/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Config struct {
TestConfigNestedStruct
TestMap
TestInterface
TestArray
}

// TestConfigI8 will hold an int8 value for testing
Expand Down Expand Up @@ -180,3 +181,9 @@ type MapValues struct {
type TestInterface struct {
Value interface{}
}

// TestArray will hold an array of strings and integers
type TestArray struct {
Strings []string
Ints []int
}
4 changes: 4 additions & 0 deletions testscommon/toml/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@
[Map]
[Map.Key1]
Number = 999

[TestArray]
Strings = ["a", "b", "c"]
Ints = [0, 1, 2]
79 changes: 41 additions & 38 deletions testscommon/toml/overwrite.toml
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
OverridableConfigTomlValues = [
{ File = "config.toml", Path = "TestConfigI8.Int8.Number", Value = 127 },
{ File = "config.toml", Path = "TestConfigI8.Int8.Number", Value = 128 },
{ File = "config.toml", Path = "TestConfigI8.Int8.Number", Value = -128 },
{ File = "config.toml", Path = "TestConfigI8.Int8.Number", Value = -129 },
{ File = "config.toml", Path = "TestConfigI16.Int16.Number", Value = 32767 },
{ File = "config.toml", Path = "TestConfigI16.Int16.Number", Value = 32768 },
{ File = "config.toml", Path = "TestConfigI16.Int16.Number", Value = -32768 },
{ File = "config.toml", Path = "TestConfigI16.Int16.Number", Value = -32769 },
{ File = "config.toml", Path = "TestConfigI32.Int32.Number", Value = 2147483647 },
{ File = "config.toml", Path = "TestConfigI32.Int32.Number", Value = 2147483648 },
{ File = "config.toml", Path = "TestConfigI32.Int32.Number", Value = -2147483648 },
{ File = "config.toml", Path = "TestConfigI32.Int32.Number", Value = -2147483649 },
{ File = "config.toml", Path = "TestConfigI64.Int64.Number", Value = 9223372036854775807 },
{ File = "config.toml", Path = "TestConfigI64.Int64.Number", Value = -9223372036854775808 },
{ File = "config.toml", Path = "TestConfigU8.Uint8.Number", Value = 255 },
{ File = "config.toml", Path = "TestConfigU8.Uint8.Number", Value = 256 },
{ File = "config.toml", Path = "TestConfigU8.Uint8.Number", Value = -256 },
{ File = "config.toml", Path = "TestConfigU16.Uint16.Number", Value = 65535 },
{ File = "config.toml", Path = "TestConfigU16.Uint16.Number", Value = 65536 },
{ File = "config.toml", Path = "TestConfigU16.Uint16.Number", Value = -65536 },
{ File = "config.toml", Path = "TestConfigU32.Uint32.Number", Value = 4294967295 },
{ File = "config.toml", Path = "TestConfigU32.Uint32.Number", Value = 4294967296 },
{ File = "config.toml", Path = "TestConfigU32.Uint32.Number", Value = -4294967296 },
{ File = "config.toml", Path = "TestConfigU64.Uint64.Number", Value = 9223372036854775807 },
{ File = "config.toml", Path = "TestConfigU64.Uint64.Number", Value = -9223372036854775808 },
{ File = "config.toml", Path = "TestConfigF32.Float32.Number", Value = 3.4 },
{ File = "config.toml", Path = "TestConfigF32.Float32.Number", Value = 3.4e+39 },
{ File = "config.toml", Path = "TestConfigF32.Float32.Number", Value = -3.4 },
{ File = "config.toml", Path = "TestConfigF32.Float32.Number", Value = -3.4e+40 },
{ File = "config.toml", Path = "TestConfigF64.Float64.Number", Value = 1.7e+308 },
{ File = "config.toml", Path = "TestConfigF64.Float64.Number", Value = -1.7e+308 },
{ File = "config.toml", Path = "TestConfigStruct.ConfigStruct.Description", Value = { Number = 11 } },
{ File = "config.toml", Path = "TestConfigStruct.ConfigStruct.Description", Value = { Nr = 222 } },
{ File = "config.toml", Path = "TestConfigStruct.ConfigStruct.Description", Value = { Number = "11" } },
{ File = "config.toml", Path = "TestConfigNestedStruct.ConfigNestedStruct", Value = { Text = "Overwritten text", Message = { Public = false, MessageDescription = [{ Text = "Overwritten Text1" }] } } },
{ File = "config.toml", Path = "TestConfigNestedStruct.ConfigNestedStruct.Message.MessageDescription", Value = [{ Text = "Overwritten Text1" }, { Text = "Overwritten Text2" }] },
{ File = "config.toml", Path = "TestMap.Map", Value = { "Key1" = { Number = 10 }, "Key2" = { Number = 11 } } },
{ File = "config.toml", Path = "TestMap.Map", Value = { "Key2" = { Number = 2 }, "Key3" = { Number = 3 } } },
{ File = "config.toml", Path = "TestConfigI8.Int8.Number", Value = 127 },
{ File = "config.toml", Path = "TestConfigI8.Int8.Number", Value = 128 },
{ File = "config.toml", Path = "TestConfigI8.Int8.Number", Value = -128 },
{ File = "config.toml", Path = "TestConfigI8.Int8.Number", Value = -129 },
{ File = "config.toml", Path = "TestConfigI16.Int16.Number", Value = 32767 },
{ File = "config.toml", Path = "TestConfigI16.Int16.Number", Value = 32768 },
{ File = "config.toml", Path = "TestConfigI16.Int16.Number", Value = -32768 },
{ File = "config.toml", Path = "TestConfigI16.Int16.Number", Value = -32769 },
{ File = "config.toml", Path = "TestConfigI32.Int32.Number", Value = 2147483647 },
{ File = "config.toml", Path = "TestConfigI32.Int32.Number", Value = 2147483648 },
{ File = "config.toml", Path = "TestConfigI32.Int32.Number", Value = -2147483648 },
{ File = "config.toml", Path = "TestConfigI32.Int32.Number", Value = -2147483649 },
{ File = "config.toml", Path = "TestConfigI64.Int64.Number", Value = 9223372036854775807 },
{ File = "config.toml", Path = "TestConfigI64.Int64.Number", Value = -9223372036854775808 },
{ File = "config.toml", Path = "TestConfigU8.Uint8.Number", Value = 255 },
{ File = "config.toml", Path = "TestConfigU8.Uint8.Number", Value = 256 },
{ File = "config.toml", Path = "TestConfigU8.Uint8.Number", Value = -256 },
{ File = "config.toml", Path = "TestConfigU16.Uint16.Number", Value = 65535 },
{ File = "config.toml", Path = "TestConfigU16.Uint16.Number", Value = 65536 },
{ File = "config.toml", Path = "TestConfigU16.Uint16.Number", Value = -65536 },
{ File = "config.toml", Path = "TestConfigU32.Uint32.Number", Value = 4294967295 },
{ File = "config.toml", Path = "TestConfigU32.Uint32.Number", Value = 4294967296 },
{ File = "config.toml", Path = "TestConfigU32.Uint32.Number", Value = -4294967296 },
{ File = "config.toml", Path = "TestConfigU64.Uint64.Number", Value = 9223372036854775807 },
{ File = "config.toml", Path = "TestConfigU64.Uint64.Number", Value = -9223372036854775808 },
{ File = "config.toml", Path = "TestConfigF32.Float32.Number", Value = 3.4 },
{ File = "config.toml", Path = "TestConfigF32.Float32.Number", Value = 3.4e+39 },
{ File = "config.toml", Path = "TestConfigF32.Float32.Number", Value = -3.4 },
{ File = "config.toml", Path = "TestConfigF32.Float32.Number", Value = -3.4e+40 },
{ File = "config.toml", Path = "TestConfigF64.Float64.Number", Value = 1.7e+308 },
{ File = "config.toml", Path = "TestConfigF64.Float64.Number", Value = -1.7e+308 },
{ File = "config.toml", Path = "TestConfigStruct.ConfigStruct.Description", Value = { Number = 11 } },
{ File = "config.toml", Path = "TestConfigStruct.ConfigStruct.Description", Value = { Nr = 222 } },
{ File = "config.toml", Path = "TestConfigStruct.ConfigStruct.Description", Value = { Number = "11" } },
{ File = "config.toml", Path = "TestConfigNestedStruct.ConfigNestedStruct", Value = { Text = "Overwritten text", Message = { Public = false, MessageDescription = [{ Text = "Overwritten Text1" }] } } },
{ File = "config.toml", Path = "TestConfigNestedStruct.ConfigNestedStruct.Message.MessageDescription", Value = [{ Text = "Overwritten Text1" }, { Text = "Overwritten Text2" }] },
{ File = "config.toml", Path = "TestMap.Map", Value = { "Key1" = { Number = 10 }, "Key2" = { Number = 11 } } },
{ File = "config.toml", Path = "TestMap.Map", Value = { "Key2" = { Number = 2 }, "Key3" = { Number = 3 } } },
{ File = "config.toml", Path = "TestArray.Strings", Value = ["x", "y", "z"] },
{ File = "config.toml", Path = "TestArray.Ints", Value = [10, 20, 30] },
{ File = "config.toml", Path = "TestArray", Value = { Strings = ["x", "y", "z"], Ints = [10, 20, 30] } },
]
Loading