This commit is contained in:
2023-06-10 18:35:56 +02:00
parent a30da61419
commit ceff0161c6
5 changed files with 91 additions and 16 deletions

View File

@@ -1,11 +1,14 @@
package wmo
import (
"context"
"go.mongodb.org/mongo-driver/bson/primitive"
"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"
"reflect"
"testing"
"time"
)
@@ -177,3 +180,31 @@ func TestReflectionGetFieldValueAsTokenString(t *testing.T) {
tst.AssertEqual(t, gfvats(d, "cdate"), t0.Format(time.RFC3339Nano))
tst.AssertEqual(t, gfvats(d, "mdate"), t0.Format(time.RFC3339Nano))
}
func TestReflectionWithInterface(t *testing.T) {
type TestData struct {
ID primitive.ObjectID `bson:"_id"`
CDate time.Time `bson:"cdate"`
}
type TestInterface interface {
}
coll1 := W[TestInterface](&mongo.Collection{})
tst.AssertTrue(t, coll1.coll != nil)
tst.AssertEqual(t, 0, len(coll1.implDataTypeMap))
df := func(ctx context.Context, dec Decodable) (TestInterface, error) {
return TestData{}, nil
}
coll2 := W[TestInterface](&mongo.Collection{}).WithDecodeFunc(df, TestData{})
tst.AssertTrue(t, coll2.coll != nil)
tst.AssertEqual(t, 1, len(coll2.implDataTypeMap))
tst.AssertEqual(t, "ID", coll2.implDataTypeMap[reflect.TypeOf(TestData{})]["_id"].Name)
tst.AssertEqual(t, "CDate", coll2.implDataTypeMap[reflect.TypeOf(TestData{})]["cdate"].Name)
}