More robust settings value conversion

Fixes #16
This commit is contained in:
Denis Arh
2020-08-29 16:47:29 +02:00
parent f7b5d27fe2
commit 04711ce4ad
2 changed files with 13 additions and 3 deletions
+13 -2
View File
@@ -3,6 +3,7 @@ package settings
import (
"github.com/pkg/errors"
"reflect"
"strconv"
"strings"
)
@@ -143,8 +144,18 @@ func DecodeKV(kv KV, dst interface{}, pp ...string) (err error) {
// Native type
if val, ok := kv[key]; ok {
// Always use pointer to value
if err = val.Unmarshal(structField.Addr().Interface()); err != nil {
return errors.Wrapf(err, "cannot decode settings for %q", key)
if val.Unmarshal(structField.Addr().Interface()) != nil {
// Try to get numbers encoded as strings...
var tmp interface{}
if val.Unmarshal(&tmp) != nil {
return err
}
switch cnv := tmp.(type) {
case string:
num, _ := strconv.ParseUint(cnv, 10, 64)
structField.SetUint(num)
}
}
}
}
-1
View File
@@ -130,7 +130,6 @@ func (svc service) BulkSet(ctx context.Context, vv ValueSet) (err error) {
} else {
vv = current.Changed(vv)
}
err = svc.repository.With(ctx).BulkSet(vv)
if err != nil {
return