Skip to content

Commit a0ef39d

Browse files
authored
Merge pull request #1502 from Ace-Tang/config_empty_value
fix: support nullable bool value set in config
2 parents 5159116 + f240d6c commit a0ef39d

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

pkg/utils/utils.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,13 @@ func doMerge(src, dest reflect.Value) error {
196196
return nil
197197
}
198198

199-
// From src/pkg/encoding/json
199+
// From src/pkg/encoding/json,
200+
// we recognize nullable values like `false` `0` as not empty.
200201
func isEmptyValue(v reflect.Value) bool {
201202
switch v.Kind() {
202203
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
203204
return v.Len() == 0
204-
case reflect.Bool:
205-
return !v.Bool()
206-
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
207-
return v.Int() == 0
208-
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
205+
case reflect.Uintptr:
209206
return v.Uint() == 0
210207
case reflect.Float32, reflect.Float64:
211208
return v.Float() == 0

pkg/utils/utils_test.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,17 @@ func TestMerge(t *testing.T) {
187187
}, {
188188
src: &simple{},
189189
dest: &simple{Sa: 1, Sb: "hello", Sc: true, Sd: map[string]string{"go": "gogo"}, Se: nestS{Na: 22}},
190-
expected: &simple{Sa: 1, Sb: "hello", Sc: true, Sd: map[string]string{"go": "gogo"}, Se: nestS{Na: 22}},
190+
expected: &simple{Sa: 0, Sb: "hello", Sc: false, Sd: map[string]string{"go": "gogo"}, Se: nestS{Na: 0}},
191191
ok: true,
192192
}, {
193193
src: &simple{Sa: 1, Sc: true, Sd: map[string]string{"go": "gogo"}, Se: nestS{Na: 11}, Sf: []string{"foo"}},
194-
dest: &simple{Sa: 2, Sb: "world", Sc: false, Sd: map[string]string{"go": "old"}, Se: nestS{Na: 22}, Sf: []string{"foo"}},
195-
expected: &simple{Sa: 1, Sb: "world", Sc: true, Sd: map[string]string{"go": "gogo"}, Se: nestS{Na: 11}, Sf: []string{"foo", "foo"}},
194+
dest: &simple{Sa: 2, Sb: "!", Sc: false, Sd: map[string]string{"go": "old"}, Se: nestS{Na: 22}, Sf: []string{"foo"}},
195+
expected: &simple{Sa: 1, Sb: "!", Sc: true, Sd: map[string]string{"go": "gogo"}, Se: nestS{Na: 11}, Sf: []string{"foo", "foo"}},
196+
ok: true,
197+
}, {
198+
src: &simple{Sa: 0, Sc: false, Sd: map[string]string{"go": "gogo"}, Se: nestS{Na: 11}, Sf: []string{"foo"}},
199+
dest: &simple{Sa: 2, Sb: "world", Sc: true, Sd: map[string]string{"go": "old"}, Se: nestS{Na: 22}, Sf: []string{"foo"}},
200+
expected: &simple{Sa: 0, Sb: "world", Sc: false, Sd: map[string]string{"go": "gogo"}, Se: nestS{Na: 11}, Sf: []string{"foo", "foo"}},
196201
ok: true,
197202
}, {
198203
src: &simple{Sd: map[string]string{"go": "gogo", "a": "b"}},
@@ -204,6 +209,18 @@ func TestMerge(t *testing.T) {
204209
dest: &simple{},
205210
expected: &simple{Sd: map[string]string{"go": "gogo", "a": "b"}},
206211
ok: true,
212+
}, {
213+
// empty map should not overwrite
214+
src: &simple{Sd: map[string]string{}},
215+
dest: &simple{Sd: map[string]string{"a": "b"}},
216+
expected: &simple{Sd: map[string]string{"a": "b"}},
217+
ok: true,
218+
}, {
219+
// empty slice should not overwrite
220+
src: &simple{Sf: []string{}},
221+
dest: &simple{Sf: []string{"c"}},
222+
expected: &simple{Sf: []string{"c"}},
223+
ok: true,
207224
},
208225
} {
209226
err := Merge(tm.src, tm.dest)

0 commit comments

Comments
 (0)