Skip to content

Commit 3e076d1

Browse files
Port Encodable-based QueryEngine implementation (#93)
This PR removes Macro-based Query implementation and lowers the minimum Swift version requirement to 5.8 to use this package in the CI build pipeline. Co-authored-by: Max Desiatov <[email protected]>
1 parent 311e535 commit 3e076d1

File tree

14 files changed

+457
-326
lines changed

14 files changed

+457
-326
lines changed

Package.resolved

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// swift-tools-version: 5.9
1+
// swift-tools-version: 5.8
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

4-
import CompilerPluginSupport
54
import PackageDescription
65

76
let package = Package(
@@ -27,7 +26,6 @@ let package = Package(
2726
.package(url: "https://github.com/apple/swift-nio-extras.git", from: "1.20.0"),
2827
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.3"),
2928
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.3.0"),
30-
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.2"),
3129
],
3230
targets: [
3331
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
@@ -77,7 +75,6 @@ let package = Package(
7775
.product(name: "Logging", package: "swift-log"),
7876
.product(name: "SystemPackage", package: "swift-system"),
7977
"Helpers",
80-
"Macros",
8178
"SystemSQLite",
8279
]
8380
),
@@ -100,24 +97,6 @@ let package = Package(
10097
"Helpers",
10198
]
10299
),
103-
.macro(
104-
name: "Macros",
105-
dependencies: [
106-
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
107-
.product(name: "SwiftSyntax", package: "swift-syntax"),
108-
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
109-
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
110-
.product(name: "SwiftDiagnostics", package: "swift-syntax"),
111-
]
112-
),
113-
.testTarget(
114-
name: "MacrosTests",
115-
dependencies: [
116-
"Macros",
117-
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
118-
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
119-
]
120-
),
121100
.systemLibrary(name: "SystemSQLite", pkgConfig: "sqlite3"),
122101
.target(
123102
name: "AsyncProcess",

Sources/GeneratorEngine/Cache/CacheKeyProtocol.swift

Lines changed: 111 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,88 +10,160 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import Macros
14-
1513
@_exported import protocol Crypto.HashFunction
1614
import struct Foundation.URL
1715
import struct SystemPackage.FilePath
1816

1917
/// Indicates that values of a conforming type can be hashed with an arbitrary hashing function. Unlike `Hashable`,
2018
/// this protocol doesn't utilize random seed values and produces consistent hash values across process launches.
21-
public protocol CacheKeyProtocol {
22-
func hash(with hashFunction: inout some HashFunction)
19+
public protocol CacheKey: Encodable {
2320
}
2421

25-
extension Bool: CacheKeyProtocol {
26-
public func hash(with hashFunction: inout some HashFunction) {
22+
extension Bool: CacheKey {
23+
func hash(with hashFunction: inout some HashFunction) {
2724
String(reflecting: Self.self).hash(with: &hashFunction)
2825
hashFunction.update(data: self ? [1] : [0])
2926
}
3027
}
3128

32-
extension Int: CacheKeyProtocol {
33-
public func hash(with hashFunction: inout some HashFunction) {
29+
extension Int: CacheKey {
30+
func hash(with hashFunction: inout some HashFunction) {
3431
String(reflecting: Self.self).hash(with: &hashFunction)
3532
withUnsafeBytes(of: self) {
36-
hashFunction.update(bufferPointer: $0)
33+
hashFunction.update(data: $0)
3734
}
3835
}
3936
}
4037

41-
extension String: CacheKeyProtocol {
42-
public func hash(with hashFunction: inout some HashFunction) {
43-
var t = String(reflecting: Self.self)
44-
t.withUTF8 {
45-
hashFunction.update(bufferPointer: .init($0))
38+
extension Int8: CacheKey {
39+
func hash(with hashFunction: inout some HashFunction) {
40+
String(reflecting: Self.self).hash(with: &hashFunction)
41+
withUnsafeBytes(of: self) {
42+
hashFunction.update(data: $0)
4643
}
47-
var x = self
48-
x.withUTF8 {
49-
hashFunction.update(bufferPointer: .init($0))
44+
}
45+
}
46+
47+
extension Int16: CacheKey {
48+
func hash(with hashFunction: inout some HashFunction) {
49+
String(reflecting: Self.self).hash(with: &hashFunction)
50+
withUnsafeBytes(of: self) {
51+
hashFunction.update(data: $0)
5052
}
5153
}
5254
}
5355

54-
extension FilePath: CacheKeyProtocol {
55-
public func hash(with hashFunction: inout some HashFunction) {
56+
extension Int32: CacheKey {
57+
func hash(with hashFunction: inout some HashFunction) {
5658
String(reflecting: Self.self).hash(with: &hashFunction)
57-
self.string.hash(with: &hashFunction)
59+
withUnsafeBytes(of: self) {
60+
hashFunction.update(data: $0)
61+
}
5862
}
5963
}
6064

61-
extension FilePath.Component: CacheKeyProtocol {
62-
public func hash(with hashFunction: inout some HashFunction) {
65+
extension Int64: CacheKey {
66+
func hash(with hashFunction: inout some HashFunction) {
6367
String(reflecting: Self.self).hash(with: &hashFunction)
64-
self.string.hash(with: &hashFunction)
68+
withUnsafeBytes(of: self) {
69+
hashFunction.update(data: $0)
70+
}
6571
}
6672
}
6773

68-
extension URL: CacheKeyProtocol {
69-
public func hash(with hashFunction: inout some HashFunction) {
74+
extension UInt: CacheKey {
75+
func hash(with hashFunction: inout some HashFunction) {
7076
String(reflecting: Self.self).hash(with: &hashFunction)
71-
self.description.hash(with: &hashFunction)
77+
withUnsafeBytes(of: self) {
78+
hashFunction.update(data: $0)
79+
}
80+
}
81+
}
82+
83+
extension UInt8: CacheKey {
84+
func hash(with hashFunction: inout some HashFunction) {
85+
String(reflecting: Self.self).hash(with: &hashFunction)
86+
withUnsafeBytes(of: self) {
87+
hashFunction.update(data: $0)
88+
}
89+
}
90+
}
91+
92+
extension UInt16: CacheKey {
93+
func hash(with hashFunction: inout some HashFunction) {
94+
String(reflecting: Self.self).hash(with: &hashFunction)
95+
withUnsafeBytes(of: self) {
96+
hashFunction.update(data: $0)
97+
}
98+
}
99+
}
100+
101+
extension UInt32: CacheKey {
102+
func hash(with hashFunction: inout some HashFunction) {
103+
String(reflecting: Self.self).hash(with: &hashFunction)
104+
withUnsafeBytes(of: self) {
105+
hashFunction.update(data: $0)
106+
}
107+
}
108+
}
109+
110+
extension UInt64: CacheKey {
111+
func hash(with hashFunction: inout some HashFunction) {
112+
String(reflecting: Self.self).hash(with: &hashFunction)
113+
withUnsafeBytes(of: self) {
114+
hashFunction.update(data: $0)
115+
}
72116
}
73117
}
74118

75-
extension Optional: CacheKeyProtocol where Wrapped: CacheKeyProtocol {
76-
public func hash(with hashFunction: inout some HashFunction) {
119+
extension Float: CacheKey {
120+
func hash(with hashFunction: inout some HashFunction) {
77121
String(reflecting: Self.self).hash(with: &hashFunction)
78-
if let self {
79-
true.hash(with: &hashFunction)
80-
self.hash(with: &hashFunction)
81-
} else {
82-
false.hash(with: &hashFunction)
122+
withUnsafeBytes(of: self) {
123+
hashFunction.update(data: $0)
83124
}
84125
}
85126
}
86127

87-
extension Array: CacheKeyProtocol where Element: CacheKeyProtocol {
88-
public func hash(with hashFunction: inout some HashFunction) {
128+
extension Double: CacheKey {
129+
func hash(with hashFunction: inout some HashFunction) {
89130
String(reflecting: Self.self).hash(with: &hashFunction)
90-
for element in self {
91-
element.hash(with: &hashFunction)
131+
withUnsafeBytes(of: self) {
132+
hashFunction.update(data: $0)
92133
}
93134
}
94135
}
95136

96-
@attached(extension, conformances: CacheKeyProtocol, names: named(hash(with:)))
97-
public macro CacheKey() = #externalMacro(module: "Macros", type: "CacheKeyMacro")
137+
extension String: CacheKey {
138+
func hash(with hashFunction: inout some HashFunction) {
139+
var t = String(reflecting: Self.self)
140+
t.withUTF8 {
141+
hashFunction.update(data: $0)
142+
}
143+
var x = self
144+
x.withUTF8 {
145+
hashFunction.update(data: $0)
146+
}
147+
}
148+
}
149+
150+
extension FilePath: CacheKey {
151+
func hash(with hashFunction: inout some HashFunction) {
152+
String(reflecting: Self.self).hash(with: &hashFunction)
153+
self.string.hash(with: &hashFunction)
154+
}
155+
}
156+
157+
extension FilePath.Component: CacheKey {
158+
func hash(with hashFunction: inout some HashFunction) {
159+
String(reflecting: Self.self).hash(with: &hashFunction)
160+
self.string.hash(with: &hashFunction)
161+
}
162+
}
163+
164+
extension URL: CacheKey {
165+
func hash(with hashFunction: inout some HashFunction) {
166+
String(reflecting: Self.self).hash(with: &hashFunction)
167+
self.description.hash(with: &hashFunction)
168+
}
169+
}

0 commit comments

Comments
 (0)