-
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 format
Signed-off-by: Friedrich Albert Kyuri <[email protected]>
- Loading branch information
Friedrich Albert Kyuri
committed
Feb 14, 2024
1 parent
4da3a5b
commit 724b63e
Showing
4 changed files
with
283 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// GetValueByPath retrieves a value from a nested map using a dot-separated path | ||
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 v := val.(type) { | ||
case map[interface{}]interface{}: | ||
// Convert map[interface{}]interface{} to map[string]interface{} | ||
current = make(map[string]interface{}) | ||
for k, v := range v { | ||
current[fmt.Sprintf("%v", k)] = v | ||
} | ||
case map[string]interface{}: | ||
current = v | ||
default: | ||
// Reached the final value | ||
return val, nil | ||
} | ||
} | ||
|
||
return nil, fmt.Errorf("path '%s' does not lead to a value", path) | ||
} |
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,71 @@ | ||
package util | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestGetValueByPath(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input map[string]interface{} | ||
path string | ||
expected interface{} | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Valid path - String value", | ||
input: map[string]interface{}{ | ||
"some": map[string]interface{}{ | ||
"nested": map[string]interface{}{ | ||
"key": "value", | ||
}, | ||
}, | ||
}, | ||
path: "some.nested.key", | ||
expected: "value", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Valid path - Integer value", | ||
input: map[string]interface{}{ | ||
"another": map[string]interface{}{ | ||
"nested": map[string]interface{}{ | ||
"key": 42, | ||
}, | ||
}, | ||
}, | ||
path: "another.nested.key", | ||
expected: 42, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Invalid path - Key not found", | ||
input: map[string]interface{}{ | ||
"some": map[string]interface{}{ | ||
"nested": map[string]interface{}{ | ||
"key": "value", | ||
}, | ||
}, | ||
}, | ||
path: "nonexistent.path", | ||
expected: nil, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
actual, err := GetValueByPath(tt.input, tt.path) | ||
|
||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Unexpected error status. got %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
|
||
if !reflect.DeepEqual(actual, tt.expected) { | ||
t.Errorf("Mismatched result. got %v, want %v", actual, tt.expected) | ||
} | ||
}) | ||
} | ||
} |