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
77af24d7
Verified
Commit
77af24d7
authored
1 year ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
feat: set support slieces
#8
parent
b88fef16
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_7_test.go
+45
-0
45 additions, 0 deletions
issue_7_test.go
set.go
+13
-0
13 additions, 0 deletions
set.go
with
58 additions
and
0 deletions
issue_7_test.go
+
45
−
0
View file @
77af24d7
...
...
@@ -45,3 +45,48 @@ func TestSetValue(t *testing.T) {
assert
.
Equal
(
t
,
v
,
nv
)
}
type
Issue7Routing
struct
{
P
PathValue
`json:"p" yaml:"p"`
X
string
`json:"x" yaml:"x"`
}
type
Issue7Server
struct
{
Routing
[]
Issue7Routing
`json:"routing" yaml:"routing"`
}
type
Issue7Config
struct
{
Server
Issue7Server
`json:"server" yaml:"server"`
}
func
TestPathRewrite
(
t
*
testing
.
T
)
{
obj
:=
Issue7Config
{
Server
:
Issue7Server
{
Routing
:
[]
Issue7Routing
{
{
P
:
"./test"
,
X
:
"testX"
,
},
},
},
}
v
,
err
:=
GetValue
[
*
Issue7Config
](
&
obj
,
"Server.Routing.0.P"
)
assert
.
Nil
(
t
,
err
)
assert
.
Equal
(
t
,
v
,
PathValue
(
"./test"
))
nv
:=
PathValue
(
"newValue"
)
err
=
SetValue
[
*
Issue7Config
](
&
obj
,
"Server.Routing.0.P"
,
nv
)
assert
.
Nil
(
t
,
err
)
if
obj
.
Server
.
Routing
[
0
]
.
P
!=
"newValue"
{
t
.
Fatalf
(
"expected newValue, got: %s"
,
obj
.
Server
.
Routing
[
0
]
.
P
)
}
v
,
err
=
GetValue
[
*
Issue7Config
](
&
obj
,
"Server.Routing.0.P"
)
assert
.
Nil
(
t
,
err
)
assert
.
Equal
(
t
,
v
,
nv
)
}
This diff is collapsed.
Click to expand it.
set.go
+
13
−
0
View file @
77af24d7
...
...
@@ -85,6 +85,19 @@ func SetValue[D any](obj D, keyWithDots string, newValue any) error {
switch
v
.
Elem
()
.
Kind
()
{
case
reflect
.
Struct
:
v
=
v
.
Elem
()
.
FieldByName
(
key
)
case
reflect
.
Slice
:
// index is a number and get v from slice with index
index
,
err
:=
strconv
.
Atoi
(
key
)
if
err
!=
nil
{
return
newInvalidPathError
(
keyWithDots
)
}
if
index
>=
v
.
Elem
()
.
Len
()
{
return
newInvalidPathError
(
keyWithDots
)
}
v
=
v
.
Elem
()
.
Index
(
index
)
default
:
return
newUnsupportedTypePathError
(
keyWithDots
,
v
.
Type
())
}
...
...
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