package wmo import ( "go.mongodb.org/mongo-driver/mongo" "gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/tst" "testing" "time" ) func TestReflectionGetFieldType(t *testing.T) { type IDType string type TestData struct { ID IDType `bson:"_id"` CDate time.Time `bson:"cdate"` Sub struct { A string `bson:"a"` } `bson:"sub"` Str string `bson:"str"` Ptr *int `bson:"ptr"` } coll := W[TestData](&mongo.Collection{}) coll.init() t0 := time.Now() d := TestData{ ID: "1", CDate: t0, Sub: struct { A string `bson:"a"` }{ A: "2", }, Str: "3", Ptr: langext.Ptr(4), } tst.AssertEqual(t, coll.getFieldType("_id").Kind.String(), "string") tst.AssertEqual(t, coll.getFieldType("_id").Type.String(), "wmo.IDType") tst.AssertEqual(t, coll.getFieldType("_id").Name, "ID") tst.AssertEqual(t, coll.getFieldType("_id").IsPointer, false) tst.AssertEqual(t, coll.getFieldValue(d, "_id").(IDType), "1") tst.AssertEqual(t, coll.getFieldType("cdate").Kind.String(), "struct") tst.AssertEqual(t, coll.getFieldType("cdate").Type.String(), "time.Time") tst.AssertEqual(t, coll.getFieldType("cdate").Name, "CDate") tst.AssertEqual(t, coll.getFieldType("cdate").IsPointer, false) tst.AssertEqual(t, coll.getFieldValue(d, "cdate").(time.Time), t0) tst.AssertEqual(t, coll.getFieldType("sub.a").Kind.String(), "string") tst.AssertEqual(t, coll.getFieldType("sub.a").Type.String(), "string") tst.AssertEqual(t, coll.getFieldType("sub.a").Name, "A") tst.AssertEqual(t, coll.getFieldType("sub.a").IsPointer, false) tst.AssertEqual(t, coll.getFieldValue(d, "sub.a").(string), "2") tst.AssertEqual(t, coll.getFieldType("str").Kind.String(), "string") tst.AssertEqual(t, coll.getFieldType("str").Type.String(), "string") tst.AssertEqual(t, coll.getFieldType("str").Name, "Str") tst.AssertEqual(t, coll.getFieldType("str").IsPointer, false) tst.AssertEqual(t, coll.getFieldValue(d, "str").(string), "3") tst.AssertEqual(t, coll.getFieldType("ptr").Kind.String(), "int") tst.AssertEqual(t, coll.getFieldType("ptr").Type.String(), "int") tst.AssertEqual(t, coll.getFieldType("ptr").Name, "Ptr") tst.AssertEqual(t, coll.getFieldType("ptr").IsPointer, true) tst.AssertEqual(t, *coll.getFieldValue(d, "ptr").(*int), 4) } func TestReflectionGetTokenValueAsMongoType(t *testing.T) { type IDType string type TestData struct { ID IDType `bson:"_id"` CDate time.Time `bson:"cdate"` Sub struct { A string `bson:"a"` } `bson:"sub"` Str string `bson:"str"` Ptr *int `bson:"ptr"` Num int `bson:"num"` } coll := W[TestData](&mongo.Collection{}) coll.init() gtvasmt := func(value string, fieldName string) any { v, err := coll.getTokenValueAsMongoType(value, fieldName) if err != nil { t.Errorf("%s", "failed to getTokenValueAsMongoType") } return v } tst.AssertEqual(t, gtvasmt("hello", "str").(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) tst.AssertEqual(t, *(gtvasmt("123", "ptr").(*int)), 123) } func TestReflectionGetFieldValueAsTokenString(t *testing.T) { type IDType string type TestData struct { ID IDType `bson:"_id"` CDate time.Time `bson:"cdate"` Sub struct { A string `bson:"a"` } `bson:"sub"` Str string `bson:"str"` Ptr *int `bson:"ptr"` Num int `bson:"num"` Ptr2 *int `bson:"ptr2"` FFF float64 `bson:"fff"` } coll := W[TestData](&mongo.Collection{}) coll.init() t0 := time.Now() d := TestData{ ID: "1", CDate: t0, Sub: struct { A string `bson:"a"` }{ A: "2", }, Str: "3", Ptr: langext.Ptr(4), Num: 22, FFF: 22.5, Ptr2: nil, } gfvats := func(value TestData, fieldName string) string { v, err := coll.getFieldValueAsTokenString(value, fieldName) if err != nil { t.Errorf("%s", "failed to getTokenValueAsMongoType") } return v } tst.AssertEqual(t, gfvats(d, "str"), "3") tst.AssertEqual(t, gfvats(d, "num"), "22") tst.AssertEqual(t, gfvats(d, "_id"), "1") tst.AssertEqual(t, gfvats(d, "ptr"), "4") tst.AssertEqual(t, gfvats(d, "ptr2"), "") tst.AssertEqual(t, gfvats(d, "fff"), "22.5") }