use type instead of value for Reflection in Coll.initFields

This commit is contained in:
2023-09-12 10:47:41 +02:00
parent 29a3f73f15
commit 11dc6d2640
2 changed files with 38 additions and 19 deletions

View File

@@ -23,6 +23,9 @@ func TestReflectionGetFieldType(t *testing.T) {
Sub struct {
A string `bson:"a"`
} `bson:"sub"`
SubPtr *struct {
A string `bson:"a"`
} `bson:"subPtr"`
Str string `bson:"str"`
Ptr *int `bson:"ptr"`
MDate rfctime.RFC3339NanoTime `bson:"mdate"`
@@ -43,6 +46,11 @@ func TestReflectionGetFieldType(t *testing.T) {
}{
A: "2",
},
SubPtr: &struct {
A string `bson:"a"`
}{
A: "4",
},
Str: "3",
Ptr: langext.Ptr(4),
MDate: t1,
@@ -82,6 +90,12 @@ func TestReflectionGetFieldType(t *testing.T) {
tst.AssertEqual(t, gft("sub.a").IsPointer, false)
tst.AssertEqual(t, gfv("sub.a").(string), "2")
tst.AssertEqual(t, gft("subPtr.a").Kind.String(), "string")
tst.AssertEqual(t, gft("subPtr.a").Type.String(), "string")
tst.AssertEqual(t, gft("subPtr.a").Name, "A")
tst.AssertEqual(t, gft("subPtr.a").IsPointer, false)
tst.AssertEqual(t, gfv("subPtr.a").(string), "4")
tst.AssertEqual(t, gft("str").Kind.String(), "string")
tst.AssertEqual(t, gft("str").Type.String(), "string")
tst.AssertEqual(t, gft("str").Name, "Str")
@@ -105,6 +119,9 @@ func TestReflectionGetTokenValueAsMongoType(t *testing.T) {
Sub struct {
A string `bson:"a"`
} `bson:"sub"`
SubPtr struct {
A string `bson:"a"`
} `bson:"subPtr"`
Str string `bson:"str"`
Ptr *int `bson:"ptr"`
Num int `bson:"num"`
@@ -130,6 +147,8 @@ func TestReflectionGetTokenValueAsMongoType(t *testing.T) {
}
tst.AssertEqual(t, gtvasmt("hello", "str").(string), "hello")
tst.AssertEqual(t, gtvasmt("hello", "sub.a").(string), "hello")
tst.AssertEqual(t, gtvasmt("hello", "subPtr.a").(string), "hello")
tst.AssertEqual(t, gtvasmt("4", "num").(int), 4)
tst.AssertEqual(t, gtvasmt("asdf", "_id").(IDType), "asdf")
tst.AssertEqual(t, gtvasmt("", "ptr").(*int), nil)