fix infinite recursion in self-referential models

This commit is contained in:
☙◦ The Tablet ❀ GamerGirlandCo ◦❧ 2024-09-19 19:56:25 -04:00
parent 8c559d5926
commit 866a459855
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") 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 := "" coll := ""
refs := make(map[string]Reference) refs := make(map[string]Reference)
idcs := make(map[string][]InternalIndex) idcs := make(map[string][]InternalIndex)
@ -130,9 +130,9 @@ func parseTags(t reflect.Type, v reflect.Value) (map[string][]InternalIndex, map
switch ft.Kind() { switch ft.Kind() {
case reflect.Slice: case reflect.Slice:
ft = ft.Elem() 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 { 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 { for k, vv := range ii2 {
idcs[sft.Name+"."+k] = vv idcs[sft.Name+"."+k] = vv
} }
@ -285,7 +285,7 @@ func (r TModelRegistry) Model(mdl ...any) {
if idx < 0 { if idx < 0 {
panic("A model must embed the Document struct!") panic("A model must embed the Document struct!")
} }
inds, refs, gfs, coll := parseTags(t, v) inds, refs, gfs, coll := parseTags(t, v, "")
if coll == "" { if coll == "" {
panic(fmt.Sprintf("a Document needs to be given a collection name! (passed type: %s)", n)) panic(fmt.Sprintf("a Document needs to be given a collection name! (passed type: %s)", n))
} }