Created
March 16, 2019 03:37
-
-
Save beccadax/d29024f1ee8bfb98e65c72e417b8ac28 to your computer and use it in GitHub Desktop.
A not-quite-functional way to simplify string interpolation conformances.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct _AppendingStringInterpolation<Result: ExpressibleByStringInterpolation> { | |
var result: Result | |
} | |
protocol ExpressibleByAppendingStringInterpolation: ExpressibleByStringInterpolation where StringInterpolation == _AppendingStringInterpolation<Self> { | |
init(stringLiteral: String) | |
mutating func append(_ value: Self) | |
} | |
extension ExpressibleByAppendingStringInterpolation where StringInterpolation == _AppendingStringInterpolation<Self> { | |
init(stringInterpolation value: _AppendingStringInterpolation<Self>) { | |
self = value.result | |
} | |
} | |
extension _AppendingStringInterpolation: StringInterpolationProtocol where Result: ExpressibleByAppendingStringInterpolation { | |
init(literalCapacity: Int, interpolationCount: Int) { | |
var string = "" | |
string.reserveCapacity(literalCapacity + interpolationCount * 2) | |
result = Result(stringLiteral: string) | |
} | |
mutating func appendLiteral(_ literal: Result.StringLiteralType) { | |
result.append(Result(stringLiteral: literal)) | |
} | |
mutating func appendInterpolation(_ similar: Result) { | |
result.append(similar) | |
} | |
} | |
// ********************* | |
typealias SQLValue = String // Stub | |
struct QueryPart { | |
var sql: String | |
var values: [SQLValue] | |
} | |
extension QueryPart: ExpressibleByAppendingStringInterpolation { | |
init(stringLiteral value: String) { | |
self.init(sql: value, values: []) | |
} | |
mutating func append(_ part: QueryPart) { | |
sql += part.sql | |
values += part.values | |
} | |
} | |
extension QueryPart.StringInterpolation { | |
mutating func appendInterpolation(raw: String) { | |
result.append(QueryPart(sql: raw, values: [])) | |
} | |
mutating func appendInterpolation(param: SQLValue) { | |
let placeholder = "$\(result.values.count + 1)" | |
result.append(QueryPart(sql: placeholder, values: [param])) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment