Skip to content

Commit 01af4b5

Browse files
authored
Enable IDs to be expressed using string interpolation (#2)
Add automatic conformance to `ExpressibleByStringInterpolation` whenever an identifier’s raw value conforms to that protocol with `String` as its literal type.
1 parent 24d4369 commit 01af4b5

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Sources/Identity/Identity.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ extension Identifier: ExpressibleByStringLiteral
6565
}
6666
}
6767

68+
extension Identifier: ExpressibleByStringInterpolation
69+
where Value.RawIdentifier: ExpressibleByStringInterpolation,
70+
Value.RawIdentifier.StringLiteralType == String {}
71+
6872
// MARK: - Compiler-generated protocol support
6973

7074
extension Identifier: Equatable where Value.RawIdentifier: Equatable {}

Tests/IdentityTests/IdentityTests.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ final class IdentityTests: XCTestCase {
5050
XCTAssertEqual(json?["id"] as? String, "I'm an ID")
5151
}
5252

53+
func testExpressingIdentifierUsingStringInterpolation() {
54+
struct Model: Identifiable {
55+
let id: ID
56+
}
57+
58+
let model = Model(id: "Hello, world!")
59+
XCTAssertEqual(model.id, "Hello, \("world!")")
60+
}
61+
5362
func testAllTestsRunOnLinux() {
5463
verifyAllTestsRunOnLinux()
5564
}
@@ -60,6 +69,7 @@ extension IdentityTests: LinuxTestable {
6069
("testStringBasedIdentifier", testStringBasedIdentifier),
6170
("testIntBasedIdentifier", testIntBasedIdentifier),
6271
("testCodableIdentifier", testCodableIdentifier),
63-
("testIdentifierEncodedAsSingleValue", testIdentifierEncodedAsSingleValue)
72+
("testIdentifierEncodedAsSingleValue", testIdentifierEncodedAsSingleValue),
73+
("testExpressingIdentifierUsingStringInterpolation", testExpressingIdentifierUsingStringInterpolation)
6474
]
6575
}

0 commit comments

Comments
 (0)