Compare commits

..

No commits in common. "6d7cfb86f861079c34b46b291279819644677235" and "5b8d7ebf876f90cc6f1379ea17b213a8bd9d3692" have entirely different histories.

3 changed files with 10 additions and 30 deletions

View File

@ -1,5 +1,5 @@
package goext
const GoextVersion = "0.0.260"
const GoextVersion = "0.0.259"
const GoextVersionTimestamp = "2023-09-12T11:40:39+0200"
const GoextVersionTimestamp = "2023-09-12T10:48:57+0200"

View File

@ -25,7 +25,7 @@ func (c *Coll[TData]) EnsureInitializedReflection(v TData) {
m := make(map[string]fullTypeRef)
c.initFields("", rval.Type(), m, make([]int, 0), make([]reflect.Type, 0))
c.initFields("", rval.Type(), m, make([]int, 0))
c.implDataTypeMap[rval.Type()] = m
}
@ -50,13 +50,14 @@ func (c *Coll[TData]) init() {
c.implDataTypeMap = make(map[reflect.Type]map[string]fullTypeRef)
v := reflect.ValueOf(example)
c.initFields("", v.Type(), c.dataTypeMap, make([]int, 0), make([]reflect.Type, 0))
c.initFields("", v.Type(), c.dataTypeMap, make([]int, 0))
}
}
func (c *Coll[TData]) initFields(prefix string, rtyp reflect.Type, m map[string]fullTypeRef, idxarr []int, typesInPath []reflect.Type) {
func (c *Coll[TData]) initFields(prefix string, rtyp reflect.Type, m map[string]fullTypeRef, idxarr []int) {
for i := 0; i < rtyp.NumField(); i++ {
rsfield := rtyp.Field(i)
@ -90,7 +91,7 @@ func (c *Coll[TData]) initFields(prefix string, rtyp reflect.Type, m map[string]
if langext.InArray("inline", bsontags) && rsfield.Type.Kind() == reflect.Struct {
// pass-through field
c.initFields(prefix, rsfield.Type, m, newIdxArr, typesInPath)
c.initFields(prefix, rsfield.Type, m, newIdxArr)
} else {
@ -121,25 +122,11 @@ func (c *Coll[TData]) initFields(prefix string, rtyp reflect.Type, m map[string]
}
if rsfield.Type.Kind() == reflect.Struct {
c.initFields(fullKey+".", rsfield.Type, m, newIdxArr, typesInPath)
c.initFields(fullKey+".", rsfield.Type, m, newIdxArr)
}
if rsfield.Type.Kind() == reflect.Pointer && rsfield.Type.Elem().Kind() == reflect.Struct {
innerType := rsfield.Type.Elem()
// check if there is recursion
recursion := false
for _, typ := range typesInPath {
recursion = recursion || (typ == innerType)
}
if !recursion {
// Store all seen types before that deref a pointer to prevent endless recursion
newTypesInPath := make([]reflect.Type, len(typesInPath))
copy(newTypesInPath, typesInPath)
newTypesInPath = append(newTypesInPath, rtyp)
c.initFields(fullKey+".", innerType, m, newIdxArr, newTypesInPath)
}
c.initFields(fullKey+".", rsfield.Type.Elem(), m, newIdxArr)
}
}

View File

@ -113,25 +113,19 @@ func TestReflectionGetTokenValueAsMongoType(t *testing.T) {
type IDType string
type RecurseiveType struct {
Other int `bson:"other"`
Inner *RecurseiveType `bson:"inner"`
}
type TestData struct {
ID IDType `bson:"_id"`
CDate time.Time `bson:"cdate"`
Sub struct {
A string `bson:"a"`
} `bson:"sub"`
SubPtr *struct {
SubPtr struct {
A string `bson:"a"`
} `bson:"subPtr"`
Str string `bson:"str"`
Ptr *int `bson:"ptr"`
Num int `bson:"num"`
MDate rfctime.RFC3339NanoTime `bson:"mdate"`
Rec RecurseiveType `bson:"rec"`
}
coll := W[TestData](&mongo.Collection{})
@ -155,7 +149,6 @@ 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", "rec.other").(int), 4)
tst.AssertEqual(t, gtvasmt("4", "num").(int), 4)
tst.AssertEqual(t, gtvasmt("asdf", "_id").(IDType), "asdf")
tst.AssertEqual(t, gtvasmt("", "ptr").(*int), nil)