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 ...@@ -19,7 +19,19 @@ go get gitlab.schukai.com/oss/libraries/go/utilities/pathfinder
## Usage ## 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 ## Contributing
......
...@@ -11,7 +11,7 @@ import ( ...@@ -11,7 +11,7 @@ import (
) )
// This function returns the value of a field in a struct, given a path to the field. // 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, ".") keySlice := strings.Split(keyWithDots, ".")
v := reflect.ValueOf(obj) v := reflect.ValueOf(obj)
...@@ -50,7 +50,7 @@ func GetValueFrom[D any](obj D, keyWithDots string) (any, error) { ...@@ -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. // 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, ".") keySlice := strings.Split(keyWithDots, ".")
v := reflect.ValueOf(obj) v := reflect.ValueOf(obj)
......
...@@ -32,7 +32,7 @@ func TestPathFindError(t *testing.T) { ...@@ -32,7 +32,7 @@ func TestPathFindError(t *testing.T) {
s := PathfindTestStruct1{} s := PathfindTestStruct1{}
_, err := GetValueFrom[PathfindTestStruct1](s, "Sub1.Sub2.Sub3.XX") _, err := GetValue[PathfindTestStruct1](s, "Sub1.Sub2.Sub3.XX")
if err == nil { if err == nil {
t.Error("err == nil") t.Error("err == nil")
} }
...@@ -58,7 +58,7 @@ func TestPathFindSetValueString(t *testing.T) { ...@@ -58,7 +58,7 @@ func TestPathFindSetValueString(t *testing.T) {
for k, v := range testData { for k, v := range testData {
s := &PathfindTestStruct1{} s := &PathfindTestStruct1{}
err := SetValueUsingPath[*PathfindTestStruct1](s, k, v) err := SetValue[*PathfindTestStruct1](s, k, v)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
...@@ -74,7 +74,7 @@ func TestPathFindGetValueFrom(t *testing.T) { ...@@ -74,7 +74,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
s.Sub1.Bs = "3" s.Sub1.Bs = "3"
s.Sub1.Bf = 4.0 s.Sub1.Bf = 4.0
v, err := GetValueFrom[PathfindTestStruct1](s, "Sub1.B") v, err := GetValue[PathfindTestStruct1](s, "Sub1.B")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
...@@ -83,7 +83,7 @@ func TestPathFindGetValueFrom(t *testing.T) { ...@@ -83,7 +83,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t.Error("v != true") t.Error("v != true")
} }
v, err = GetValueFrom[PathfindTestStruct1](s, "Sub1.Bi") v, err = GetValue[PathfindTestStruct1](s, "Sub1.Bi")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
...@@ -92,7 +92,7 @@ func TestPathFindGetValueFrom(t *testing.T) { ...@@ -92,7 +92,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t.Error("v != 2") t.Error("v != 2")
} }
v, err = GetValueFrom[PathfindTestStruct1](s, "Sub1.Bs") v, err = GetValue[PathfindTestStruct1](s, "Sub1.Bs")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
...@@ -101,7 +101,7 @@ func TestPathFindGetValueFrom(t *testing.T) { ...@@ -101,7 +101,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t.Error("v != 3") t.Error("v != 3")
} }
v, err = GetValueFrom[PathfindTestStruct1](s, "Sub1.Bf") v, err = GetValue[PathfindTestStruct1](s, "Sub1.Bf")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
...@@ -115,7 +115,7 @@ func TestPathFindGetValueFrom(t *testing.T) { ...@@ -115,7 +115,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
s.Sub1.Sub2.Cs = "3" s.Sub1.Sub2.Cs = "3"
s.Sub1.Sub2.Cf = 4.0 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 { if err != nil {
t.Error(err) t.Error(err)
} }
...@@ -124,7 +124,7 @@ func TestPathFindGetValueFrom(t *testing.T) { ...@@ -124,7 +124,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t.Error("v != true") t.Error("v != true")
} }
v, err = GetValueFrom[PathfindTestStruct1](s, "Sub1.Sub2.Ci") v, err = GetValue[PathfindTestStruct1](s, "Sub1.Sub2.Ci")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
...@@ -133,7 +133,7 @@ func TestPathFindGetValueFrom(t *testing.T) { ...@@ -133,7 +133,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t.Error("v != 2") t.Error("v != 2")
} }
v, err = GetValueFrom[PathfindTestStruct1](s, "Sub1.Sub2.Cs") v, err = GetValue[PathfindTestStruct1](s, "Sub1.Sub2.Cs")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
...@@ -142,7 +142,7 @@ func TestPathFindGetValueFrom(t *testing.T) { ...@@ -142,7 +142,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t.Error("v != 3") t.Error("v != 3")
} }
v, err = GetValueFrom[PathfindTestStruct1](s, "Sub1.Sub2.Cf") v, err = GetValue[PathfindTestStruct1](s, "Sub1.Sub2.Cf")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
...@@ -156,7 +156,7 @@ func TestPathFindGetValueFrom(t *testing.T) { ...@@ -156,7 +156,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
s.Sub1.Sub2.Sub3.Ds = "3" s.Sub1.Sub2.Sub3.Ds = "3"
s.Sub1.Sub2.Sub3.Df = 4.0 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 { if err != nil {
t.Error(err) t.Error(err)
...@@ -166,7 +166,7 @@ func TestPathFindGetValueFrom(t *testing.T) { ...@@ -166,7 +166,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t.Error("v != true") 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 { if err != nil {
t.Error(err) t.Error(err)
} }
...@@ -175,7 +175,7 @@ func TestPathFindGetValueFrom(t *testing.T) { ...@@ -175,7 +175,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t.Error("v != 2") 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 { if err != nil {
t.Error(err) t.Error(err)
} }
...@@ -184,7 +184,7 @@ func TestPathFindGetValueFrom(t *testing.T) { ...@@ -184,7 +184,7 @@ func TestPathFindGetValueFrom(t *testing.T) {
t.Error("v != 3") 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 { if err != nil {
t.Error(err) t.Error(err)
} }
...@@ -198,10 +198,10 @@ func TestPathFindGetValueFrom(t *testing.T) { ...@@ -198,10 +198,10 @@ func TestPathFindGetValueFrom(t *testing.T) {
func TestPathFindSetValueFrom(t *testing.T) { func TestPathFindSetValueFrom(t *testing.T) {
s := &PathfindTestStruct1{} s := &PathfindTestStruct1{}
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.B", "true") SetValue[*PathfindTestStruct1](s, "Sub1.B", "true")
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Bi", "2") SetValue[*PathfindTestStruct1](s, "Sub1.Bi", "2")
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Bs", "3") SetValue[*PathfindTestStruct1](s, "Sub1.Bs", "3")
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Bf", "4.0") SetValue[*PathfindTestStruct1](s, "Sub1.Bf", "4.0")
if s.Sub1.B != true { if s.Sub1.B != true {
t.Error("s.Sub1.B != true") t.Error("s.Sub1.B != true")
...@@ -220,10 +220,10 @@ func TestPathFindSetValueFrom(t *testing.T) { ...@@ -220,10 +220,10 @@ func TestPathFindSetValueFrom(t *testing.T) {
t.Error("s.Sub1.Bf != 4.0") t.Error("s.Sub1.Bf != 4.0")
} }
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Sub2.C", "true") SetValue[*PathfindTestStruct1](s, "Sub1.Sub2.C", "true")
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Sub2.Ci", "2") SetValue[*PathfindTestStruct1](s, "Sub1.Sub2.Ci", "2")
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Sub2.Cs", "3") SetValue[*PathfindTestStruct1](s, "Sub1.Sub2.Cs", "3")
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Sub2.Cf", "4.0") SetValue[*PathfindTestStruct1](s, "Sub1.Sub2.Cf", "4.0")
if s.Sub1.Sub2.C != true { if s.Sub1.Sub2.C != true {
t.Error("s.Sub1.Sub2.C != true") t.Error("s.Sub1.Sub2.C != true")
...@@ -250,10 +250,10 @@ func TestPathFindSetValueFrom(t *testing.T) { ...@@ -250,10 +250,10 @@ func TestPathFindSetValueFrom(t *testing.T) {
} }
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Sub2.Sub3.D", "true") SetValue[*PathfindTestStruct1](s, "Sub1.Sub2.Sub3.D", "true")
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Sub2.Sub3.Di", "2") SetValue[*PathfindTestStruct1](s, "Sub1.Sub2.Sub3.Di", "2")
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Sub2.Sub3.Ds", "3") SetValue[*PathfindTestStruct1](s, "Sub1.Sub2.Sub3.Ds", "3")
SetValueUsingPath[*PathfindTestStruct1](s, "Sub1.Sub2.Sub3.Df", "4.0") SetValue[*PathfindTestStruct1](s, "Sub1.Sub2.Sub3.Df", "4.0")
if s.Sub1.Sub2.Sub3.D != true { if s.Sub1.Sub2.Sub3.D != true {
t.Error("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.
Please register or to comment