Skip to content

Commit

Permalink
zero fields overriden in config files to avoid undesired merging logic (
Browse files Browse the repository at this point in the history
flyteorg#81)

Signed-off-by: Haytham Abuelfutuh <[email protected]>
  • Loading branch information
EngHabu authored Apr 27, 2021
1 parent 971d050 commit 378f424
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
32 changes: 32 additions & 0 deletions flytestdlib/config/tests/accessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,38 @@ func TestAccessor_UpdateConfig(t *testing.T) {
assert.Equal(t, 4, (*topLevel)[1].IntValue)
})

t.Run(fmt.Sprintf("[%v] Override default array config", provider(config.Options{}).ID()), func(t *testing.T) {
root := config.NewRootSection()
_, err := root.RegisterSection(MyComponentSectionKey, &ItemArray{
Items: []Item{
{
ID: "default_1",
Name: "default_Name",
},
{
ID: "default_2",
Name: "default_2_Name",
},
},
OtherItem: Item{
ID: "default_3",
Name: "default_3_name",
},
})
assert.NoError(t, err)

v := provider(config.Options{
SearchPaths: []string{filepath.Join("testdata", "array_config_2.yaml")},
RootSection: root,
})

assert.NoError(t, v.UpdateConfig(context.TODO()))
r := root.GetSection(MyComponentSectionKey).GetConfig().(*ItemArray)
assert.Len(t, r.Items, 1)
assert.Equal(t, "abc", r.Items[0].ID)
assert.Equal(t, "default_3", r.OtherItem.ID)
})

t.Run(fmt.Sprintf("[%v] Override in Env Var", provider(config.Options{}).ID()), func(t *testing.T) {
reg := config.NewRootSection()
_, err := reg.RegisterSection(MyComponentSectionKey, &MyComponentConfig{})
Expand Down
4 changes: 4 additions & 0 deletions flytestdlib/config/tests/testdata/array_config_2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
my-component:
items:
- id: abc
name: "A b c"
10 changes: 10 additions & 0 deletions flytestdlib/config/tests/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ type OtherComponentConfig struct {
StringArrayWithDefaults []string `json:"strings-def"`
}

type Item struct {
ID string `json:"id"`
Name string `json:"name"`
}

type ItemArray struct {
Items []Item `json:"items"`
OtherItem Item `json:"otherItem"`
}

func (MyComponentConfig) GetPFlagSet(prefix string) *pflag.FlagSet {
cmdFlags := pflag.NewFlagSet("MyComponentConfig", pflag.ExitOnError)
cmdFlags.String(fmt.Sprintf("%v%v", prefix, "str"), "hello world", "life is short")
Expand Down
2 changes: 2 additions & 0 deletions flytestdlib/config/viper/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ func defaultDecoderConfig(output interface{}, opts ...viperLib.DecoderConfigOpti
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToSliceHookFunc(","),
),
// Empty/zero fields before applying provided values. This avoids potentially undesired/unexpected merging logic.
ZeroFields: true,
}

for _, opt := range opts {
Expand Down

0 comments on commit 378f424

Please sign in to comment.