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
6b61bc3a
Verified
Commit
6b61bc3a
authored
1 year ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
fix: set a string of a derived type
#6
parent
85d91e1d
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
+40
-0
40 additions, 0 deletions
issue_7_test.go
set.go
+17
-3
17 additions, 3 deletions
set.go
with
57 additions
and
3 deletions
issue_7_test.go
0 → 100644
+
40
−
0
View file @
6b61bc3a
package
pathfinder
import
(
"testing"
)
//// Ihre Fehlerdefinitionen
//func newInvalidPathError(path string) error {
// return fmt.Errorf("invalid path: %s", path)
//}
//
//func newCannotSetError(path string) error {
// return fmt.Errorf("cannot set: %s", path)
//}
// ... weitere Fehlerdefinitionen
// Test für SetValue
func
TestSetValue
(
t
*
testing
.
T
)
{
type
Inner
struct
{
Field
PathValue
}
type
Outer
struct
{
InnerField
*
Inner
}
obj
:=
&
Outer
{
InnerField
:
&
Inner
{
Field
:
"oldValue"
,
},
}
err
:=
SetValue
(
obj
,
"InnerField.Field"
,
PathValue
(
"newValue"
))
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
if
obj
.
InnerField
.
Field
!=
"newValue"
{
t
.
Fatalf
(
"expected newValue, got: %s"
,
obj
.
InnerField
.
Field
)
}
}
This diff is collapsed.
Click to expand it.
set.go
+
17
−
3
View file @
6b61bc3a
...
...
@@ -34,7 +34,7 @@ func SetValue[D any](obj D, keyWithDots string, newValue any) error {
for
keyIndex
,
key
:=
range
keySlice
[
0
:
len
(
keySlice
)
-
1
]
{
if
v
.
Kind
()
==
reflect
.
Map
{
if
v
.
IsNil
()
{
return
newInvalidPathError
(
keyWithDots
)
}
...
...
@@ -160,8 +160,22 @@ func SetValue[D any](obj D, keyWithDots string, newValue any) error {
switch
v
.
Kind
()
{
case
reflect
.
String
:
if
newValueKind
==
reflect
.
String
{
v
.
SetString
(
newValue
.
(
string
))
if
v
.
Kind
()
==
reflect
.
Ptr
||
v
.
Kind
()
==
reflect
.
Interface
{
if
v
.
Elem
()
.
CanSet
()
&&
v
.
Elem
()
.
Kind
()
==
reflect
.
String
{
if
newValueKind
==
reflect
.
String
{
v
.
Elem
()
.
SetString
(
newValue
.
(
string
))
}
else
{
v
.
Elem
()
.
SetString
(
fmt
.
Sprintf
(
"%v"
,
newValue
))
}
}
}
else
if
newValueKind
==
reflect
.
String
{
if
reflect
.
TypeOf
(
newValue
)
.
ConvertibleTo
(
reflect
.
TypeOf
(
""
))
{
newValueString
:=
reflect
.
ValueOf
(
newValue
)
.
Convert
(
reflect
.
TypeOf
(
""
))
.
Interface
()
.
(
string
)
v
.
SetString
(
newValueString
)
}
else
{
return
newUnsupportedTypePathError
(
keyWithDots
,
v
.
Type
())
}
}
else
{
v
.
SetString
(
fmt
.
Sprintf
(
"%v"
,
newValue
))
}
...
...
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