Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Pathfinder
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Libraries
Go
Utilities
Pathfinder
Commits
2b47ee4c
Verified
Commit
2b47ee4c
authored
1 year ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
fix: find and get dont work
#13
parent
b9d29fd1
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
find.go
+2
-7
2 additions, 7 deletions
find.go
find_test.go
+29
-5
29 additions, 5 deletions
find_test.go
with
31 additions
and
12 deletions
find.go
+
2
−
7
View file @
2b47ee4c
...
...
@@ -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
}
...
...
This diff is collapsed.
Click to expand it.
find_test.go
+
29
−
5
View file @
2b47ee4c
...
...
@@ -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
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment