Skip to content

Commit

Permalink
Merge pull request go-gorm#616 from kiancchen/fix-ParseStructRelation…
Browse files Browse the repository at this point in the history
…Ship

fix: ParseStructRelationShip didn't use cache
  • Loading branch information
tr1v3r authored Aug 25, 2022
2 parents ee66b65 + e406fe6 commit 6e3c514
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/generate/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]bool)
cache := make(map[string][]field.Relation)
return append(append(append(append(
make([]field.Relation, 0, 4),
pullRelationShip(cache, relationship.HasOne)...),
Expand Down
8 changes: 5 additions & 3 deletions internal/generate/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,25 @@ func isStructType(data reflect.Value) bool {
(data.Kind() == reflect.Ptr && data.Elem().Kind() == reflect.Struct)
}

func pullRelationShip(cache map[string]bool, relationships []*schema.Relationship) []field.Relation {
func pullRelationShip(cache map[string][]field.Relation, relationships []*schema.Relationship) []field.Relation {
if len(relationships) == 0 {
return nil
}
result := make([]field.Relation, len(relationships))
for i, relationship := range relationships {
var childRelations []field.Relation
varType := strings.TrimLeft(relationship.Field.FieldType.String(), "[]*")
if !cache[varType] {
cache[varType] = true
if cacheChildRelations, ok := cache[varType]; ok {
childRelations = cacheChildRelations
} else {
childRelations = pullRelationShip(cache, append(append(append(append(
make([]*schema.Relationship, 0, 4),
relationship.FieldSchema.Relationships.BelongsTo...),
relationship.FieldSchema.Relationships.HasOne...),
relationship.FieldSchema.Relationships.HasMany...),
relationship.FieldSchema.Relationships.Many2Many...),
)
cache[varType] = childRelations
}
result[i] = *field.NewRelationWithType(field.RelationshipType(relationship.Type), relationship.Name, varType, childRelations...)
}
Expand Down

0 comments on commit 6e3c514

Please sign in to comment.