diff --git a/find.go b/find.go index e1a262b18e030f650a1f2d1c1e75e9c8f836de26..39955d3ed370d8c61899f371138f380a2b34f65e 100644 --- a/find.go +++ b/find.go @@ -26,19 +26,14 @@ func FindPaths(v reflect.Value, targetType reflect.Type, path []string, paths *[ for _, key := range v.MapKeys() { newPath := append(path, fmt.Sprint(key)) FindPaths(v.MapIndex(key), targetType, newPath, paths) - if v.MapIndex(key).Type() == targetType { - *paths = append(*paths, strings.Join(newPath, ".")) - } } case reflect.Slice, reflect.Array: for i := 0; i < v.Len(); i++ { newPath := append(path, fmt.Sprint(i)) FindPaths(v.Index(i), targetType, newPath, paths) - if v.Index(i).Type() == targetType { - *paths = append(*paths, strings.Join(newPath, ".")) - } } - case reflect.String: + + case reflect.Bool, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uint: if vType != targetType { return } diff --git a/find_test.go b/find_test.go index 367a33ef2d6406d2ef4167b6005af15b1b32d66f..e241df51d326f94d072e580be78bbaebcb01e14b 100644 --- a/find_test.go +++ b/find_test.go @@ -28,6 +28,35 @@ func TestFindPaths(t *testing.T) { reflect.TypeOf(""), []string{"Field1", "Inner.SubField2"}, }, + + { + nil, + reflect.TypeOf(""), + []string{}, + }, + } + + for i, test := range tests { + var paths []string + FindPaths(reflect.ValueOf(test.input), test.targetType, []string{}, &paths) + + if len(paths) == 0 && len(test.expectedPaths) == 0 { + continue + } + + if !reflect.DeepEqual(paths, test.expectedPaths) { + t.Errorf("Test case %d failed, expected %v, got %v", i+1, test.expectedPaths, paths) + } + } +} + +func TestFindPaths2(t *testing.T) { + tests := []struct { + input interface{} + targetType reflect.Type + expectedPaths []string + }{ + { []int{1, 2, 3}, reflect.TypeOf(0), @@ -38,11 +67,6 @@ func TestFindPaths(t *testing.T) { reflect.TypeOf(0), []string{"key1", "key2"}, }, - { - nil, - reflect.TypeOf(""), - []string{}, - }, } for i, test := range tests {