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
Compare revisions
v0.4.0 to v0.5.0
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
oss/libraries/go/utilities/pathfinder
Select target project
No results found
v0.5.0
Select Git revision
Swap
Target
oss/libraries/go/utilities/pathfinder
Select target project
oss/libraries/go/utilities/pathfinder
1 result
v0.4.0
Select Git revision
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source (3)
fix: optimize getter
· 8345a09b
Volker Schukai
authored
2 years ago
8345a09b
Bump version to 0.5.0
· 7d1448df
Volker Schukai
authored
2 years ago
7d1448df
Update changelog
· c4f15f6c
Volker Schukai
authored
2 years ago
c4f15f6c
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
CHANGELOG.md
+4
-0
4 additions, 0 deletions
CHANGELOG.md
get.go
+25
-36
25 additions, 36 deletions
get.go
get_test.go
+19
-0
19 additions, 0 deletions
get_test.go
release.json
+1
-1
1 addition, 1 deletion
release.json
with
49 additions
and
37 deletions
CHANGELOG.md
View file @
c4f15f6c
<a
name=
"v0.5.0"
></a>
## [v0.5.0] - 2022-12-18
<a
name=
"v0.4.0"
></a>
## [v0.4.0] - 2022-12-17
...
...
@@ -20,6 +23,7 @@
-
feat takeover from other project
[
v0.5.0
]:
https://gitlab.schukai.com/oss/libraries/go/utilities/pathfinder/compare/v0.4.0...v0.5.0
[
v0.4.0
]:
https://gitlab.schukai.com/oss/libraries/go/utilities/pathfinder/compare/v0.3.1...v0.4.0
[
v0.3.1
]:
https://gitlab.schukai.com/oss/libraries/go/utilities/pathfinder/compare/v0.3.0...v0.3.1
[
v0.3.0
]:
https://gitlab.schukai.com/oss/libraries/go/utilities/pathfinder/compare/v0.2.0...v0.3.0
This diff is collapsed.
Click to expand it.
get.go
View file @
c4f15f6c
...
...
@@ -5,6 +5,7 @@ package pathfinder
import
(
"reflect"
"strconv"
"strings"
)
...
...
@@ -13,28 +14,35 @@ func GetValue[D any](obj D, keyWithDots string) (any, error) {
keySlice
:=
strings
.
Split
(
keyWithDots
,
"."
)
v
:=
reflect
.
ValueOf
(
obj
)
for
_
,
key
:=
range
keySlice
[
0
:
len
(
keySlice
)
-
1
]
{
for
v
.
Kind
()
==
reflect
.
Ptr
{
if
v
.
Kind
()
==
reflect
.
Invalid
{
return
nil
,
newInvalidPathError
(
keyWithDots
)
}
for
_
,
key
:=
range
keySlice
[
0
:
len
(
keySlice
)]
{
switch
v
.
Kind
()
{
case
reflect
.
Ptr
,
reflect
.
Slice
,
reflect
.
Array
,
reflect
.
Interface
:
v
=
v
.
Elem
()
}
if
v
.
Kind
()
==
reflect
.
Map
{
switch
v
.
Type
()
.
Key
()
.
Kind
()
{
case
reflect
.
String
:
v
=
v
.
MapIndex
(
reflect
.
ValueOf
(
key
))
.
Elem
()
continue
default
:
return
nil
,
newUnsupportedTypePathError
(
keyWithDots
,
v
.
Type
())
switch
v
.
Kind
()
{
case
reflect
.
Map
:
v
=
v
.
MapIndex
(
reflect
.
ValueOf
(
key
))
if
!
v
.
IsValid
()
{
return
nil
,
newInvalidPathError
(
keyWithDots
)
}
case
reflect
.
Slice
,
reflect
.
Array
:
index
,
err
:=
strconv
.
Atoi
(
key
)
if
err
!=
nil
{
return
nil
,
newInvalidPathError
(
keyWithDots
)
}
}
else
if
v
.
Kind
()
!=
reflect
.
Struct
{
return
nil
,
newUnsupportedTypePathError
(
keyWithDots
,
v
.
Type
())
v
=
v
.
Index
(
index
)
case
reflect
.
Struct
:
v
=
v
.
FieldByName
(
key
)
if
!
v
.
IsValid
()
{
return
nil
,
newInvalidPathError
(
keyWithDots
)
}
default
:
return
nil
,
newInvalidPathError
(
keyWithDots
)
}
v
=
v
.
FieldByName
(
key
)
}
if
v
.
Kind
()
==
reflect
.
Invalid
{
...
...
@@ -44,26 +52,7 @@ func GetValue[D any](obj D, keyWithDots string) (any, error) {
for
v
.
Kind
()
==
reflect
.
Ptr
{
v
=
v
.
Elem
()
}
if
v
.
Kind
()
==
reflect
.
Map
{
switch
v
.
Type
()
.
Key
()
.
Kind
()
{
case
reflect
.
String
:
return
v
.
MapIndex
(
reflect
.
ValueOf
(
keySlice
[
len
(
keySlice
)
-
1
]))
.
Interface
(),
nil
default
:
return
nil
,
newUnsupportedTypePathError
(
keyWithDots
,
v
.
Type
())
}
}
// non-supporter type at the top of the path
if
v
.
Kind
()
!=
reflect
.
Struct
{
return
nil
,
newUnsupportedTypeAtTopOfPathError
(
keyWithDots
,
v
.
Type
())
}
v
=
v
.
FieldByName
(
keySlice
[
len
(
keySlice
)
-
1
])
if
!
v
.
IsValid
()
{
return
nil
,
newInvalidPathError
(
keyWithDots
)
}
return
v
.
Interface
(),
nil
}
This diff is collapsed.
Click to expand it.
get_test.go
View file @
c4f15f6c
...
...
@@ -8,6 +8,25 @@ import (
"testing"
)
func
TestGetIndexFromArray
(
t
*
testing
.
T
)
{
m
:=
map
[
string
]
any
{
"A"
:
"true"
,
"B"
:
[]
string
{
"1"
,
"2"
,
"3"
,
},
}
v
,
err
:=
GetValue
[
map
[
string
]
any
](
m
,
"B.1"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
assert
.
Equal
(
t
,
"2"
,
v
)
}
func
TestGetValueFrom
(
t
*
testing
.
T
)
{
m
:=
map
[
string
]
string
{
...
...
This diff is collapsed.
Click to expand it.
release.json
View file @
c4f15f6c
{
"version"
:
"0.
4
.0"
}
{
"version"
:
"0.
5
.0"
}
This diff is collapsed.
Click to expand it.