Enable get() to be used with alias $.StateValues (#2081)

This commit is contained in:
Quan TRAN 2022-02-09 01:18:46 +01:00 committed by GitHub
parent 19927fc147
commit 766b03047c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -38,6 +38,10 @@ func get(path string, varArgs ...interface{}) (interface{}, error) {
var v interface{} var v interface{}
var ok bool var ok bool
switch typedObj := obj.(type) { switch typedObj := obj.(type) {
case *map[string]interface{}:
obj = *typedObj
}
switch typedObj := obj.(type) {
case map[string]interface{}: case map[string]interface{}:
v, ok = typedObj[keys[0]] v, ok = typedObj[keys[0]]
if !ok { if !ok {

View File

@ -53,6 +53,26 @@ func TestGetMap(t *testing.T) {
} }
} }
func TestGetMapPtr(t *testing.T) {
obj := map[string]interface{}{"Foo": map[string]interface{}{"Bar": "Bar"}}
objPrt := &obj
v1, err := get("Foo.Bar", objPrt)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if v1 != "Bar" {
t.Errorf("unexpected value for path Foo.Bar in %v: expected=Bar, actual=%v", objPrt, v1)
}
_, err = get("Foo.baz", objPrt)
if err == nil {
t.Errorf("expected error but was not occurred")
}
}
func TestGet_Default(t *testing.T) { func TestGet_Default(t *testing.T) {
obj := map[string]interface{}{"Foo": map[string]interface{}{}, "foo": 1} obj := map[string]interface{}{"Foo": map[string]interface{}{}, "foo": 1}