func mergeRelationships()

in Sources/SymbolKit/UnifiedSymbolGraph/UnifiedSymbolGraph.swift [48:73]


    func mergeRelationships(with otherRelations: [SymbolGraph.Relationship]) {
        struct RelationKey: Hashable {
            let source: String
            let target: String
            let kind: SymbolGraph.Relationship.Kind

            init(fromRelation relationship: SymbolGraph.Relationship) {
                self.source = relationship.source
                self.target = relationship.target
                self.kind = relationship.kind
            }

            static func makePair(fromRelation relationship: SymbolGraph.Relationship) -> (RelationKey, SymbolGraph.Relationship) {
                return (RelationKey(fromRelation: relationship), relationship)
            }
        }

        // first add the new relations to this one
        self.relationships.append(contentsOf: otherRelations)

        // deduplicate the combined relationships array by source/target/kind
        // FIXME: Actually merge relationships if they have different mixins (rdar://84267943)
        let map = [:].merging(self.relationships.map({ RelationKey.makePair(fromRelation: $0) }), uniquingKeysWith: { r1, r2 in r1 })

        self.relationships = Array(map.values)
    }