Skip to content
Snippets Groups Projects
Verified Commit bf7301b9 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

refactor change function signatur

parent dc222667
No related branches found
No related tags found
No related merge requests found
......@@ -19,7 +19,19 @@ go get gitlab.schukai.com/oss/libraries/go/utilities/pathfinder
## Usage
### Set values
```go
s := &StructA{}
err := GetValue[*StructA](s, "my.key")
```
### Get values
```go
s := &StructA{}
err := SetValue[*StructA](s, "my.key", "value")
```
## Contributing
......
......@@ -11,7 +11,7 @@ import (
)
// This function returns the value of a field in a struct, given a path to the field.
func GetValueFrom[D any](obj D, keyWithDots string) (any, error) {
func GetValue[D any](obj D, keyWithDots string) (any, error) {
keySlice := strings.Split(keyWithDots, ".")
v := reflect.ValueOf(obj)
......@@ -50,7 +50,7 @@ func GetValueFrom[D any](obj D, keyWithDots string) (any, error) {
}
// This function sets the value of a field in a struct, given a path to the field.
func SetValueUsingPath[D any](obj D, keyWithDots string, newValue any) error {
func SetValue[D any](obj D, keyWithDots string, newValue any) error {
keySlice := strings.Split(keyWithDots, ".")
v := reflect.ValueOf(obj)
......
......@@ -32,7 +32,7 @@ func TestPathFindError(t *testing.T) {
s := PathfindTestStruct1{}
_, err := GetValueFrom[PathfindTestStruct1](s, "Sub1.Sub2.Sub3.XX")
_, err := GetValue[PathfindTestStruct1](s, "Sub1.Sub2.Sub3.XX")
if err == nil {
t.Error("err == nil")
}
......@@ -58,7 +58,7 @@ func TestPathFindSetValueString(t *testing.T) {
for k, v := range testData {
s := &PathfindTestStruct1{}
err := SetValueUsingPath[*PathfindTestStruct1](s, k, v)
err := SetValue[*PathfindTestStruct1](s, k, v)
if err != nil {
t.Error(err)
}
......@@ -74,7 +74,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
s.Sub1.Bs = "3"
s.Sub1.Bf = 4.0
v, err := GetValueFrom[PathfindTestStruct1](s, "Sub1.B")
v, err := GetValue[PathfindTestStruct1](s, "Sub1.B")
if err != nil {
t.Error(err)
}
......@@ -83,7 +83,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t.Error("v != true")
}
v, err = GetValueFrom[PathfindTestStruct1](s, "Sub1.Bi")
v, err = GetValue[PathfindTestStruct1](s, "Sub1.Bi")
if err != nil {
t.Error(err)
}
......@@ -92,7 +92,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t.Error("v != 2")
}
v, err = GetValueFrom[PathfindTestStruct1](s, "Sub1.Bs")
v, err = GetValue[PathfindTestStruct1](s, "Sub1.Bs")
if err != nil {
t.Error(err)
}
......@@ -101,7 +101,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t.Error("v != 3")
}
v, err = GetValueFrom[PathfindTestStruct1](s, "Sub1.Bf")
v, err = GetValue[PathfindTestStruct1](s, "Sub1.Bf")
if err != nil {
t.Error(err)
}
......@@ -115,7 +115,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
s.Sub1.Sub2.Cs = "3"
s.Sub1.Sub2.Cf = 4.0
v, err = GetValueFrom[PathfindTestStruct1](s, "Sub1.Sub2.C")
v, err = GetValue[PathfindTestStruct1](s, "Sub1.Sub2.C")
if err != nil {
t.Error(err)
}
......@@ -124,7 +124,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t.Error("v != true")
}
v, err = GetValueFrom[PathfindTestStruct1](s, "Sub1.Sub2.Ci")
v, err = GetValue[PathfindTestStruct1](s, "Sub1.Sub2.Ci")
if err != nil {
t.Error(err)
}
......@@ -133,7 +133,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t.Error("v != 2")
}
v, err = GetValueFrom[PathfindTestStruct1](s, "Sub1.Sub2.Cs")
v, err = GetValue[PathfindTestStruct1](s, "Sub1.Sub2.Cs")
if err != nil {
t.Error(err)
}
......@@ -142,7 +142,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t.Error("v != 3")
}
v, err = GetValueFrom[PathfindTestStruct1](s, "Sub1.Sub2.Cf")
v, err = GetValue[PathfindTestStruct1](s, "Sub1.Sub2.Cf")
if err != nil {
t.Error(err)
}
......@@ -156,7 +156,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
s.Sub1.Sub2.Sub3.Ds = "3"
s.Sub1.Sub2.Sub3.Df = 4.0
v, err = GetValueFrom[PathfindTestStruct1](s, "Sub1.Sub2.Sub3.D")
v, err = GetValue[PathfindTestStruct1](s, "Sub1.Sub2.Sub3.D")
if err != nil {
t.Error(err)
......@@ -166,7 +166,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t.Error("v != true")
}
v, err = GetValueFrom[PathfindTestStruct1](s, "Sub1.Sub2.Sub3.Di")
v, err = GetValue[PathfindTestStruct1](s, "Sub1.Sub2.Sub3.Di")
if err != nil {
t.Error(err)
}
......@@ -175,7 +175,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t.Error("v != 2")
}
v, err = GetValueFrom[PathfindTestStruct1](s, "Sub1.Sub2.Sub3.Ds")
v, err = GetValue[PathfindTestStruct1](s, "Sub1.Sub2.Sub3.Ds")
if err != nil {
t.Error(err)
}
......@@ -184,7 +184,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t.Error("v != 3")
}
v, err = GetValueFrom[PathfindTestStruct1](s, "Sub1.Sub2.Sub3.Df")
v, err = GetValue[PathfindTestStruct1](s, "Sub1.Sub2.Sub3.Df")
if err != nil {
t.Error(err)
}
......@@ -198,10 +198,10 @@ func TestPathFindGetValueFrom(t *testing.T) {
func TestPathFindSetValueFrom(t *testing.T) {
s := &PathfindTestStruct1{}
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.B", "true")
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Bi", "2")
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Bs", "3")
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Bf", "4.0")
SetValue[*PathfindTestStruct1](s, "Sub1.B", "true")
SetValue[*PathfindTestStruct1](s, "Sub1.Bi", "2")
SetValue[*PathfindTestStruct1](s, "Sub1.Bs", "3")
SetValue[*PathfindTestStruct1](s, "Sub1.Bf", "4.0")
if s.Sub1.B != true {
t.Error("s.Sub1.B != true")
......@@ -220,10 +220,10 @@ func TestPathFindSetValueFrom(t *testing.T) {
t.Error("s.Sub1.Bf != 4.0")
}
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Sub2.C", "true")
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Sub2.Ci", "2")
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Sub2.Cs", "3")
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Sub2.Cf", "4.0")
SetValue[*PathfindTestStruct1](s, "Sub1.Sub2.C", "true")
SetValue[*PathfindTestStruct1](s, "Sub1.Sub2.Ci", "2")
SetValue[*PathfindTestStruct1](s, "Sub1.Sub2.Cs", "3")
SetValue[*PathfindTestStruct1](s, "Sub1.Sub2.Cf", "4.0")
if s.Sub1.Sub2.C != true {
t.Error("s.Sub1.Sub2.C != true")
......@@ -250,10 +250,10 @@ func TestPathFindSetValueFrom(t *testing.T) {
}
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Sub2.Sub3.D", "true")
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Sub2.Sub3.Di", "2")
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Sub2.Sub3.Ds", "3")
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Sub2.Sub3.Df", "4.0")
SetValue[*PathfindTestStruct1](s, "Sub1.Sub2.Sub3.D", "true")
SetValue[*PathfindTestStruct1](s, "Sub1.Sub2.Sub3.Di", "2")
SetValue[*PathfindTestStruct1](s, "Sub1.Sub2.Sub3.Ds", "3")
SetValue[*PathfindTestStruct1](s, "Sub1.Sub2.Sub3.Df", "4.0")
if s.Sub1.Sub2.Sub3.D != true {
t.Error("s.Sub1.Sub2.Sub3.D != true")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment