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
b2d96812
Verified
Commit
b2d96812
authored
2 years ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
fix secure access to structure with a constraint
parent
39dc25f9
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
get.go
+51
-0
51 additions, 0 deletions
get.go
set.go
+3
-39
3 additions, 39 deletions
set.go
set_test.go
+0
-0
0 additions, 0 deletions
set_test.go
with
54 additions
and
39 deletions
get.go
0 → 100644
+
51
−
0
View file @
b2d96812
// Copyright 2022 schukai GmbH
// SPDX-License-Identifier: AGPL-3.0
package
pathfinder
import
(
"reflect"
"strings"
)
// This function returns the value of a field in a struct, given a path to the field.
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
)
}
v
=
v
.
Elem
()
}
if
v
.
Kind
()
!=
reflect
.
Struct
{
return
nil
,
newUnsupportedTypePathError
(
keyWithDots
,
v
.
Type
())
}
v
=
v
.
FieldByName
(
key
)
}
if
v
.
Kind
()
==
reflect
.
Invalid
{
return
nil
,
newInvalidPathError
(
keyWithDots
)
}
for
v
.
Kind
()
==
reflect
.
Ptr
{
v
=
v
.
Elem
()
}
// 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.
pathfind
.go
→
set
.go
+
3
−
39
View file @
b2d96812
...
...
@@ -10,45 +10,6 @@ import (
"strings"
)
// This function returns the value of a field in a struct, given a path to the field.
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
{
v
=
v
.
Elem
()
}
if
v
.
Kind
()
!=
reflect
.
Struct
{
return
nil
,
newUnsupportedTypePathError
(
keyWithDots
,
v
.
Type
())
}
v
=
v
.
FieldByName
(
key
)
}
if
v
.
Kind
()
==
reflect
.
Invalid
{
return
nil
,
newInvalidPathError
(
keyWithDots
)
}
for
v
.
Kind
()
==
reflect
.
Ptr
{
v
=
v
.
Elem
()
}
// 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 function sets the value of a field in a struct, given a path to the field.
func
SetValue
[
D
any
](
obj
D
,
keyWithDots
string
,
newValue
any
)
error
{
...
...
@@ -57,6 +18,9 @@ func SetValue[D any](obj D, keyWithDots string, newValue any) error {
for
_
,
key
:=
range
keySlice
[
0
:
len
(
keySlice
)
-
1
]
{
for
v
.
Kind
()
!=
reflect
.
Ptr
{
if
v
.
Kind
()
==
reflect
.
Invalid
{
return
newInvalidPathError
(
keyWithDots
)
}
v
=
v
.
Addr
()
}
...
...
This diff is collapsed.
Click to expand it.
pathfind
_test.go
→
set
_test.go
+
0
−
0
View file @
b2d96812
File moved
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