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
85d91e1d
Verified
Commit
85d91e1d
authored
1 year ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
feat: set values in a map
#5
parent
4a41f7ac
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
issue_2_test.go
+33
-0
33 additions, 0 deletions
issue_2_test.go
set.go
+45
-5
45 additions, 5 deletions
set.go
with
78 additions
and
5 deletions
issue_2_test.go
+
33
−
0
View file @
85d91e1d
...
...
@@ -106,3 +106,36 @@ func TestSetValueWithArray(t *testing.T) {
assert
.
NotNil
(
t
,
err
)
}
type
PathValue
string
type
SubTestSubPaths
struct
{
Template
PathValue
Definitions
[]
PathValue
}
type
SubTest2Def
struct
{
Paths
SubTestSubPaths
}
type
SubTestStruct1
map
[
string
]
SubTest2Def
type
MainTestStruct
struct
{
Sub
SubTestStruct1
}
func
TestReplacePathForConfig
(
t
*
testing
.
T
)
{
config
:=
MainTestStruct
{
Sub
:
SubTestStruct1
{
"Default"
:
SubTest2Def
{
Paths
:
SubTestSubPaths
{
Template
:
"../../../default.html"
,
Definitions
:
[]
PathValue
{
"../../../legacy.yaml"
},
},
},
},
}
err
:=
SetValue
[
*
MainTestStruct
](
&
config
,
"Sub.Default.Paths.Template"
,
"test"
)
assert
.
Nil
(
t
,
err
)
}
This diff is collapsed.
Click to expand it.
set.go
+
45
−
5
View file @
85d91e1d
...
...
@@ -4,12 +4,26 @@
package
pathfinder
import
(
"bytes"
"encoding/gob"
"fmt"
"reflect"
"strconv"
"strings"
)
func
deepCopy
(
src
,
dst
interface
{})
error
{
var
buf
bytes
.
Buffer
enc
:=
gob
.
NewEncoder
(
&
buf
)
dec
:=
gob
.
NewDecoder
(
&
buf
)
if
err
:=
enc
.
Encode
(
src
);
err
!=
nil
{
return
err
}
return
dec
.
Decode
(
dst
)
}
// SetValue sets the value of a field in a struct, given a path to the field.
// The object must be a pointer to a struct, otherwise an error is returned.
func
SetValue
[
D
any
](
obj
D
,
keyWithDots
string
,
newValue
any
)
error
{
...
...
@@ -17,7 +31,33 @@ func SetValue[D any](obj D, keyWithDots string, newValue any) error {
keySlice
:=
strings
.
Split
(
keyWithDots
,
"."
)
v
:=
reflect
.
ValueOf
(
obj
)
for
_
,
key
:=
range
keySlice
[
0
:
len
(
keySlice
)
-
1
]
{
for
keyIndex
,
key
:=
range
keySlice
[
0
:
len
(
keySlice
)
-
1
]
{
if
v
.
Kind
()
==
reflect
.
Map
{
if
v
.
IsNil
()
{
return
newInvalidPathError
(
keyWithDots
)
}
currentValue
:=
v
.
MapIndex
(
reflect
.
ValueOf
(
key
))
.
Interface
()
newValueCopy
:=
reflect
.
New
(
reflect
.
TypeOf
(
currentValue
))
.
Interface
()
if
err
:=
deepCopy
(
currentValue
,
newValueCopy
);
err
!=
nil
{
return
err
}
newValueCopyPtr
:=
&
newValueCopy
newValueCopyReflect
:=
reflect
.
ValueOf
(
newValueCopyPtr
)
.
Elem
()
if
!
newValueCopyReflect
.
CanAddr
()
{
return
newCannotSetError
(
"Wert ist nicht adressierbar"
)
}
newKey
:=
strings
.
Join
(
keySlice
[
keyIndex
+
1
:
],
"."
)
return
SetValue
(
newValueCopyPtr
,
newKey
,
newValue
)
}
if
v
.
Kind
()
==
reflect
.
Ptr
&&
v
.
Elem
()
.
Kind
()
==
reflect
.
Interface
{
v
=
v
.
Elem
()
.
Elem
()
}
for
v
.
Kind
()
!=
reflect
.
Ptr
{
if
v
.
Kind
()
==
reflect
.
Invalid
{
return
newInvalidPathError
(
keyWithDots
)
...
...
@@ -35,13 +75,13 @@ func SetValue[D any](obj D, keyWithDots string, newValue any) error {
return
newUnsupportedTypePathError
(
keyWithDots
,
v
.
Type
())
}
elem
:=
v
.
Elem
()
if
elem
.
Kind
()
!=
reflect
.
Struct
{
switch
v
.
Elem
()
.
Kind
()
{
case
reflect
.
Struct
:
v
=
v
.
Elem
()
.
FieldByName
(
key
)
default
:
return
newUnsupportedTypePathError
(
keyWithDots
,
v
.
Type
())
}
v
=
elem
.
FieldByName
(
key
)
}
if
v
.
Kind
()
==
reflect
.
Invalid
{
...
...
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