Skip to content

Commit 85b4d14

Browse files
Fix generation of DAO objects with cycles
1 parent a31c34b commit 85b4d14

File tree

11 files changed

+2711
-8
lines changed

11 files changed

+2711
-8
lines changed

internal/generate/export.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func BuildDIYMethod(f *parser.InterfaceSet, s *QueryStructMeta, data []*Interfac
217217
// ParseStructRelationShip parse struct's relationship
218218
// No one should use it directly in project
219219
func ParseStructRelationShip(relationship *schema.Relationships) []field.Relation {
220-
cache := make(map[string]bool)
220+
cache := make(map[string][]field.Relation)
221221
return append(append(append(append(
222222
make([]field.Relation, 0, 4),
223223
pullRelationShip(cache, relationship.HasOne)...),

internal/generate/query.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -245,25 +245,32 @@ func isStructType(data reflect.Value) bool {
245245
(data.Kind() == reflect.Ptr && data.Elem().Kind() == reflect.Struct)
246246
}
247247

248-
func pullRelationShip(cache map[string]bool, relationships []*schema.Relationship) []field.Relation {
248+
func pullRelationShip(cache map[string][]field.Relation, relationships []*schema.Relationship) []field.Relation {
249249
if len(relationships) == 0 {
250250
return nil
251251
}
252252
result := make([]field.Relation, len(relationships))
253-
for i, relationship := range relationships {
254-
var childRelations []field.Relation
253+
254+
for _, relationship := range relationships {
255255
varType := strings.TrimLeft(relationship.Field.FieldType.String(), "[]*")
256-
if !cache[varType] {
257-
cache[varType] = true
258-
childRelations = pullRelationShip(cache, append(append(append(append(
256+
_, ok := cache[varType]
257+
if !ok {
258+
cache[varType] = []field.Relation{}
259+
childRelations := pullRelationShip(cache, append(append(append(append(
259260
make([]*schema.Relationship, 0, 4),
260261
relationship.FieldSchema.Relationships.BelongsTo...),
261262
relationship.FieldSchema.Relationships.HasOne...),
262263
relationship.FieldSchema.Relationships.HasMany...),
263264
relationship.FieldSchema.Relationships.Many2Many...),
264265
)
266+
cache[varType] = childRelations
265267
}
266-
result[i] = *field.NewRelationWithType(field.RelationshipType(relationship.Type), relationship.Name, varType, childRelations...)
268+
}
269+
270+
for i, relationship := range relationships {
271+
varType := strings.TrimLeft(relationship.Field.FieldType.String(), "[]*")
272+
cached := cache[varType]
273+
result[i] = *field.NewRelationWithType(field.RelationshipType(relationship.Type), relationship.Name, varType, cached...)
267274
}
268275
return result
269276
}

0 commit comments

Comments
 (0)