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
c768f8a0
Verified
Commit
c768f8a0
authored
1 year ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
fix: set interface do not work
#14
parent
3780c619
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_14_test.go
+25
-2
25 additions, 2 deletions
issue_14_test.go
set.go
+7
-46
7 additions, 46 deletions
set.go
with
32 additions
and
48 deletions
issue_14_test.go
+
25
−
2
View file @
c768f8a0
package
pathfinder
import
(
"github.com/stretchr/testify/assert"
"testing"
)
func
TestPathFindSetValueFrom14
(
t
*
testing
.
T
)
{
type
DeepNestedStruct
struct
{
A
PathValue
}
type
MyStruct
struct
{
I
map
[
string
]
DeepNestedStruct
}
func
TestReplacePath
(
t
*
testing
.
T
)
{
// Sample seed data to guide the fuzzer
s
:=
&
MyStruct
{
I
:
map
[
string
]
DeepNestedStruct
{
"key1GI"
:
{
A
:
"rel/pathGIA"
,
},
},
}
err
:=
SetValue
[
*
MyStruct
](
s
,
"I.key1GI.A"
,
"new/pathGIA"
)
assert
.
Nil
(
t
,
err
)
assert
.
Equal
(
t
,
PathValue
(
"new/pathGIA"
),
s
.
I
[
"key1GI"
]
.
A
)
}
This diff is collapsed.
Click to expand it.
set.go
+
7
−
46
View file @
c768f8a0
...
...
@@ -51,6 +51,7 @@ func SetValue[D any](obj D, keyWithDots string, newValue any) error {
return
newCannotSetError
(
"Wert ist nicht adressierbar"
)
}
newKey
:=
strings
.
Join
(
keySlice
[
keyIndex
+
1
:
],
"."
)
err
:=
SetValue
(
newValueCopyPtr
,
newKey
,
newValue
)
if
err
!=
nil
{
return
err
...
...
@@ -146,58 +147,13 @@ func SetValue[D any](obj D, keyWithDots string, newValue any) error {
m
.
SetMapIndex
(
keyValConverted
,
newValConverted
)
return
nil
//currentValue := reflectionOfObject.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:], ".")
////err := SetValue(newValueCopyPtr, newKey, newValue)
////if err != nil {
//// return err
////}
//
//reflectionOfObject.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(newValueCopy).Elem())
//return nil
//if reflectionOfObject.IsNil() {
// return newInvalidPathError(keyWithDots)
//}
//
//index := keySlice[len(keySlice)-1]
//reflectedIndex := reflect.ValueOf(index)
//
//if !reflectedIndex.Type().AssignableTo(reflectionOfObject.Type().Key()) {
// return newInvalidPathError(keyWithDots)
//}
//
//currentValue := reflectionOfObject.MapIndex(reflectedIndex).Interface()
//newValueCopy := reflect.New(reflect.TypeOf(currentValue)).Interface()
//if err := deepCopy(currentValue, newValueCopy); err != nil {
// return err
//}
//
//if !reflect.ValueOf(newValueCopy).Elem().Type().AssignableTo(reflectionOfObject.Type().Elem()) {
// return newInvalidPathError(keyWithDots)
//}
//
//newValueCopyX := reflect.ValueOf(newValueCopy).Elem()
//reflectionOfObject.SetMapIndex(reflectedIndex, newValueCopyX)
case
reflect
.
Slice
:
// index is a number and get reflectionOfObject from slice with index
index
,
err
:=
strconv
.
Atoi
(
keySlice
[
len
(
keySlice
)
-
1
])
if
err
!=
nil
{
return
newInvalidPathError
(
keyWithDots
)
}
// index out of range
if
index
>=
reflectionOfObject
.
Len
()
{
return
newInvalidPathError
(
keyWithDots
)
}
...
...
@@ -214,7 +170,12 @@ func SetValue[D any](obj D, keyWithDots string, newValue any) error {
}
return
nil
case
reflect
.
Interface
:
return
newUnsupportedTypeAtTopOfPathError
(
keyWithDots
,
reflectionOfObject
.
Type
())
// check if reflectionOfObject is an interface to an struct pointer
if
reflectionOfObject
.
Elem
()
.
Kind
()
==
reflect
.
Ptr
&&
reflectionOfObject
.
Elem
()
.
Elem
()
.
Kind
()
==
reflect
.
Struct
{
return
SetValue
(
reflectionOfObject
.
Elem
()
.
Interface
(),
keySlice
[
len
(
keySlice
)
-
1
],
newValue
)
}
case
reflect
.
Chan
:
return
newUnsupportedTypeAtTopOfPathError
(
keyWithDots
,
reflectionOfObject
.
Type
())
case
reflect
.
Func
:
...
...
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