Skip to content

Commit

Permalink
support slice iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
pPrecel committed Jan 28, 2025
1 parent 278fc35 commit 50ec257
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
32 changes: 29 additions & 3 deletions internal/cmd/alpha/templates/parameters/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package parameters
import (
"fmt"
"reflect"
"strconv"
"strings"

"github.com/kyma-project/cli.v3/internal/clierror"
Expand Down Expand Up @@ -45,15 +46,22 @@ func buildExtraValuesObject(value interface{}, fields ...string) (map[string]int
return obj, unstructured.SetNestedField(obj, value, fields...)
}

if strings.HasSuffix(fields[i], "[]") {
if strings.HasSuffix(fields[i], "]") && strings.Contains(fields[i], "[") {
// is slice
fields[i] = strings.TrimSuffix(fields[i], "[]")
sliceIter, err := getSliceFieldIterator(fields[i])
if err != nil {
return nil, errors.Wrapf(err, "failed to get slice element number from field %s", fields[i])
}

fields[i] = trimSliceFieldSuffix(fields[i])
subObj, err := buildExtraValuesObject(value, fields[i+1:]...)
if err != nil {
return nil, err
}

err = unstructured.SetNestedSlice(obj, []interface{}{subObj}, fields[:i+1]...)
slice := make([]interface{}, sliceIter+1)
slice[sliceIter] = subObj
err = unstructured.SetNestedSlice(obj, slice, fields[:i+1]...)
if err != nil {
return nil, errors.Wrapf(err, "failed to set slice value for path .%s", strings.Join(fields[:i], "."))
}
Expand All @@ -69,6 +77,21 @@ func buildExtraValuesObject(value interface{}, fields ...string) (map[string]int
return obj, nil
}

func getSliceFieldIterator(field string) (int, error) {
stringIter := field[strings.Index(field, "[")+1 : len(field)-1]
if stringIter == "" {
// is []
return 0, nil
}

iter, err := strconv.ParseInt(stringIter, 10, 0)
return int(iter), err
}

func trimSliceFieldSuffix(field string) string {
return field[:strings.Index(field, "[")]
}

func mergeObjects(from map[string]interface{}, to map[string]interface{}) error {
for key, val := range from {
toVal, ok := to[key]
Expand Down Expand Up @@ -117,6 +140,9 @@ func mergeSlices(from, to []interface{}) ([]interface{}, error) {
err = mergeObjects(from[i].(map[string]interface{}), dest[i].(map[string]interface{}))
case []interface{}:
dest[i], err = mergeSlices(from[i].([]interface{}), dest[i].([]interface{}))
case nil:
// in this case we want keep value from dest slice
continue
default:
// is simple type like int64, string, bool...
dest[i] = from[i]
Expand Down
6 changes: 5 additions & 1 deletion internal/cmd/alpha/templates/parameters/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func TestSet(t *testing.T) {
newStringValue(".metadata.namespace", "default"), // set with existing .metadata
newInt64Value(".spec.runtimes", 3), // set new field
newInt64Value(".spec.elems[].iter", 1), // set slice
newInt64Value(".spec.elems[].iter", 2), // append slice
newInt64Value(".spec.elems[].iter", 2), // overwrite slice
newInt64Value(".spec.elems[1].iter", 3), // append another slice elem
},
},
want: map[string]interface{}{
Expand All @@ -47,6 +48,9 @@ func TestSet(t *testing.T) {
map[string]interface{}{
"iter": int64(2),
},
map[string]interface{}{
"iter": int64(3),
},
},
},
},
Expand Down

0 comments on commit 50ec257

Please sign in to comment.