package wmo import ( "go.mongodb.org/mongo-driver/mongo" "gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/rfctime" "gogs.mikescher.com/BlackForestBytes/goext/timeext" "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"` MDate rfctime.RFC3339NanoTime `bson:"mdate"` } coll := W[TestData](&mongo.Collection{}) coll.init() t0 := time.Now() t1 := rfctime.NewRFC3339Nano(t0) d := TestData{ ID: "1", CDate: t0, Sub: struct { A string `bson:"a"` }{ A: "2", }, Str: "3", Ptr: langext.Ptr(4), MDate: t1, } 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"` MDate rfctime.RFC3339NanoTime `bson:"mdate"` } 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") t.Errorf("%v+", err) } return v } tx, err := time.Parse(time.RFC3339Nano, "2009-11-10T23:00:00Z") if err != nil { t.Errorf("%v", err) } 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) tst.AssertEqual(t, gtvasmt("2009-11-10T23:00:00Z", "cdate").(time.Time), tx) tst.AssertEqual(t, gtvasmt("2009-11-10T23:00:00Z", "mdate").(rfctime.RFC3339NanoTime), rfctime.NewRFC3339Nano(tx)) } 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"` MDate rfctime.RFC3339NanoTime `bson:"mdate"` } coll := W[TestData](&mongo.Collection{}) coll.init() t0 := time.Date(2000, 1, 1, 12, 0, 0, 0, timeext.TimezoneBerlin) t1 := rfctime.NewRFC3339Nano(t0) d := TestData{ ID: "1", CDate: t0, MDate: t1, 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: %v", "failed to getTokenValueAsMongoType", err) } 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") tst.AssertEqual(t, gfvats(d, "cdate"), t0.Format(time.RFC3339Nano)) tst.AssertEqual(t, gfvats(d, "mdate"), t0.Format(time.RFC3339Nano)) }