GetEntityPtrs test added

This commit is contained in:
Dmitry Anderson 2024-11-12 11:34:15 +01:00
parent 72c5d83108
commit b42cc5c811
2 changed files with 40 additions and 2 deletions

View File

@ -19,8 +19,7 @@ func GetEntityPtrs(dstItem any, tag string) (dstItemPtrsMap map[string]any, err
field := t.Field(i) field := t.Field(i)
dbTag := field.Tag.Get(tag) dbTag := field.Tag.Get(tag)
if dbTag != "" { if dbTag != "" {
valueField := v.Field(i).Addr().Interface() dstItemPtrsMap[dbTag] = v.Field(i).Addr().Interface()
dstItemPtrsMap[dbTag] = &valueField
} }
} }

View File

@ -0,0 +1,39 @@
package reflectUtils
import "testing"
type TestStruct struct {
TestVal1 string `test:"test1"`
TestVal2 string `test:"test2"`
TestVal3 string `test:"test3"`
}
func TestGetEntityPtrs(t *testing.T) {
dst := &TestStruct{
TestVal1: "test1",
TestVal2: "test2",
TestVal3: "test3",
}
ptrsMap, err := GetEntityPtrs(dst, "test")
if err != nil {
t.Fatal(err.Error())
}
valsCounter := 0
for tag, ptr := range ptrsMap {
if ptr == nil {
t.Fatalf("unexpected nil pointer in a structure")
return
}
val, ok := ptr.(*string)
if !ok {
t.Fatal("failed to extract ptr")
} else if *val != tag {
t.Fatalf("unecpected value: expected test, but got %s", *val)
}
valsCounter++
}
if valsCounter != 3 {
t.Fatalf("some values are missing")
}
}