Skip to content

Commit

Permalink
Avoid touching the original structure of the options.
Browse files Browse the repository at this point in the history
Signed-off-by: Ruben Vargas <[email protected]>
  • Loading branch information
rubenvp8510 committed Jul 12, 2019
1 parent 1dfbda6 commit e71a66e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/apis/jaegertracing/v1/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
// Options defines a common options parameter to the different structs
type Options struct {
opts map[string]string
json []byte
}

// NewOptions build a new Options object based on the given map
Expand Down Expand Up @@ -46,13 +47,18 @@ func (o *Options) UnmarshalJSON(b []byte) error {
}

o.parse(entries)
o.json = b
return nil
}

// MarshalJSON specifies how to convert this object into JSON
func (o Options) MarshalJSON() ([]byte, error) {
b, err := json.Marshal(o.opts)
return b, err
if len(o.json) == 0 && len(o.opts) == 0 {
return []byte("{}"), nil
} else if len(o.json) == 0 && len(o.opts) > 0 {
return json.Marshal(o.opts)
}
return o.json, nil
}

func (o *Options) parse(entries map[string]interface{}) {
Expand Down
17 changes: 17 additions & 0 deletions pkg/apis/jaegertracing/v1/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,20 @@ func TestExposedMap(t *testing.T) {
o.UnmarshalJSON([]byte(`{"cassandra": {"servers": "cassandra:9042"}}`))
assert.Equal(t, "cassandra:9042", o.Map()["cassandra.servers"])
}

func TestMarshallRaw(t *testing.T) {
json := []byte(`{"cassandra": {"servers": "cassandra:9042"}}`)
o := NewOptions(nil)
o.json = json
bytes, err := o.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bytes, json)
}

func TestMarshallEmpty(t *testing.T) {
o := NewOptions(nil)
json := []byte(`{}`)
bytes, err := o.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bytes, json)
}
5 changes: 5 additions & 0 deletions pkg/apis/jaegertracing/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e71a66e

Please sign in to comment.