Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • oss/libraries/go/utilities/pathfinder
1 result
Select Git revision
Show changes
Commits on Source (3)
<a name="v0.4.0"></a>
## [v0.4.0] - 2022-12-17
<a name="v0.3.1"></a>
## [v0.3.1] - 2022-10-16
### Bug Fixes
......@@ -17,5 +20,6 @@
- feat takeover from other project
[v0.4.0]: https://gitlab.schukai.com/oss/libraries/go/utilities/pathfinder/compare/v0.3.1...v0.4.0
[v0.3.1]: https://gitlab.schukai.com/oss/libraries/go/utilities/pathfinder/compare/v0.3.0...v0.3.1
[v0.3.0]: https://gitlab.schukai.com/oss/libraries/go/utilities/pathfinder/compare/v0.2.0...v0.3.0
......@@ -21,7 +21,16 @@ func GetValue[D any](obj D, keyWithDots string) (any, error) {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
if v.Kind() == reflect.Map {
switch v.Type().Key().Kind() {
case reflect.String:
v = v.MapIndex(reflect.ValueOf(key)).Elem()
continue
default:
return nil, newUnsupportedTypePathError(keyWithDots, v.Type())
}
} else if v.Kind() != reflect.Struct {
return nil, newUnsupportedTypePathError(keyWithDots, v.Type())
}
......@@ -36,6 +45,15 @@ func GetValue[D any](obj D, keyWithDots string) (any, error) {
v = v.Elem()
}
if v.Kind() == reflect.Map {
switch v.Type().Key().Kind() {
case reflect.String:
return v.MapIndex(reflect.ValueOf(keySlice[len(keySlice)-1])).Interface(), nil
default:
return nil, newUnsupportedTypePathError(keyWithDots, v.Type())
}
}
// non-supporter type at the top of the path
if v.Kind() != reflect.Struct {
return nil, newUnsupportedTypeAtTopOfPathError(keyWithDots, v.Type())
......
// Copyright 2022 schukai GmbH
// SPDX-License-Identifier: AGPL-3.0
package pathfinder
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestGetValueFrom(t *testing.T) {
m := map[string]string{
"KeyA": "true",
"KeyB": "2",
"KeyC": "3",
"KeyD": "4.0",
}
v, err := GetValue[map[string]string](m, "KeyA")
if err != nil {
t.Error(err)
}
assert.Equal(t, v, "true")
}
func TestPathGetValueFromX(t *testing.T) {
m := map[string]any{
"A": "true",
"B": map[string]any{
"C": 2,
"D": map[string]any{
"E": "3",
},
},
"X": "3",
"Y": "4.0",
}
v, err := GetValue[map[string]any](m, "B.D.E")
if err != nil {
t.Error(err)
}
assert.Equal(t, "3", v)
v, err = GetValue[map[string]any](m, "B.C")
if err != nil {
t.Error(err)
}
assert.Equal(t, 2, v)
}
func TestPathGetValueFrom(t *testing.T) {
type PathfindTestStruct1 struct {
A bool
Sub1 struct {
B int
Sub2 struct {
C bool
}
D int
}
}
var testData PathfindTestStruct1
testData.A = true
testData.Sub1.B = 2
testData.Sub1.Sub2.C = true
testData.Sub1.D = 4
GetValue[PathfindTestStruct1](testData, "Sub1.B")
GetValue[PathfindTestStruct1](testData, "Sub1.Sub2.C")
GetValue[PathfindTestStruct1](testData, "Sub1.D")
}
{"version":"0.3.1"}
{"version":"0.4.0"}