fix infinite recursion in self-referential models
This commit is contained in:
		
							parent
							
								
									8c559d5926
								
							
						
					
					
						commit
						866a459855
					
				| @ -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)) | ||||
| 		} | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user