diff --git a/README.md b/README.md index a7c0c493b1b79edf93da01eadae5e9f4331b72e1..4d1522c15b54f8c311a7cedeb5c6bfad357b9053 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/pathfind.go b/pathfind.go index 53cc13608f0910961b92677862c46da700dda97e..28adede2ac502ba6efe9a196dd916cec3ab4a44a 100644 --- a/pathfind.go +++ b/pathfind.go @@ -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) diff --git a/pathfind_test.go b/pathfind_test.go index 5f38cb0da3a723f87f35c81eac0cd056557b820e..37adcb7f17068bc574fcffecfb4973e35bb9139b 100644 --- a/pathfind_test.go +++ b/pathfind_test.go @@ -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")