Skip to content

Commit

Permalink
Simplify type sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLeif committed Nov 3, 2024
1 parent 758821d commit c4062f2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
19 changes: 8 additions & 11 deletions Sources/MockedMacros/MockedMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,26 @@ public struct MockedMacro: PeerMacro {
let objectType: String = requiresClassConformance ? "class" : "struct"

// Check for associated types in the protocol
var associatedTypes: [String: String?] = [:]
var associatedTypes: [String] = []

for member in protocolDecl.memberBlock.members {
if let associatedTypeDecl = member.decl.as(AssociatedTypeDeclSyntax.self) {
let name = associatedTypeDecl.name.text
let constraint = associatedTypeDecl.inheritanceClause?.description.trimmingCharacters(in: .whitespacesAndNewlines)
associatedTypes[name] = constraint

if let constraint {
associatedTypes.append("\(name)\(constraint)")
} else {
associatedTypes.append(name)
}
}
}

// Construct generic type parameters if there are associated types
let genericValues = if associatedTypes.isEmpty {
""
} else {
"<" + associatedTypes
.sorted(by: { $0.key < $1.key })
.map { key, value in
if let value = value, !value.isEmpty {
return "\(key)\(value)"
} else {
return key
}
}.joined(separator: ", ") + ">"
"<" + associatedTypes.joined(separator: ", ") + ">"
}

return [
Expand Down
2 changes: 1 addition & 1 deletion Tests/MockedTests/MockedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ final class MockedMacroTests: XCTestCase {
}
/// Mocked version of ExampleProtocol
struct MockedExampleProtocol<ItemKey: Hashable, ItemType, ItemValue: Codable>: ExampleProtocol {
struct MockedExampleProtocol<ItemType, ItemValue: Codable, ItemKey: Hashable>: ExampleProtocol {
// MARK: - MockedExampleProtocol Variables
var name: String
Expand Down

0 comments on commit c4062f2

Please sign in to comment.