Skip to content

Commit

Permalink
Invert unions during manifest generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenh committed Oct 27, 2023
1 parent 2ef8edd commit 277486e
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (m *Migrator) Process() error {
m.fixupObjectTypes()
m.sortObjectTypes()

m.invertUnions()
m.fixupRelationTypes()

m.trueUpRelations()
Expand Down Expand Up @@ -328,6 +329,32 @@ func (m *Migrator) removeObsoleteRelationTypes() {
})
}

func (m *Migrator) invertUnions() {
for i := 0; i < len(m.Metadata.ObjectTypes); i++ {
obj := m.Metadata.ObjectTypes[i]

rels := lo.Filter(m.Metadata.RelationTypes, func(rel *dsc2.RelationType, _ int) bool {
return rel.ObjectType == obj.Name
})

inverted := lo.Associate(rels, func(rel *dsc2.RelationType) (string, []string) {
return rel.Name, []string{}
})

for _, rel := range rels {
for _, union := range rel.Unions {
if _, ok := inverted[union]; ok {
inverted[union] = append(inverted[union], rel.Name)
}
}
}

for _, rel := range rels {
rel.Unions = inverted[rel.Name]
}
}
}

func (m *Migrator) fixupRelationTypes() {
for i := 0; i < len(m.Metadata.RelationTypes); i++ {
rtMap, ok := RefRelationTypes[m.Metadata.RelationTypes[i].ObjectType]
Expand Down

0 comments on commit 277486e

Please sign in to comment.