Skip to content

Commit

Permalink
⚡ WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Sep 2, 2023
1 parent c788eae commit 5ebd1a1
Show file tree
Hide file tree
Showing 22 changed files with 755 additions and 283 deletions.
4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ let package = Package(
name: "VergeNormalizationTests",
dependencies: ["VergeNormalization"]
),
.testTarget(
name: "VergeNormalizationDerivedTests",
dependencies: ["VergeNormalizationDerived"]
),
.testTarget(
name: "VergeORMTests",
dependencies: ["VergeORM"]
Expand Down
7 changes: 7 additions & 0 deletions Sources/VergeMacrosPlugin/IndexMacro.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros

public struct DatabaseIndexMacro: Macro {

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros

public struct DatabaseMacro: Macro {
public struct NormalizedStorageMacro: Macro {

}

extension DatabaseMacro: ExtensionMacro {
extension NormalizedStorageMacro: ExtensionMacro {
public static func expansion(
of node: SwiftSyntax.AttributeSyntax,
attachedTo declaration: some SwiftSyntax.DeclGroupSyntax,
Expand Down Expand Up @@ -36,7 +36,7 @@ extension DatabaseMacro: ExtensionMacro {
$0.attributes.contains {
switch $0 {
case .attribute(let attribute):
return attribute.attributeName.description == "TableAccessor"
return attribute.attributeName.description == "Table"
case .ifConfigDecl:
return false
}
Expand Down Expand Up @@ -68,14 +68,13 @@ extension DatabaseMacro: ExtensionMacro {
let decls = tableMembers.map { member in
"""
struct \(member.bindings.first!.pattern.trimmed): TableSelector {
typealias _Table = \(member.bindings.first!.typeAnnotation!.type.description)
typealias Entity = _Table.Entity
typealias Storage = \(structDecl.name.trimmed)
func select(storage: Storage) -> Table<Entity> {
storage.\(member.bindings.first!.pattern.trimmed)
}
typealias _Table = \(member.bindings.first!.typeAnnotation!.type.description)
typealias Entity = _Table.Entity
typealias Storage = \(structDecl.name.trimmed)
func select(storage: Storage) -> _Table {
storage.\(member.bindings.first!.pattern.trimmed)
}
}
"""
}
Expand Down Expand Up @@ -107,8 +106,9 @@ extension DatabaseMacro: ExtensionMacro {

}

#if false
/// Add @Table
extension DatabaseMacro: MemberAttributeMacro {
extension NormalizedStorageMacro: MemberAttributeMacro {

public static func expansion(
of node: SwiftSyntax.AttributeSyntax,
Expand Down Expand Up @@ -139,7 +139,7 @@ extension DatabaseMacro: MemberAttributeMacro {
// }

return [
"@TableAccessor"
"@Table"
]

}
Expand All @@ -150,8 +150,10 @@ extension DatabaseMacro: MemberAttributeMacro {

}

#endif

/// Add member
extension DatabaseMacro: MemberMacro {
extension NormalizedStorageMacro: MemberMacro {

final class RenamingVisitor: SyntaxRewriter {

Expand Down Expand Up @@ -234,47 +236,3 @@ extension DatabaseMacro: MemberMacro {
}

}

public struct DatabaseTableMacro: Macro {

}

extension DatabaseTableMacro: PeerMacro {
public static func expansion(of node: SwiftSyntax.AttributeSyntax, providingPeersOf declaration: some SwiftSyntax.DeclSyntaxProtocol, in context: some SwiftSyntaxMacros.MacroExpansionContext) throws -> [SwiftSyntax.DeclSyntax] {
return []
}

}

extension DatabaseTableMacro: AccessorMacro {
public static func expansion(
of node: SwiftSyntax.AttributeSyntax,
providingAccessorsOf declaration: some SwiftSyntax.DeclSyntaxProtocol,
in context: some SwiftSyntaxMacros.MacroExpansionContext
) throws -> [SwiftSyntax.AccessorDeclSyntax] {

func identifier(from node: VariableDeclSyntax) -> TokenSyntax {
node.bindings.first!.cast(PatternBindingSyntax.self).pattern.cast(IdentifierPatternSyntax.self).identifier
}

let id = identifier(from: declaration.cast(VariableDeclSyntax.self))

return [
// """
// get {
// _$\(id)
// }
// """,
// """
// set {
// _$\(id) = newValue
// }
// """ ,
]
}

}

public struct DatabaseIndexMacro: Macro {

}
4 changes: 2 additions & 2 deletions Sources/VergeMacrosPlugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ struct Plugin: CompilerPlugin {
DatabaseStateMacro.self,
IfChangedMacro.self,

DatabaseMacro.self,
DatabaseTableMacro.self,
NormalizedStorageMacro.self,
TableMacro.self,
DatabaseIndexMacro.self
]
}
43 changes: 43 additions & 0 deletions Sources/VergeMacrosPlugin/TableMacro.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros

public struct TableMacro: Macro {

}

extension TableMacro: PeerMacro {
public static func expansion(of node: SwiftSyntax.AttributeSyntax, providingPeersOf declaration: some SwiftSyntax.DeclSyntaxProtocol, in context: some SwiftSyntaxMacros.MacroExpansionContext) throws -> [SwiftSyntax.DeclSyntax] {
return []
}

}

extension TableMacro: AccessorMacro {
public static func expansion(
of node: SwiftSyntax.AttributeSyntax,
providingAccessorsOf declaration: some SwiftSyntax.DeclSyntaxProtocol,
in context: some SwiftSyntaxMacros.MacroExpansionContext
) throws -> [SwiftSyntax.AccessorDeclSyntax] {

func identifier(from node: VariableDeclSyntax) -> TokenSyntax {
node.bindings.first!.cast(PatternBindingSyntax.self).pattern.cast(IdentifierPatternSyntax.self).identifier
}

let id = identifier(from: declaration.cast(VariableDeclSyntax.self))

return [
// """
// get {
// _$\(id)
// }
// """,
// """
// set {
// _$\(id) = newValue
// }
// """ ,
]
}

}
76 changes: 38 additions & 38 deletions Sources/VergeNormalization/Comparison.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,57 @@ public enum NormalizedStorageComparisons<Storage: NormalizedStorageType> {
public struct StorageComparison: Comparison {
public typealias Input = Storage

public func callAsFunction(_ lhs: Storage, _ rhs: Storage) -> Bool {
public init() {}

lhs == rhs

// (lhs._backingStorage.entityUpdatedMarker, lhs._backingStorage.indexUpdatedMarker) == (rhs._backingStorage.entityUpdatedMarker, rhs._backingStorage.indexUpdatedMarker)
public func callAsFunction(_ lhs: Input, _ rhs: Input) -> Bool {
Storage.compare(lhs: lhs, rhs: rhs)
}
}

/// Returns true if the table of the entity in database has no changes.
///
/// - Complexity: O(1)
public struct TableComparison<Entity: EntityType>: Comparison {

public typealias Input = Storage
public typealias Input = Tables.Hash<Entity>

public func callAsFunction(_ lhs: Storage, _ rhs: Storage) -> Bool {
// lhs._backingStorage.entityBackingStorage.table(Entity.self).updatedMarker == rhs._backingStorage.entityBackingStorage.table(Entity.self).updatedMarker
fatalError()
public init() {}

public func callAsFunction(_ lhs: Input, _ rhs: Input) -> Bool {
guard lhs.updatedMarker == rhs.updatedMarker else {
return lhs == rhs
}
return true
}

}

/// Returns true if the updates result does not contain the entity.
public struct UpdateComparison<Entity: EntityType>: Comparison {

public typealias Input = Storage


public let entityID: Entity.EntityID

public init(entityID: Entity.EntityID) {
self.entityID = entityID
}

public func callAsFunction(_ lhs: Storage, _ rhs: Storage) -> Bool {

fatalError()

// guard let result = rhs._backingStorage.lastUpdatesResult else {
// return false
// }
// guard !result.wasUpdated(entityID) else {
// return false
// }
// guard !result.wasDeleted(entityID) else {
// return false
// }
// return true

}
}
// public struct UpdateComparison<Entity: EntityType>: Comparison {
//
// public typealias Input = Storage
//
// public let entityID: Entity.EntityID
//
// public init(entityID: Entity.EntityID) {
// self.entityID = entityID
// }
//
// public func callAsFunction(_ lhs: Storage, _ rhs: Storage) -> Bool {
//
// fatalError()
//
//// guard let result = rhs._backingStorage.lastUpdatesResult else {
//// return false
//// }
//// guard !result.wasUpdated(entityID) else {
//// return false
//// }
//// guard !result.wasDeleted(entityID) else {
//// return false
//// }
//// return true
//
// }
// }

}

41 changes: 41 additions & 0 deletions Sources/VergeNormalization/Indexes/Indexes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

import HashTreeCollections

public enum Indexes {

/// A Indexing store
///
/// {
/// Grouping-ID : [
/// - Grouped-ID
/// - Grouped-ID
/// - Grouped-ID
/// ],
/// Grouping-ID : [
/// - Grouped-ID
/// - Grouped-ID
/// - Grouped-ID
/// ]
/// }
///
// public typealias GroupByEntity = Never

// public typealias GroupByKey = Never

/**
Mapping another key to the entity id.
```
[
Key: EntityID
Key: EntityID
Key: EntityID
]
```
*/
public typealias Hash<Key: Hashable, Entity: EntityType> = HashTreeCollections.TreeDictionary<Key, Entity.EntityID>

public typealias Ordered<Entity: EntityType> = Array<Entity.EntityID>

public typealias Set = Never

}
2 changes: 1 addition & 1 deletion Sources/VergeNormalization/NonAtomicCounter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public struct NonAtomicCounter: Hashable, Sendable {

public init() {}

public consuming func increment() {
public mutating func increment() {
value &+= 1
}

Expand Down
1 change: 1 addition & 0 deletions Sources/VergeNormalization/NormalizedStorageType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ public protocol NormalizedStorageType: Equatable {

func finalizeTransaction(transaction: inout ModifyingTransaction<Self>)

static func compare(lhs: Self, rhs: Self) -> Bool
}

extension NormalizedStorageType {
Expand Down
Loading

0 comments on commit 5ebd1a1

Please sign in to comment.