Skip to content

Commit

Permalink
Fix parsing dynamic config numbers (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigozhou committed May 8, 2023
1 parent 067a32c commit 9d15378
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 16 additions & 2 deletions server/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
package server

import (
"bytes"
"encoding/json"
"fmt"
"net"
Expand Down Expand Up @@ -353,8 +354,21 @@ func getDynamicConfigValues(input []string) (map[dynamicconfig.Key][]dynamicconf
key := dynamicconfig.Key(keyVal[0])
// We don't support constraints currently
var val dynamicconfig.ConstrainedValue
if err := json.Unmarshal([]byte(keyVal[1]), &val.Value); err != nil {
return nil, fmt.Errorf("invalid JSON value for key %q: %w", key, err)
dec := json.NewDecoder(bytes.NewReader([]byte(keyVal[1])))
dec.UseNumber()
if err := dec.Decode(&val.Value); err != nil || dec.InputOffset() != int64(len(keyVal[1])) {
return nil, fmt.Errorf("invalid JSON value for key %q", key)
}
if num, ok := val.Value.(json.Number); ok {
if v, err := num.Int64(); err == nil {
// dynamic config only accepts int type, not int32 nor int64
val.Value = int(v)
} else if v, err := num.Float64(); err == nil {
val.Value = v
} else {
// should not be possible: decoded json.Number must be parsable to int64 or float64
return nil, fmt.Errorf("invalid JSON value for key %q", key)
}
}
ret[key] = append(ret[key], val)
}
Expand Down
4 changes: 2 additions & 2 deletions server/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ func TestGetDynamicConfigValues(t *testing.T) {
assertBadVal("foo=bar")
assertBadVal("foo=123a")

assertGoodVals(v{"foo": {123.0}}, "foo=123")
assertGoodVals(v{"foo": {123}}, "foo=123")
assertGoodVals(
v{"foo": {123.0, []interface{}{"123", false}}, "bar": {"baz"}, "qux": {true}},
v{"foo": {123, []interface{}{"123", false}}, "bar": {"baz"}, "qux": {true}},
"foo=123", `bar="baz"`, "qux=true", `foo=["123", false]`,
)
}

0 comments on commit 9d15378

Please sign in to comment.