fix more infinite recursion when registering models

This commit is contained in:
☙◦ The Tablet ❀ GamerGirlandCo ◦❧ 2025-03-25 16:18:00 -04:00
parent e6a83d12e3
commit cd72703068
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C

View File

@ -111,7 +111,7 @@ func makeRef(idx int, modelName string, fieldName string, ht reflect.Type) Refer
panic("model name was empty")
}
func parseTags(t reflect.Type, v reflect.Value, lastParsed string) (map[string][]InternalIndex, map[string]Reference, map[string]gridFSReference, string) {
func parseTags(t reflect.Type, v reflect.Value, lastParsed string, eqCount int) (map[string][]InternalIndex, map[string]Reference, map[string]gridFSReference, string) {
coll := ""
refs := make(map[string]Reference)
idcs := make(map[string][]InternalIndex)
@ -130,9 +130,15 @@ func parseTags(t reflect.Type, v reflect.Value, lastParsed string) (map[string][
switch ft.Kind() {
case reflect.Slice:
ft = ft.Elem()
if _, ok := tags.Get("ref"); ok == nil && lastParsed != ft.String() {
count := eqCount
if lastParsed != ft.String() {
count = 0
} else {
count = count + 1
}
if /*_, ok := tags.Get("ref"); ok != nil && */ count < 3 {
if ft.Kind() == reflect.Struct {
ii2, rr2, gg2, _ := parseTags(ft, reflect.New(ft).Elem(), ft.String())
ii2, rr2, gg2, _ := parseTags(ft, reflect.New(ft).Elem(), ft.String(), count)
for k, vv := range ii2 {
idcs[sft.Name+"."+k] = vv
}
@ -285,7 +291,7 @@ func (r TModelRegistry) Model(mdl ...any) {
if idx < 0 {
panic("A model must embed the Document struct!")
}
inds, refs, gfs, coll := parseTags(t, v, "")
inds, refs, gfs, coll := parseTags(t, v, "", 0)
if coll == "" {
panic(fmt.Sprintf("a Document needs to be given a collection name! (passed type: %s)", n))
}
@ -342,7 +348,23 @@ func innerWatch(coll *mongo.Collection) {
log.Fatal(err)
}
var uid = data["documentKey"].(bson.M)["_id"]
var uid any
docKey := data["documentKey"]
switch docKey.(type) {
case bson.M:
uid = docKey.(bson.M)["_id"]
case bson.D:
for _, vv := range docKey.(bson.D) {
if vv.Key == "_id" {
uid = vv.Value
break
}
}
}
//var uid = data["documentKey"].(bson.M)["_id"]
if data["operationType"] == "insert" {
counterColl := DB.Collection(COUNTER_COL)