0
0
Fork 0

fix type identification when object indexed is pointer to struct

fixes #508
This commit is contained in:
Marty Schoch 2016-12-08 08:07:38 -05:00
parent d4f21a6290
commit 3b2bc30b54
2 changed files with 52 additions and 0 deletions

View File

@ -51,6 +51,11 @@ func lookupPropertyPathPart(data interface{}, part string) interface{} {
if field.IsValid() && field.CanInterface() {
return field.Interface()
}
case reflect.Ptr:
ptrElem := val.Elem()
if ptrElem.IsValid() && ptrElem.CanInterface() {
return lookupPropertyPathPart(ptrElem.Interface(), part)
}
}
return nil
}

47
mapping/reflect_test.go Normal file
View File

@ -0,0 +1,47 @@
package mapping
import (
"reflect"
"testing"
)
func TestLookupPropertyPath(t *testing.T) {
tests := []struct {
input interface{}
path string
output interface{}
}{
{
input: map[string]interface{}{
"Type": "a",
},
path: "Type",
output: "a",
},
{
input: struct {
Type string
}{
Type: "b",
},
path: "Type",
output: "b",
},
{
input: &struct {
Type string
}{
Type: "b",
},
path: "Type",
output: "b",
},
}
for _, test := range tests {
actual := lookupPropertyPath(test.input, test.path)
if !reflect.DeepEqual(actual, test.output) {
t.Fatalf("expected '%v', got '%v', for path '%s' in %+v", test.output, actual, test.path, test.input)
}
}
}