Skip to content

Commit

Permalink
Introduce UnorderedComparingArray
Browse files Browse the repository at this point in the history
  • Loading branch information
dfed committed Jan 13, 2024
1 parent decff3a commit 74b5e11
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
42 changes: 39 additions & 3 deletions Sources/SafeDICore/Models/TypeDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public enum TypeDescription: Codable, Hashable, Comparable, Sendable {
/// A nested type with possible generics. e.g. Array.Element or Swift.Array<Element>
indirect case nested(name: String, parentType: TypeDescription, generics: [TypeDescription])
/// A composed type. e.g. Identifiable & Equatable
indirect case composition(Set<TypeDescription>)
indirect case composition(UnorderedComparingArray<TypeDescription>)
/// An optional type. e.g. Int?
indirect case optional(TypeDescription)
/// An implicitly unwrapped optional type. e.g. Int!
Expand Down Expand Up @@ -201,6 +201,8 @@ public enum TypeDescription: Codable, Hashable, Comparable, Sendable {
}
}

// MARK: - TypeSyntax

extension TypeSyntax {

/// - Returns: the type description for the receiver.
Expand All @@ -225,7 +227,7 @@ extension TypeSyntax {
generics: genericTypeVisitor.genericArguments)

} else if let typeIdentifiers = CompositionTypeSyntax(self) {
return .composition(Set(typeIdentifiers.elements.map { $0.type.typeDescription }))
return .composition(UnorderedComparingArray(typeIdentifiers.elements.map { $0.type.typeDescription }))

} else if let typeIdentifier = OptionalTypeSyntax(self) {
return .optional(typeIdentifier.wrappedType.typeDescription)
Expand Down Expand Up @@ -290,6 +292,8 @@ extension TypeSyntax {
}
}

// MARK: - ExprSyntax

extension ExprSyntax {
public var typeDescription: TypeDescription {
if let typeExpr = TypeExprSyntax(self) {
Expand Down Expand Up @@ -359,7 +363,7 @@ extension ExprSyntax {
}
} else if let sequenceExpr = SequenceExprSyntax(self) {
if sequenceExpr.elements.contains(where: { BinaryOperatorExprSyntax($0) != nil }) {
return .composition(Set(
return .composition(UnorderedComparingArray(
sequenceExpr
.elements
.filter { BinaryOperatorExprSyntax($0) == nil }
Expand Down Expand Up @@ -406,6 +410,38 @@ extension ExprSyntax {
}
}

// MARK: - UnorderedComparingArray

public struct UnorderedComparingArray<Element: Codable & Hashable & Sendable>: Codable, Hashable, Sendable, Collection {

init(_ array: [Element]) {
self.array = array
set = Set(array)
}

let array: [Element]
private let set: Set<Element>

public static func == (lhs: UnorderedComparingArray, rhs: UnorderedComparingArray) -> Bool {
lhs.set == rhs.set
}

public func makeIterator() -> IndexingIterator<Array<Element>> {
array.makeIterator()
}
public var startIndex: Int { array.startIndex }
public var endIndex: Int { array.endIndex }
public func index(after i: Int) -> Int {
array.index(after: i)
}

public subscript(position: Int) -> Element {
array[position]
}
}

// MARK: - GenericArgumentVisitor

private final class GenericArgumentVisitor: SyntaxVisitor {

private(set) var genericArguments = [TypeDescription]()
Expand Down
16 changes: 8 additions & 8 deletions Tests/SafeDICoreTests/TypeDescriptionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -532,27 +532,27 @@ final class TypeDescriptionTests: XCTestCase {

func test_equality_isTrueWhenComparingLexigraphicallyEquivalentCompositions() {
XCTAssertEqual(
TypeDescription.composition([
TypeDescription.composition(.init([
.simple(name: "Foo"),
.simple(name: "Bar"),
]),
TypeDescription.composition([
])),
TypeDescription.composition(.init([
.simple(name: "Foo"),
.simple(name: "Bar"),
])
]))
)
}

func test_equality_isTrueWhenComparingReversedCompositions() {
XCTAssertEqual(
TypeDescription.composition([
TypeDescription.composition(.init([
.simple(name: "Foo"),
.simple(name: "Bar"),
]),
TypeDescription.composition([
])),
TypeDescription.composition(.init([
.simple(name: "Bar"),
.simple(name: "Foo"),
])
]))
)
}

Expand Down

0 comments on commit 74b5e11

Please sign in to comment.