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
bf7301b9
Verified
Commit
bf7301b9
authored
2 years ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
refactor change function signatur
parent
dc222667
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
README.md
+12
-0
12 additions, 0 deletions
README.md
pathfind.go
+2
-2
2 additions, 2 deletions
pathfind.go
pathfind_test.go
+26
-26
26 additions, 26 deletions
pathfind_test.go
with
40 additions
and
28 deletions
README.md
+
12
−
0
View file @
bf7301b9
...
...
@@ -19,7 +19,19 @@ go get gitlab.schukai.com/oss/libraries/go/utilities/pathfinder
## Usage
### Set values
```
go
s
:=
&
StructA
{}
err
:=
GetValue
[
*
StructA
](
s
,
"my.key"
)
```
### Get values
```
go
s
:=
&
StructA
{}
err
:=
SetValue
[
*
StructA
](
s
,
"my.key"
,
"value"
)
```
## Contributing
...
...
This diff is collapsed.
Click to expand it.
pathfind.go
+
2
−
2
View file @
bf7301b9
...
...
@@ -11,7 +11,7 @@ import (
)
// This function returns the value of a field in a struct, given a path to the field.
func
GetValue
From
[
D
any
](
obj
D
,
keyWithDots
string
)
(
any
,
error
)
{
func
GetValue
[
D
any
](
obj
D
,
keyWithDots
string
)
(
any
,
error
)
{
keySlice
:=
strings
.
Split
(
keyWithDots
,
"."
)
v
:=
reflect
.
ValueOf
(
obj
)
...
...
@@ -50,7 +50,7 @@ func GetValueFrom[D any](obj D, keyWithDots string) (any, error) {
}
// This function sets the value of a field in a struct, given a path to the field.
func
SetValue
UsingPath
[
D
any
](
obj
D
,
keyWithDots
string
,
newValue
any
)
error
{
func
SetValue
[
D
any
](
obj
D
,
keyWithDots
string
,
newValue
any
)
error
{
keySlice
:=
strings
.
Split
(
keyWithDots
,
"."
)
v
:=
reflect
.
ValueOf
(
obj
)
...
...
This diff is collapsed.
Click to expand it.
pathfind_test.go
+
26
−
26
View file @
bf7301b9
...
...
@@ -32,7 +32,7 @@ func TestPathFindError(t *testing.T) {
s
:=
PathfindTestStruct1
{}
_
,
err
:=
GetValue
From
[
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Sub3.XX"
)
_
,
err
:=
GetValue
[
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Sub3.XX"
)
if
err
==
nil
{
t
.
Error
(
"err == nil"
)
}
...
...
@@ -58,7 +58,7 @@ func TestPathFindSetValueString(t *testing.T) {
for
k
,
v
:=
range
testData
{
s
:=
&
PathfindTestStruct1
{}
err
:=
SetValue
UsingPath
[
*
PathfindTestStruct1
](
s
,
k
,
v
)
err
:=
SetValue
[
*
PathfindTestStruct1
](
s
,
k
,
v
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
...
...
@@ -74,7 +74,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
s
.
Sub1
.
Bs
=
"3"
s
.
Sub1
.
Bf
=
4.0
v
,
err
:=
GetValue
From
[
PathfindTestStruct1
](
s
,
"Sub1.B"
)
v
,
err
:=
GetValue
[
PathfindTestStruct1
](
s
,
"Sub1.B"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
...
...
@@ -83,7 +83,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t
.
Error
(
"v != true"
)
}
v
,
err
=
GetValue
From
[
PathfindTestStruct1
](
s
,
"Sub1.Bi"
)
v
,
err
=
GetValue
[
PathfindTestStruct1
](
s
,
"Sub1.Bi"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
...
...
@@ -92,7 +92,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t
.
Error
(
"v != 2"
)
}
v
,
err
=
GetValue
From
[
PathfindTestStruct1
](
s
,
"Sub1.Bs"
)
v
,
err
=
GetValue
[
PathfindTestStruct1
](
s
,
"Sub1.Bs"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
...
...
@@ -101,7 +101,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t
.
Error
(
"v != 3"
)
}
v
,
err
=
GetValue
From
[
PathfindTestStruct1
](
s
,
"Sub1.Bf"
)
v
,
err
=
GetValue
[
PathfindTestStruct1
](
s
,
"Sub1.Bf"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
...
...
@@ -115,7 +115,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
s
.
Sub1
.
Sub2
.
Cs
=
"3"
s
.
Sub1
.
Sub2
.
Cf
=
4.0
v
,
err
=
GetValue
From
[
PathfindTestStruct1
](
s
,
"Sub1.Sub2.C"
)
v
,
err
=
GetValue
[
PathfindTestStruct1
](
s
,
"Sub1.Sub2.C"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
...
...
@@ -124,7 +124,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t
.
Error
(
"v != true"
)
}
v
,
err
=
GetValue
From
[
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Ci"
)
v
,
err
=
GetValue
[
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Ci"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
...
...
@@ -133,7 +133,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t
.
Error
(
"v != 2"
)
}
v
,
err
=
GetValue
From
[
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Cs"
)
v
,
err
=
GetValue
[
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Cs"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
...
...
@@ -142,7 +142,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t
.
Error
(
"v != 3"
)
}
v
,
err
=
GetValue
From
[
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Cf"
)
v
,
err
=
GetValue
[
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Cf"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
...
...
@@ -156,7 +156,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
s
.
Sub1
.
Sub2
.
Sub3
.
Ds
=
"3"
s
.
Sub1
.
Sub2
.
Sub3
.
Df
=
4.0
v
,
err
=
GetValue
From
[
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Sub3.D"
)
v
,
err
=
GetValue
[
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Sub3.D"
)
if
err
!=
nil
{
t
.
Error
(
err
)
...
...
@@ -166,7 +166,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t
.
Error
(
"v != true"
)
}
v
,
err
=
GetValue
From
[
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Sub3.Di"
)
v
,
err
=
GetValue
[
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Sub3.Di"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
...
...
@@ -175,7 +175,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t
.
Error
(
"v != 2"
)
}
v
,
err
=
GetValue
From
[
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Sub3.Ds"
)
v
,
err
=
GetValue
[
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Sub3.Ds"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
...
...
@@ -184,7 +184,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t
.
Error
(
"v != 3"
)
}
v
,
err
=
GetValue
From
[
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Sub3.Df"
)
v
,
err
=
GetValue
[
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Sub3.Df"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
...
...
@@ -198,10 +198,10 @@ func TestPathFindGetValueFrom(t *testing.T) {
func
TestPathFindSetValueFrom
(
t
*
testing
.
T
)
{
s
:=
&
PathfindTestStruct1
{}
SetValue
UsingPath
[
*
PathfindTestStruct1
](
s
,
"Sub1.B"
,
"true"
)
SetValue
UsingPath
[
*
PathfindTestStruct1
](
s
,
"Sub1.Bi"
,
"2"
)
SetValue
UsingPath
[
*
PathfindTestStruct1
](
s
,
"Sub1.Bs"
,
"3"
)
SetValue
UsingPath
[
*
PathfindTestStruct1
](
s
,
"Sub1.Bf"
,
"4.0"
)
SetValue
[
*
PathfindTestStruct1
](
s
,
"Sub1.B"
,
"true"
)
SetValue
[
*
PathfindTestStruct1
](
s
,
"Sub1.Bi"
,
"2"
)
SetValue
[
*
PathfindTestStruct1
](
s
,
"Sub1.Bs"
,
"3"
)
SetValue
[
*
PathfindTestStruct1
](
s
,
"Sub1.Bf"
,
"4.0"
)
if
s
.
Sub1
.
B
!=
true
{
t
.
Error
(
"s.Sub1.B != true"
)
...
...
@@ -220,10 +220,10 @@ func TestPathFindSetValueFrom(t *testing.T) {
t
.
Error
(
"s.Sub1.Bf != 4.0"
)
}
SetValue
UsingPath
[
*
PathfindTestStruct1
](
s
,
"Sub1.Sub2.C"
,
"true"
)
SetValue
UsingPath
[
*
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Ci"
,
"2"
)
SetValue
UsingPath
[
*
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Cs"
,
"3"
)
SetValue
UsingPath
[
*
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Cf"
,
"4.0"
)
SetValue
[
*
PathfindTestStruct1
](
s
,
"Sub1.Sub2.C"
,
"true"
)
SetValue
[
*
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Ci"
,
"2"
)
SetValue
[
*
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Cs"
,
"3"
)
SetValue
[
*
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Cf"
,
"4.0"
)
if
s
.
Sub1
.
Sub2
.
C
!=
true
{
t
.
Error
(
"s.Sub1.Sub2.C != true"
)
...
...
@@ -250,10 +250,10 @@ func TestPathFindSetValueFrom(t *testing.T) {
}
SetValue
UsingPath
[
*
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Sub3.D"
,
"true"
)
SetValue
UsingPath
[
*
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Sub3.Di"
,
"2"
)
SetValue
UsingPath
[
*
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Sub3.Ds"
,
"3"
)
SetValue
UsingPath
[
*
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Sub3.Df"
,
"4.0"
)
SetValue
[
*
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Sub3.D"
,
"true"
)
SetValue
[
*
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Sub3.Di"
,
"2"
)
SetValue
[
*
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Sub3.Ds"
,
"3"
)
SetValue
[
*
PathfindTestStruct1
](
s
,
"Sub1.Sub2.Sub3.Df"
,
"4.0"
)
if
s
.
Sub1
.
Sub2
.
Sub3
.
D
!=
true
{
t
.
Error
(
"s.Sub1.Sub2.Sub3.D != true"
)
...
...
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