GetEntityPtrs test added
This commit is contained in:
parent
72c5d83108
commit
b42cc5c811
@ -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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
39
reflect/extract_by_tags_test.go
Normal file
39
reflect/extract_by_tags_test.go
Normal 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")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user