-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Metrics API Scaler support different formats (#5347)
* Metrics API Scaler support different format Signed-off-by: Friedrich Albert Kyuri <friedrichak42@gmail.com> * fix golangci-lint issues Signed-off-by: Friedrich Albert Kyuri <friedrichak42@gmail.com> * trigger ci and go mod tidy Signed-off-by: Friedrich Albert Kyuri <friedrichak42@gmail.com> * gci Signed-off-by: Friedrich Albert Kyuri <friedrichak42@gmail.com> * fix tests for metrics_api_scaler; add new tests for value_by_path; fix []interface edge case for value_by_path Signed-off-by: Friedrich Albert Kyuri <friedrichak42@gmail.com> * fix: add format field to e2e tests; edit comment about apiformat Signed-off-by: Friedrich Albert Kyuri <friedrichak42@gmail.com> * update changelog Signed-off-by: Friedrich Albert Kyuri <friedrichak42@gmail.com> * Apply suggestions from code review Co-authored-by: Jan Wozniak <wozniak.jan@gmail.com> Signed-off-by: Murr Kyuri <39532283+Friedrich42@users.noreply.github.com> * decrease duplication by defining a variable for inputs, revert test format field to ensure backwards compatibility Signed-off-by: Friedrich Albert Kyuri <friedrichak42@gmail.com> * add docstring with examples Signed-off-by: Friedrich Albert Kyuri <friedrichak42@gmail.com> --------- Signed-off-by: Friedrich Albert Kyuri <friedrichak42@gmail.com> Signed-off-by: Murr Kyuri <39532283+Friedrich42@users.noreply.github.com> Co-authored-by: Jan Wozniak <wozniak.jan@gmail.com>
- Loading branch information
1 parent
55075a7
commit 76c6ac0
Showing
6 changed files
with
319 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// GetValueByPath retrieves a value from a nested map using a dot-separated path | ||
// It also supports .number syntax to access array elements. | ||
// | ||
// This is a helper function for niche use cases. | ||
// Consider using https://pkg.go.dev/k8s.io/apimachinery@v0.29.3/pkg/apis/meta/v1/unstructured#NestedFieldNoCopy instead | ||
// | ||
// Examples: | ||
// | ||
// data := map[string]interface{}{ | ||
// "a": map[string]interface{}{ | ||
// "b": []interface{}{ | ||
// map[string]interface{}{"c": 1}, | ||
// map[string]interface{}{"c": 2}, | ||
// }, | ||
// }, | ||
// } | ||
// | ||
// GetValueByPath(data, "a.b.0.c") // 1 | ||
// GetValueByPath(data, "not.found") // error | ||
func GetValueByPath(data map[string]interface{}, path string) (interface{}, error) { | ||
keys := strings.Split(path, ".") | ||
current := data | ||
|
||
for _, key := range keys { | ||
val, ok := current[key] | ||
if !ok { | ||
return nil, fmt.Errorf("key '%s' not found in path '%s'", key, path) | ||
} | ||
|
||
switch typedValue := val.(type) { | ||
case map[interface{}]interface{}: | ||
// Convert map[interface{}]interface{} to map[string]interface{} | ||
current = make(map[string]interface{}) | ||
for k, v := range typedValue { | ||
current[fmt.Sprintf("%v", k)] = v | ||
} | ||
case []interface{}: | ||
// Convert map[interface{}]interface{} to map[string]interface{} | ||
current = make(map[string]interface{}) | ||
for k, v := range typedValue { | ||
current[fmt.Sprintf("%v", k)] = v | ||
} | ||
case map[string]interface{}: | ||
current = typedValue | ||
default: | ||
// Reached the final value | ||
return val, nil | ||
} | ||
} | ||
|
||
return nil, fmt.Errorf("path '%s' does not lead to a value", path) | ||
} |
Oops, something went wrong.