Skip to content

Commit f576582

Browse files
authored
Fix for @DatabaseFunction when arguments are newline separated (#166)
1 parent 25f7863 commit f576582

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

Sources/StructuredQueriesSQLiteMacros/DatabaseFunctionMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ extension DatabaseFunctionMacro: PeerMacro {
135135
}
136136
signature.parameterClause.parameters[index] = parameter
137137
invocationArgumentTypes.append(type)
138-
let parameterName = "\(parameter.secondName ?? parameter.firstName)"
138+
let parameterName = (parameter.secondName ?? parameter.firstName).trimmedDescription
139139
parameters.append(parameterName)
140140
argumentBindings.append((parameterName, "\(type)(queryBinding: arguments[\(offset)])"))
141141
}

Tests/StructuredQueriesMacrosTests/DatabaseFunctionMacroTests.swift

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,5 +967,118 @@ extension SnapshotTests {
967967
"""#
968968
}
969969
}
970+
971+
@Test func formatting() {
972+
assertMacro {
973+
"""
974+
@DatabaseFunction
975+
func min(
976+
_ x: Int,
977+
_ y: Int
978+
) {
979+
Swift.min(x, y)
980+
}
981+
"""
982+
} expansion: {
983+
#"""
984+
func min(
985+
_ x: Int,
986+
_ y: Int
987+
) {
988+
Swift.min(x, y)
989+
}
990+
991+
var $min: __macro_local_3minfMu_ {
992+
__macro_local_3minfMu_(min)
993+
}
994+
995+
struct __macro_local_3minfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
996+
public typealias Input = (Int, Int)
997+
public typealias Output = Swift.Void
998+
public let name = "min"
999+
public let argumentCount: Int? = 2
1000+
public let isDeterministic = false
1001+
public let body: (Int, Int) -> Swift.Void
1002+
public init(_ body: @escaping (Int, Int) -> Swift.Void) {
1003+
self.body = body
1004+
}
1005+
public func callAsFunction(
1006+
_ x: some StructuredQueriesCore.QueryExpression<Int>,
1007+
_ y: some StructuredQueriesCore.QueryExpression<Int>
1008+
) -> some StructuredQueriesCore.QueryExpression<Swift.Void> {
1009+
StructuredQueriesCore.SQLQueryExpression(
1010+
"\(quote: self.name)(\(x), \(y))"
1011+
)
1012+
}
1013+
public func invoke(
1014+
_ arguments: [StructuredQueriesCore.QueryBinding]
1015+
) -> StructuredQueriesCore.QueryBinding {
1016+
guard self.argumentCount == nil || self.argumentCount == arguments.count, let x = Int(queryBinding: arguments[0]), let y = Int(queryBinding: arguments[1]) else {
1017+
return .invalid(InvalidInvocation())
1018+
}
1019+
self.body(x.queryOutput, y.queryOutput)
1020+
return .null
1021+
}
1022+
private struct InvalidInvocation: Error {
1023+
}
1024+
}
1025+
"""#
1026+
}
1027+
assertMacro {
1028+
"""
1029+
@DatabaseFunction
1030+
func min(
1031+
x: Int,
1032+
y: Int
1033+
) {
1034+
Swift.min(x, y)
1035+
}
1036+
"""
1037+
} expansion: {
1038+
#"""
1039+
func min(
1040+
x: Int,
1041+
y: Int
1042+
) {
1043+
Swift.min(x, y)
1044+
}
1045+
1046+
var $min: __macro_local_3minfMu_ {
1047+
__macro_local_3minfMu_(min)
1048+
}
1049+
1050+
struct __macro_local_3minfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
1051+
public typealias Input = (Int, Int)
1052+
public typealias Output = Swift.Void
1053+
public let name = "min"
1054+
public let argumentCount: Int? = 2
1055+
public let isDeterministic = false
1056+
public let body: (Int, Int) -> Swift.Void
1057+
public init(_ body: @escaping (Int, Int) -> Swift.Void) {
1058+
self.body = body
1059+
}
1060+
public func callAsFunction(
1061+
x: some StructuredQueriesCore.QueryExpression<Int>,
1062+
y: some StructuredQueriesCore.QueryExpression<Int>
1063+
) -> some StructuredQueriesCore.QueryExpression<Swift.Void> {
1064+
StructuredQueriesCore.SQLQueryExpression(
1065+
"\(quote: self.name)(\(x), \(y))"
1066+
)
1067+
}
1068+
public func invoke(
1069+
_ arguments: [StructuredQueriesCore.QueryBinding]
1070+
) -> StructuredQueriesCore.QueryBinding {
1071+
guard self.argumentCount == nil || self.argumentCount == arguments.count, let x = Int(queryBinding: arguments[0]), let y = Int(queryBinding: arguments[1]) else {
1072+
return .invalid(InvalidInvocation())
1073+
}
1074+
self.body(x.queryOutput, y.queryOutput)
1075+
return .null
1076+
}
1077+
private struct InvalidInvocation: Error {
1078+
}
1079+
}
1080+
"""#
1081+
}
1082+
}
9701083
}
9711084
}

0 commit comments

Comments
 (0)