Skip to content

Commit e834ed4

Browse files
Fix generation of DAO objects with cycles.
1 parent 897e995 commit e834ed4

File tree

11 files changed

+2799
-8
lines changed

11 files changed

+2799
-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
@@ -254,25 +254,32 @@ func isStructType(data reflect.Value) bool {
254254
(data.Kind() == reflect.Ptr && data.Elem().Kind() == reflect.Struct)
255255
}
256256

257-
func pullRelationShip(cache map[string]bool, relationships []*schema.Relationship) []field.Relation {
257+
func pullRelationShip(cache map[string][]field.Relation, relationships []*schema.Relationship) []field.Relation {
258258
if len(relationships) == 0 {
259259
return nil
260260
}
261261
result := make([]field.Relation, len(relationships))
262-
for i, relationship := range relationships {
263-
var childRelations []field.Relation
262+
263+
for _, relationship := range relationships {
264264
varType := strings.TrimLeft(relationship.Field.FieldType.String(), "[]*")
265-
if !cache[varType] {
266-
cache[varType] = true
267-
childRelations = pullRelationShip(cache, append(append(append(append(
265+
_, ok := cache[varType]
266+
if !ok {
267+
cache[varType] = []field.Relation{}
268+
childRelations := pullRelationShip(cache, append(append(append(append(
268269
make([]*schema.Relationship, 0, 4),
269270
relationship.FieldSchema.Relationships.BelongsTo...),
270271
relationship.FieldSchema.Relationships.HasOne...),
271272
relationship.FieldSchema.Relationships.HasMany...),
272273
relationship.FieldSchema.Relationships.Many2Many...),
273274
)
275+
cache[varType] = childRelations
274276
}
275-
result[i] = *field.NewRelationWithType(field.RelationshipType(relationship.Type), relationship.Name, varType, childRelations...)
277+
}
278+
279+
for i, relationship := range relationships {
280+
varType := strings.TrimLeft(relationship.Field.FieldType.String(), "[]*")
281+
cached := cache[varType]
282+
result[i] = *field.NewRelationWithType(field.RelationshipType(relationship.Type), relationship.Name, varType, cached...)
276283
}
277284
return result
278285
}

0 commit comments

Comments
 (0)