Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 19 additions & 38 deletions pkg/platform/parameters/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,44 +190,25 @@ func checkNumberBounds(num int, parameter storagev1.AppParameter) error {
}

func GetDeepValue(parameters any, path string) any {
if parameters == nil {
return nil
}

pathSegments := strings.Split(path, ".")
switch t := parameters.(type) {
case map[string]any:
return getDeepValueFromMap(t, pathSegments)
case []any:
return getDeepValueFromSlice(t, pathSegments)
}

return nil
}

func getDeepValueFromMap(t map[string]any, pathSegments []string) any {
val, ok := t[pathSegments[0]]
if !ok {
return nil
} else if len(pathSegments) == 1 {
return val
}

return GetDeepValue(val, strings.Join(pathSegments[1:], "."))
}

func getDeepValueFromSlice(t []any, pathSegments []string) any {
index, err := strconv.Atoi(pathSegments[0])
if err != nil {
return nil
} else if index < 0 || index >= len(t) {
return nil
}

val := t[index]
if len(pathSegments) == 1 {
return val
current := parameters
for segment := range strings.SplitSeq(path, ".") {
switch t := current.(type) {
case map[string]any:
val, ok := t[segment]
if !ok {
return nil
}
current = val
case []any:
index, err := strconv.Atoi(segment)
if err != nil || index < 0 || index >= len(t) {
return nil
}
current = t[index]
default:
return nil
}
}

return GetDeepValue(val, strings.Join(pathSegments[1:], "."))
return current
}
25 changes: 25 additions & 0 deletions pkg/platform/parameters/parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,31 @@ func TestGetDeepValue(t *testing.T) {
},
{name: "path through scalar yields nil", parameters: withItems, path: "text.length"},
{name: "unsupported root type yields nil", parameters: "plain", path: "a"},
{
name: "empty path looks up the empty-string key",
parameters: map[string]any{"": "blank"},
path: "",
want: "blank",
},
{
name: "empty segment is a literal map key",
parameters: map[string]any{"a": map[string]any{"": 2}},
path: "a.",
want: 2,
},
{name: "nil value mid-path yields nil", parameters: map[string]any{"a": nil}, path: "a.b"},
{
name: "plus-signed index parses via Atoi",
parameters: withItems,
path: "items.+1",
want: "y",
},
{
name: "zero-padded index parses via Atoi",
parameters: withItems,
path: "items.01",
want: "y",
},
}

for _, tc := range cases {
Expand Down
Loading