From 2f93ab9914635b6b28ba1dd8df73cae997a5db61 Mon Sep 17 00:00:00 2001 From: qqxhb <30866940+qqxhb@users.noreply.github.com> Date: Wed, 31 Aug 2022 18:05:07 +0800 Subject: [PATCH] Revert "fix: ParseStructRelationShip with cache" --- internal/generate/export.go | 2 +- internal/generate/query.go | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/internal/generate/export.go b/internal/generate/export.go index 6bd9ab53..6d2aca16 100644 --- a/internal/generate/export.go +++ b/internal/generate/export.go @@ -207,7 +207,7 @@ func BuildDIYMethod(f *parser.InterfaceSet, s *QueryStructMeta, data []*Interfac // ParseStructRelationShip parse struct's relationship // No one should use it directly in project func ParseStructRelationShip(relationship *schema.Relationships) []field.Relation { - cache := make(map[string][]field.Relation) + cache := make(map[string]bool) return append(append(append(append( make([]field.Relation, 0, 4), pullRelationShip(cache, relationship.HasOne)...), diff --git a/internal/generate/query.go b/internal/generate/query.go index 4f3bbc66..29afa5ad 100644 --- a/internal/generate/query.go +++ b/internal/generate/query.go @@ -206,7 +206,7 @@ func isStructType(data reflect.Value) bool { (data.Kind() == reflect.Ptr && data.Elem().Kind() == reflect.Struct) } -func pullRelationShip(cache map[string][]field.Relation, relationships []*schema.Relationship) []field.Relation { +func pullRelationShip(cache map[string]bool, relationships []*schema.Relationship) []field.Relation { if len(relationships) == 0 { return nil } @@ -214,9 +214,8 @@ func pullRelationShip(cache map[string][]field.Relation, relationships []*schema for i, relationship := range relationships { var childRelations []field.Relation varType := strings.TrimLeft(relationship.Field.FieldType.String(), "[]*") - if cacheChildRelations, ok := cache[varType]; ok { - childRelations = cacheChildRelations - } else { + if !cache[varType] { + cache[varType] = true childRelations = pullRelationShip(cache, append(append(append(append( make([]*schema.Relationship, 0, 4), relationship.FieldSchema.Relationships.BelongsTo...), @@ -224,7 +223,6 @@ func pullRelationShip(cache map[string][]field.Relation, relationships []*schema relationship.FieldSchema.Relationships.HasMany...), relationship.FieldSchema.Relationships.Many2Many...), ) - cache[varType] = childRelations } result[i] = *field.NewRelationWithType(field.RelationshipType(relationship.Type), relationship.Name, varType, childRelations...) }