Skip to content

Commit

Permalink
Unmarshall numbers in options to number not float64 (#308)
Browse files Browse the repository at this point in the history
* Unmarshall numbers in options to number not float64

Signed-off-by: Pavol Loffay <[email protected]>

* Add mising tests

Signed-off-by: Pavol Loffay <[email protected]>

* call unmarshall directly

Signed-off-by: Pavol Loffay <[email protected]>
  • Loading branch information
pavolloffay authored Mar 13, 2019
1 parent 0f1c97e commit 540b7e9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/apis/jaegertracing/v1/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1

import (
"bytes"
"encoding/json"
"fmt"
"strings"
Expand Down Expand Up @@ -38,7 +39,11 @@ func (o *Options) Filter(prefix string) Options {
// UnmarshalJSON implements an alternative parser for this field
func (o *Options) UnmarshalJSON(b []byte) error {
var entries map[string]interface{}
json.Unmarshal(b, &entries)
d := json.NewDecoder(bytes.NewReader(b))
d.UseNumber()
if err := d.Decode(&entries); err != nil {
return err
}

return o.parse(entries)
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/apis/jaegertracing/v1/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,34 @@ func TestMultipleSubValues(t *testing.T) {
assert.Len(t, args, 3)
}

func TestUnmarshalToArgs(t *testing.T) {
tests := []struct {
in string
args []string
err string
}{
{in: `^`, err: "invalid character '^' looking for beginning of value"},
{
in: `{"a": 5000000000, "b": 15.222, "c":true, "d": "foo"}`,
args: []string{"--a=5000000000", "--b=15.222", "--c=true", "--d=foo"},
},
}
for _, test := range tests {
opts := Options{}
err := opts.UnmarshalJSON([]byte(test.in))
if test.err != "" {
assert.EqualError(t, err, test.err)
} else {
assert.NoError(t, err)
args := opts.ToArgs()
sort.SliceStable(args, func(i, j int) bool {
return args[i] < args[j]
})
assert.Equal(t, test.args, args)
}
}
}

func TestMultipleSubValuesWithFilter(t *testing.T) {
o := NewOptions(nil)
o.UnmarshalJSON([]byte(`{"memory": {"max-traces": "50000"}, "es": {"server-urls": "http://elasticsearch:9200", "username": "elastic", "password": "changeme"}}`))
Expand Down

0 comments on commit 540b7e9

Please sign in to comment.