Skip to content
Snippets Groups Projects
Verified Commit 2b47ee4c authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

fix: find and get dont work #13

parent b9d29fd1
No related branches found
No related tags found
No related merge requests found
......@@ -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
}
......
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment