Skip to content

Commit 6bf1caa

Browse files
committed
extensions and typealiases
1 parent 37cc23d commit 6bf1caa

2 files changed

Lines changed: 48 additions & 25 deletions

File tree

Sources/Compiler/Gen/Generator.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,11 @@ public enum BuiltinOrGenerated: CustomStringConvertible {
233233
model.name
234234
}
235235
}
236+
237+
public func namespaced(to namespace: String) -> String {
238+
switch self {
239+
case .model: "\(namespace).\(self)"
240+
case .builtin: description
241+
}
242+
}
236243
}

Sources/Compiler/Gen/SwiftLanguage.swift

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ public struct SwiftLanguage: Language2 {
5959
try declaration(for: query, options: options)
6060
}
6161
}
62+
63+
for query in queries {
64+
try typealiasFor(query: query)
65+
66+
if let input = query.input, case let .model(model) = input {
67+
try inputExtension(for: query, input: model)
68+
}
69+
}
6270
}
6371

6472
return file.formatted().description
@@ -166,27 +174,47 @@ public struct SwiftLanguage: Language2 {
166174
return DeclSyntax(query)
167175
}
168176

169-
private static func outputTypeAlias(cardinality: Cardinality) throws -> TypeAliasDeclSyntax {
170-
switch cardinality {
171-
case .single:
172-
try TypeAliasDeclSyntax("typealias Output = Row?")
173-
case .many:
174-
try TypeAliasDeclSyntax("typealias Output = [Row]")
177+
private static func typealiasFor(query: GeneratedQuery) throws -> TypeAliasDeclSyntax {
178+
let inputTypeName = inputTypeName(for: query, namespaced: true)
179+
let outputTypeName = outputTypeName(for: query, namespaced: true)
180+
return try TypeAliasDeclSyntax(
181+
"typealias \(raw: query.name.capitalizedFirst)Query = Query<\(raw: inputTypeName), \(raw: outputTypeName)>"
182+
)
183+
}
184+
185+
private static func inputExtension(for query: GeneratedQuery, input: GeneratedModel) throws -> ExtensionDeclSyntax {
186+
let inputTypeName = inputTypeName(for: query, namespaced: true)
187+
return try ExtensionDeclSyntax("extension Query where Input == \(raw: inputTypeName)") {
188+
let parameters = input.fields.map { parameter in
189+
"\(parameter.key): \(parameter.value.type)"
190+
}.joined(separator: ", ")
191+
192+
let args = input.fields.map { parameter in
193+
"\(parameter.key): \(parameter.key)"
194+
}.joined(separator: ", ")
195+
196+
"""
197+
func execute(\(raw: parameters)) async throws -> Output {
198+
try await execute(with: \(raw: inputTypeName)(\(raw: args)))
199+
}
200+
"""
175201
}
176202
}
177203

178-
private static func inputTypeName(for query: GeneratedQuery) -> String {
179-
return query.input?.description ?? "()"
204+
private static func inputTypeName(for query: GeneratedQuery, namespaced: Bool = false) -> String {
205+
guard let input = query.input else { return "()" }
206+
return namespaced ? input.namespaced(to: "DB") : input.description
180207
}
181208

182-
private static func outputTypeName(for query: GeneratedQuery) -> String {
209+
private static func outputTypeName(for query: GeneratedQuery, namespaced: Bool = false) -> String {
183210
if let output = query.output {
184-
switch query.outputCardinality {
185-
case .single: "\(output)?"
186-
case .many: "[\(output)]"
211+
let type = namespaced ? output.namespaced(to: "DB") : output.description
212+
return switch query.outputCardinality {
213+
case .single: "\(type)?"
214+
case .many: "[\(type)]"
187215
}
188216
} else {
189-
"()"
217+
return "()"
190218
}
191219
}
192220

@@ -335,18 +363,6 @@ public struct SwiftLanguage: Language2 {
335363
)
336364
}
337365

338-
private static func typealiasDecl(
339-
named name: String,
340-
for type: BuiltinOrGenerated
341-
) throws -> TypeAliasDeclSyntax {
342-
return switch type {
343-
case .builtin(let type, let isArray):
344-
try TypeAliasDeclSyntax("typealias \(raw: name) = \(raw: isArray ? "[\(type)]" : type)")
345-
case .model(let model):
346-
try TypeAliasDeclSyntax("typealias \(raw: name) = \(raw: model.name)")
347-
}
348-
}
349-
350366
@CodeBlockItemListBuilder
351367
private static func bind(
352368
field: String?,

0 commit comments

Comments
 (0)