Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Sources/Normalization/NormalizedStorageType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ public protocol NormalizedStorageType: Equatable, Sendable {
Performs any additional operations for updating.
*/
func finalizeTransaction(transaction: inout ModifyingTransaction<Self>)

var version: UInt64 { get set }

static func compare(lhs: Self, rhs: Self) -> Bool
}
Expand Down Expand Up @@ -39,6 +41,8 @@ extension NormalizedStorageType {
do {
self = transaction.modifying
}

self.version += 1

}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Normalization/VergeNormalization+Macros.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

//@attached(member, names: arbitrary)
@attached(member, names: named(version))
//@attached(memberAttribute)
@attached(extension, conformances: NormalizedStorageType, Equatable, Sendable, names: named(Context), arbitrary)
public macro NormalizedStorage() = #externalMacro(module: "NormalizationMacrosPlugin", type: "NormalizedStorageMacro")
Expand Down
76 changes: 4 additions & 72 deletions Sources/NormalizationMacrosPlugin/NormalizedStorageMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,84 +176,16 @@ extension NormalizedStorageMacro: MemberAttributeMacro {

/// Add member
extension NormalizedStorageMacro: MemberMacro {

final class RenamingVisitor: SyntaxRewriter {

init() {}

override func visit(_ node: IdentifierPatternSyntax) -> PatternSyntax {
return "_$\(node.identifier)"
}

override func visit(_ node: VariableDeclSyntax) -> DeclSyntax {

// TODO: make variable private
return super.visit(node)
}
}

final class StoredPropertyCollector: SyntaxVisitor {

var storedProperties: [VariableDeclSyntax] = []

var onFoundMultipleBindings: () -> Void = {}

override func visit(_ node: VariableDeclSyntax) -> SyntaxVisitorContinueKind {

if node.bindingSpecifier == "let" {
storedProperties.append(node)
return super.visit(node)
}

if node.bindings.count > 1 {
// let a,b,c = 0
// it's stored
onFoundMultipleBindings()
return super.visit(node)
}

if node.bindings.first?.accessorBlock == nil {
storedProperties.append(node)
return super.visit(node)
}

// computed property

return .visitChildren
}

}

static func makeVariableFromConstant(_ node: VariableDeclSyntax) -> VariableDeclSyntax {
var modified = node
modified.bindingSpecifier = "var"
return modified
}


public static func expansion(
of node: SwiftSyntax.AttributeSyntax,
providingMembersOf declaration: some SwiftSyntax.DeclGroupSyntax,
in context: some SwiftSyntaxMacros.MacroExpansionContext
) throws -> [SwiftSyntax.DeclSyntax] {


let v = StoredPropertyCollector(viewMode: .fixedUp)
v.onFoundMultipleBindings = {
context.addDiagnostics(from: MacroError(message: "Cannot use multiple bindings"), node: node)
}
v.walk(declaration.memberBlock)

let storageMembers = v.storedProperties
.map(makeVariableFromConstant)
.map {

let rename = RenamingVisitor()
let renamed = rename.visit($0)

return renamed
}

return storageMembers
return [
"public var version: UInt64 = 0"
]

}

Expand Down
2 changes: 2 additions & 0 deletions Tests/NormalizationMacrosTests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ final class DatabaseMacroTests: XCTestCase {
}

extension MyDatabase: NormalizedStorageType {
public var version: UInt64 { 0 }
}

extension MyDatabase: Sendable {
Expand Down Expand Up @@ -98,6 +99,7 @@ final class DatabaseMacroTests: XCTestCase {
}

extension MyDatabase: NormalizedStorageType {
public var version: UInt64 { 0 }
}

extension MyDatabase: Sendable {
Expand Down
4 changes: 3 additions & 1 deletion Tests/NormalizationTests/Source.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ final class Tests: XCTestCase {
func testCommit() {

var state = MyStorage()

let currentVersion = state.version

var transaction = state.beginBatchUpdates()

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

XCTAssertEqual(a, b)
XCTAssertNotEqual(currentVersion, state.version)

}

Expand Down
Loading