Skip to content

Commit

Permalink
Add support repetitive arguments to operand
Browse files Browse the repository at this point in the history
Signed-off-by: Ruben Vargas <[email protected]>
  • Loading branch information
rubenvp8510 committed Apr 21, 2021
1 parent 5bf83e1 commit 7ceebc2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 19 deletions.
51 changes: 39 additions & 12 deletions pkg/apis/jaegertracing/v1/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,28 @@ import (
"strings"
)

// Values represent and string or a slice of string
type Values map[string]interface{}

// DeepCopy indicate how to do a deep copy of Values type
func (v *Values) DeepCopy() *Values {
out := make(Values, len(*v))
for key, val := range *v {
switch val.(type) {
case string:
out[key] = val

case []string:
out[key] = append([]string(nil), val.([]string)...)
}
}
return &out
}

// Options defines a common options parameter to the different structs
type Options struct {
opts map[string]string `json:"-"`
json *[]byte `json:"-"`
opts Values `json:"-"`
json *[]byte `json:"-"`
}

// NewOptions build a new Options object based on the given map
Expand All @@ -23,7 +41,7 @@ func NewOptions(o map[string]interface{}) Options {
// Filter creates a new Options object with just the elements identified by the supplied prefix
func (o *Options) Filter(prefix string) Options {
options := Options{}
options.opts = make(map[string]string)
options.opts = make(map[string]interface{})

archivePrefix := prefix + "-archive."
prefix += "."
Expand All @@ -45,7 +63,6 @@ func (o *Options) UnmarshalJSON(b []byte) error {
if err := d.Decode(&entries); err != nil {
return err
}

o.parse(entries)
o.json = &b
return nil
Expand All @@ -69,18 +86,24 @@ func (o Options) MarshalJSON() ([]byte, error) {
}

func (o *Options) parse(entries map[string]interface{}) {
o.opts = make(map[string]string)
o.opts = make(map[string]interface{})
for k, v := range entries {
o.opts = entry(o.opts, k, v)
}
}

func entry(entries map[string]string, key string, value interface{}) map[string]string {
func entry(entries map[string]interface{}, key string, value interface{}) map[string]interface{} {
switch value.(type) {
case map[string]interface{}:
for k, v := range value.(map[string]interface{}) {
entries = entry(entries, fmt.Sprintf("%s.%v", key, k), v)
}
case []interface{}:
values := make([]string, 0, len(value.([]interface{})))
for _, v := range value.([]interface{}) {
values = append(values, v.(string))
}
entries[key] = values
case interface{}:
entries[key] = fmt.Sprintf("%v", value)
}
Expand All @@ -91,21 +114,25 @@ func entry(entries map[string]string, key string, value interface{}) map[string]
// ToArgs converts the options to a value suitable for the Container.Args field
func (o *Options) ToArgs() []string {
if len(o.opts) > 0 {
i := 0
args := make([]string, len(o.opts))
args := make([]string, 0, len(o.opts))
for k, v := range o.opts {
args[i] = fmt.Sprintf("--%s=%v", k, v)
i++
switch v.(type) {
case string:
args = append(args, fmt.Sprintf("--%s=%v", k, v))
case []string:
for _, vv := range v.([]string) {
args = append(args, fmt.Sprintf("--%s=%v", k, vv))
}
}
}
return args
}

return nil
}

// Map returns a map representing the option entries. Items are flattened, with dots as separators. For instance
// an option "cassandra" with a nested "servers" object becomes an entry with the key "cassandra.servers"
func (o *Options) Map() map[string]string {
func (o *Options) Map() map[string]interface{} {
return o.opts
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/apis/jaegertracing/v1/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"sort"
"testing"

"github.com/stretchr/testify/require"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -148,3 +150,19 @@ func TestUpdate(t *testing.T) {
// verify
assert.Equal(t, o.opts["key"], "new")
}

func TestRepetitiveArguments(t *testing.T) {
o := NewOptions(nil)
err := o.UnmarshalJSON([]byte(`{"firstsarg":"v1", "additional-headers":["whatever:thing", "access-control-allow-origin:blerg"]}`))
require.NoError(t, err)
expected := []string{"--additional-headers=access-control-allow-origin:blerg", "--additional-headers=whatever:thing", "--firstsarg=v1"}

args := o.ToArgs()
sort.SliceStable(args, func(i, j int) bool {
return args[i] < args[j]
})

assert.Len(t, args, 3)
assert.Equal(t, expected, args)

}
18 changes: 11 additions & 7 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 7ceebc2

Please sign in to comment.