From 540b7e9006cd07a88f1195a63b8f76fa5a8fc56d Mon Sep 17 00:00:00 2001
From: Pavol Loffay
Date: Wed, 13 Mar 2019 15:53:55 +0100
Subject: [PATCH] Unmarshall numbers in options to number not float64 (#308)
* Unmarshall numbers in options to number not float64
Signed-off-by: Pavol Loffay
* Add mising tests
Signed-off-by: Pavol Loffay
* call unmarshall directly
Signed-off-by: Pavol Loffay
---
pkg/apis/jaegertracing/v1/options.go | 7 +++++-
pkg/apis/jaegertracing/v1/options_test.go | 28 +++++++++++++++++++++++
2 files changed, 34 insertions(+), 1 deletion(-)
diff --git a/pkg/apis/jaegertracing/v1/options.go b/pkg/apis/jaegertracing/v1/options.go
index 68ee4d542..63c449950 100644
--- a/pkg/apis/jaegertracing/v1/options.go
+++ b/pkg/apis/jaegertracing/v1/options.go
@@ -1,6 +1,7 @@
package v1
import (
+ "bytes"
"encoding/json"
"fmt"
"strings"
@@ -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)
}
diff --git a/pkg/apis/jaegertracing/v1/options_test.go b/pkg/apis/jaegertracing/v1/options_test.go
index 51d5c6b6c..9508eed6f 100644
--- a/pkg/apis/jaegertracing/v1/options_test.go
+++ b/pkg/apis/jaegertracing/v1/options_test.go
@@ -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"}}`))