Skip to content

Commit

Permalink
overwrite with null value
Browse files Browse the repository at this point in the history
  • Loading branch information
smgladkovskiy committed Jun 1, 2020
1 parent c4967dc commit 217c218
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 11 deletions.
3 changes: 2 additions & 1 deletion config_examples/configuration/defaults/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ defaults:
level: "warn"
format: "json"
host: "127.0.0.1"
port: "8888"
port: "8888"
string_value: string
3 changes: 2 additions & 1 deletion config_examples/configuration/dev/service.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dev:
debug: true
debug: true
string_value: null
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/spacetab-io/configuration-go
go 1.14

require (
github.com/imdario/mergo v0.3.9
github.com/imdario/mergo v0.3.10-0.20200517151347-9080aaff8536
github.com/stretchr/testify v1.5.1
gopkg.in/yaml.v2 v2.3.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg=
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.10-0.20200517151347-9080aaff8536 h1:X5Ang7dBiLOoIANjgXiArEGrPWSjFQdaUjtsV9+gC5I=
github.com/imdario/mergo v0.3.10-0.20200517151347-9080aaff8536/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
4 changes: 2 additions & 2 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func ReadConfigs(cfgPath string) ([]byte, error) {
}

cc := configs[folder]
_ = mergo.Merge(&cc, configFromFile[folder], mergo.WithOverride)
_ = mergo.Merge(&cc, configFromFile[folder], mergo.WithOverwriteWithEmptyValue)

configs[folder] = cc

Expand All @@ -101,7 +101,7 @@ func ReadConfigs(cfgPath string) ([]byte, error) {
config := configs[defaultStage]

if c, ok := configs[stage]; ok {
if err := mergo.Merge(&config, c, mergo.WithOverride); err == nil {
if err := mergo.Merge(&config, c, mergo.WithOverwriteWithEmptyValue); err == nil {
iSay("Stage `%s` config is loaded and merged with `defaults`", stage)
}
}
Expand Down
10 changes: 6 additions & 4 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ func TestReadConfigs(t *testing.T) {
Level string `yaml:"level"`
Format string `yaml:"format"`
} `yaml:"log"`
Host string `yaml:"host"`
Port string `yaml:"port"`
Host string `yaml:"host"`
Port string `yaml:"port"`
StringValue string `yaml:"string_value"`
}

config := &cfg{}
Expand All @@ -38,8 +39,9 @@ func TestReadConfigs(t *testing.T) {
Level string `yaml:"level"`
Format string `yaml:"format"`
}{Level: "error", Format: "text"},
Host: "127.0.0.1",
Port: "8888",
Host: "127.0.0.1",
Port: "8888",
StringValue: "",
}

assert.EqualValues(t, refConfig, config)
Expand Down

0 comments on commit 217c218

Please sign in to comment.