Skip to content

Commit 6b5ebbe

Browse files
authored
Add version to storage (#2)
1 parent 6e7cb1d commit 6b5ebbe

File tree

5 files changed

+14
-74
lines changed

5 files changed

+14
-74
lines changed

Sources/Normalization/NormalizedStorageType.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ public protocol NormalizedStorageType: Equatable, Sendable {
55
Performs any additional operations for updating.
66
*/
77
func finalizeTransaction(transaction: inout ModifyingTransaction<Self>)
8+
9+
var version: UInt64 { get set }
810

911
static func compare(lhs: Self, rhs: Self) -> Bool
1012
}
@@ -39,6 +41,8 @@ extension NormalizedStorageType {
3941
do {
4042
self = transaction.modifying
4143
}
44+
45+
self.version += 1
4246

4347
}
4448

Sources/Normalization/VergeNormalization+Macros.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
//@attached(member, names: arbitrary)
2+
@attached(member, names: named(version))
33
//@attached(memberAttribute)
44
@attached(extension, conformances: NormalizedStorageType, Equatable, Sendable, names: named(Context), arbitrary)
55
public macro NormalizedStorage() = #externalMacro(module: "NormalizationMacrosPlugin", type: "NormalizedStorageMacro")

Sources/NormalizationMacrosPlugin/NormalizedStorageMacro.swift

Lines changed: 4 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -176,84 +176,16 @@ extension NormalizedStorageMacro: MemberAttributeMacro {
176176

177177
/// Add member
178178
extension NormalizedStorageMacro: MemberMacro {
179-
180-
final class RenamingVisitor: SyntaxRewriter {
181-
182-
init() {}
183-
184-
override func visit(_ node: IdentifierPatternSyntax) -> PatternSyntax {
185-
return "_$\(node.identifier)"
186-
}
187-
188-
override func visit(_ node: VariableDeclSyntax) -> DeclSyntax {
189-
190-
// TODO: make variable private
191-
return super.visit(node)
192-
}
193-
}
194-
195-
final class StoredPropertyCollector: SyntaxVisitor {
196-
197-
var storedProperties: [VariableDeclSyntax] = []
198-
199-
var onFoundMultipleBindings: () -> Void = {}
200-
201-
override func visit(_ node: VariableDeclSyntax) -> SyntaxVisitorContinueKind {
202-
203-
if node.bindingSpecifier == "let" {
204-
storedProperties.append(node)
205-
return super.visit(node)
206-
}
207-
208-
if node.bindings.count > 1 {
209-
// let a,b,c = 0
210-
// it's stored
211-
onFoundMultipleBindings()
212-
return super.visit(node)
213-
}
214-
215-
if node.bindings.first?.accessorBlock == nil {
216-
storedProperties.append(node)
217-
return super.visit(node)
218-
}
219-
220-
// computed property
221-
222-
return .visitChildren
223-
}
224-
225-
}
226-
227-
static func makeVariableFromConstant(_ node: VariableDeclSyntax) -> VariableDeclSyntax {
228-
var modified = node
229-
modified.bindingSpecifier = "var"
230-
return modified
231-
}
232-
179+
233180
public static func expansion(
234181
of node: SwiftSyntax.AttributeSyntax,
235182
providingMembersOf declaration: some SwiftSyntax.DeclGroupSyntax,
236183
in context: some SwiftSyntaxMacros.MacroExpansionContext
237184
) throws -> [SwiftSyntax.DeclSyntax] {
238185

239-
240-
let v = StoredPropertyCollector(viewMode: .fixedUp)
241-
v.onFoundMultipleBindings = {
242-
context.addDiagnostics(from: MacroError(message: "Cannot use multiple bindings"), node: node)
243-
}
244-
v.walk(declaration.memberBlock)
245-
246-
let storageMembers = v.storedProperties
247-
.map(makeVariableFromConstant)
248-
.map {
249-
250-
let rename = RenamingVisitor()
251-
let renamed = rename.visit($0)
252-
253-
return renamed
254-
}
255-
256-
return storageMembers
186+
return [
187+
"public var version: UInt64 = 0"
188+
]
257189

258190
}
259191

Tests/NormalizationMacrosTests/Tests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ final class DatabaseMacroTests: XCTestCase {
5454
}
5555
5656
extension MyDatabase: NormalizedStorageType {
57+
public var version: UInt64 { 0 }
5758
}
5859
5960
extension MyDatabase: Sendable {
@@ -98,6 +99,7 @@ final class DatabaseMacroTests: XCTestCase {
9899
}
99100
100101
extension MyDatabase: NormalizedStorageType {
102+
public var version: UInt64 { 0 }
101103
}
102104
103105
extension MyDatabase: Sendable {

Tests/NormalizationTests/Source.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ final class Tests: XCTestCase {
2424
func testCommit() {
2525

2626
var state = MyStorage()
27-
27+
let currentVersion = state.version
28+
2829
var transaction = state.beginBatchUpdates()
2930

3031
let book = Book(rawID: "some", authorID: Author.anonymous.typedID)
@@ -36,6 +37,7 @@ final class Tests: XCTestCase {
3637
let b = state.book
3738

3839
XCTAssertEqual(a, b)
40+
XCTAssertNotEqual(currentVersion, state.version)
3941

4042
}
4143

0 commit comments

Comments
 (0)