Skip to content

Commit 28edf2c

Browse files
committed
Track required coders
1 parent 7666c70 commit 28edf2c

2 files changed

Lines changed: 54 additions & 8 deletions

File tree

Sources/Compiler/Gen/Language.swift

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public protocol Language {
1414

1515
var boolName: String { get }
1616

17+
var builtinCoders: Set<String> { get }
18+
1719
func queryTypeName(input: String, output: String) -> String
1820

1921
func typeName(for type: GenerationType) -> String
@@ -24,7 +26,8 @@ public protocol Language {
2426
func file(
2527
migrations: [String],
2628
tables: [GeneratedModel],
27-
queries: [(String?, [GeneratedQuery])]
29+
queries: [(String?, [GeneratedQuery])],
30+
coders: [String]
2831
) throws -> String
2932

3033
/// Function to generate a interpolation segment in a string
@@ -46,10 +49,20 @@ extension Language {
4649
) throws -> String {
4750
let values = try assemble(queries: queries, schema: schema)
4851

52+
// Get a list of all coders used. Right now we only have to look at the
53+
// tables since any output would inhereit the encoding of the source table.
54+
let coders: Set<String> = values.tables.reduce(into: []) { coders, table in
55+
for field in table.fields.values {
56+
guard let coder = field.type.coder else { continue }
57+
coders.insert(coder)
58+
}
59+
}
60+
4961
return try file(
5062
migrations: migrations,
5163
tables: values.tables,
52-
queries: values.queries
64+
queries: values.queries,
65+
coders: coders.subtracting(builtinCoders).sorted()
5366
)
5467
}
5568

@@ -348,9 +361,13 @@ public enum GenerationType: Equatable {
348361
case .encoded(let encoded, _, _): encoded.model
349362
}
350363
}
351-
}
352-
353-
public struct RequiredCoder: Hashable {
354-
public let sourceType: String
355-
public let storage: String
364+
365+
var coder: String? {
366+
switch self {
367+
case .void, .builtin, .model: nil
368+
case .optional(let optional): optional.coder
369+
case .array(let array): array.coder
370+
case .encoded(_, _, let coder): coder
371+
}
372+
}
356373
}

Sources/Compiler/Gen/SwiftLanguage.swift

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,25 @@ public struct SwiftLanguage: Language {
1515

1616
public var boolName: String { "Bool" }
1717

18+
public var builtinCoders: Set<String> {
19+
[
20+
"BoolDatabaseValueCoder",
21+
"Int8DatabaseValueCoder",
22+
"Int16DatabaseValueCoder",
23+
"Int32DatabaseValueCoder",
24+
"Int64DatabaseValueCoder",
25+
"UInt8DatabaseValueCoder",
26+
"UInt16DatabaseValueCoder",
27+
"UInt32DatabaseValueCoder",
28+
"UInt64DatabaseValueCoder",
29+
"UIntDatabaseValueCoder",
30+
"FloatDatabaseValueCoder",
31+
"Float16DatabaseValueCoder",
32+
"UUIDDatabaseValueCoder",
33+
"DecimalDatabaseValueCoder",
34+
]
35+
}
36+
1837
public func queryTypeName(
1938
input: String,
2039
output: String
@@ -51,8 +70,18 @@ public struct SwiftLanguage: Language {
5170
public func file(
5271
migrations: [String],
5372
tables: [GeneratedModel],
54-
queries: [(String?, [GeneratedQuery])]
73+
queries: [(String?, [GeneratedQuery])],
74+
coders: [String]
5575
) throws -> String {
76+
// Note: For now just going to ignore the `coders`
77+
// Kotlin will need that info which is why it exists.
78+
// Swift having less finegrained namespaces makes it
79+
// so it can just do module level lookups for the type.
80+
// where kotlin defining it just in the project isnt enough
81+
// cause it will be in a different namespace.
82+
//
83+
// Swift we may want to improve it. This is a good starting point though.
84+
5685
let allQueries = queries.flatMap(\.1)
5786

5887
writer.write("import Foundation")

0 commit comments

Comments
 (0)