3
0

Fix DecodeKV issue with empty strings

This fix enables app-settings struct updates even if we pass in the
empty or missing string value to settings.
This commit is contained in:
Denis Arh
2021-09-16 17:30:06 +02:00
parent c6f95e858f
commit 585d6896eb
3 changed files with 25 additions and 3 deletions
+1
View File
@@ -152,6 +152,7 @@ func (svc *settings) UpdateCurrent(ctx context.Context) error {
}
func (svc *settings) updateCurrent(ctx context.Context, vv types.SettingValueSet) (err error) {
// update current settings with new values
if err = vv.KV().Decode(svc.current); err != nil {
return
+8
View File
@@ -145,6 +145,14 @@ func DecodeKV(kv SettingsKV, dst interface{}, pp ...string) (err error) {
// Native type
if val, ok := kv[key]; ok {
// Always use pointer to value
if val == nil {
switch structFType.Type.Kind() {
case reflect.String:
structField.SetString("")
continue
}
}
if val.Unmarshal(structField.Addr().Interface()) != nil {
// Try to get numbers encoded as strings...
var tmp interface{}
+16 -3
View File
@@ -35,14 +35,21 @@ func TestDecode(t *testing.T) {
Map map[string]string `kv:"sub.map"`
S2I map[string]int `kv:"sub.s2i"`
PrefilledString1 string
PrefilledString2 string
}
)
var (
ptr = "point-me"
aux = dst{}
kv = SettingsKV{
aux = dst{
PrefilledString1: "values",
PrefilledString2: "values",
}
kv = SettingsKV{
"s": types.JSONText(`"string"`),
"b": types.JSONText("true"),
"n": types.JSONText("42"),
@@ -60,6 +67,9 @@ func TestDecode(t *testing.T) {
"sub.s2i.one": types.JSONText(`1`),
"sub.s2i.two": types.JSONText(`2`),
"prefilledString1": nil,
"prefilledString2": types.JSONText(`""`),
}
eq = dst{
@@ -85,10 +95,13 @@ func TestDecode(t *testing.T) {
"one": 1,
"two": 2,
},
PrefilledString1: "",
PrefilledString2: "",
}
)
// setting this externaly (embedded structs)
// setting this externally (embedded structs)
eq.Sub.Bar.Foo = "foobar"
require.NoError(t, DecodeKV(kv, &aux))