Skip to content

Commit e653239

Browse files
authored
Synthesize LosslessStringConvertible QueryBindable conformances (pointfreeco#28)
This commit makes it trivial to conform `LosslessStringConvertible` types to `QueryBindable`.
1 parent 02e9ed9 commit e653239

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

Sources/StructuredQueriesCore/QueryBindable.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ extension DefaultStringInterpolation {
102102
}
103103
}
104104

105+
extension QueryBindable where Self: LosslessStringConvertible {
106+
public var queryBinding: QueryBinding { description.queryBinding }
107+
}
108+
105109
extension QueryBindable where Self: RawRepresentable, RawValue: QueryBindable {
106110
public var queryBinding: QueryBinding { rawValue.queryBinding }
107111
}

Sources/StructuredQueriesCore/QueryDecodable.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,17 @@ extension UInt64: QueryDecodable {
138138
}
139139
}
140140

141+
extension QueryDecodable where Self: LosslessStringConvertible {
142+
@inlinable
143+
public init(decoder: inout some QueryDecoder) throws {
144+
guard let losslessStringConvertible = try Self(String(decoder: &decoder))
145+
else {
146+
throw DataCorruptedError()
147+
}
148+
self = losslessStringConvertible
149+
}
150+
}
151+
141152
extension QueryDecodable where Self: RawRepresentable, RawValue: QueryDecodable {
142153
@inlinable
143154
public init(decoder: inout some QueryDecoder) throws {

Tests/StructuredQueriesTests/DecodingTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ extension SnapshotTests {
3939
#expect(bytes == [0xDE, 0xAD, 0xBE, 0xEF])
4040
}
4141

42+
@Test func losslessStringConvertible() throws {
43+
struct Email: Equatable, LosslessStringConvertible, QueryBindable {
44+
var description: String
45+
46+
init?(_ description: String) {
47+
self.description = description
48+
}
49+
}
50+
#expect(
51+
try db.execute(
52+
SimpleSelect { #sql("'[email protected]'", as: Email.self) }
53+
)
54+
.first == Email("[email protected]")
55+
)
56+
}
57+
4258
@Test func rawRepresentable() throws {
4359
enum Priority: Int, QueryBindable {
4460
case low = 1

0 commit comments

Comments
 (0)