From 866a45985551e184bd93c01d862e845442b8d3de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Thu, 19 Sep 2024 19:56:25 -0400 Subject: [PATCH] fix infinite recursion in self-referential models --- registry.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/registry.go b/registry.go index 949bc1f..15c9972 100644 --- a/registry.go +++ b/registry.go @@ -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) (map[string][]InternalIndex, map[string]Reference, map[string]gridFSReference, string) { +func parseTags(t reflect.Type, v reflect.Value, lastParsed string) (map[string][]InternalIndex, map[string]Reference, map[string]gridFSReference, string) { coll := "" refs := make(map[string]Reference) idcs := make(map[string][]InternalIndex) @@ -130,9 +130,9 @@ func parseTags(t reflect.Type, v reflect.Value) (map[string][]InternalIndex, map switch ft.Kind() { case reflect.Slice: ft = ft.Elem() - if _, ok := tags.Get("ref"); ok != nil { + if _, ok := tags.Get("ref"); ok == nil && lastParsed != ft.String() { if ft.Kind() == reflect.Struct { - ii2, rr2, gg2, _ := parseTags(ft, reflect.New(ft).Elem()) + ii2, rr2, gg2, _ := parseTags(ft, reflect.New(ft).Elem(), ft.String()) for k, vv := range ii2 { idcs[sft.Name+"."+k] = vv } @@ -285,7 +285,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, "") if coll == "" { panic(fmt.Sprintf("a Document needs to be given a collection name! (passed type: %s)", n)) }