Skip to content

Commit

Permalink
@DatabaseState
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Jun 16, 2023
1 parent adddd67 commit 88f19c3
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 19 deletions.
17 changes: 12 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ let package = Package(
.library(name: "VergeORM", targets: ["VergeORM"]),
.library(name: "VergeRx", targets: ["VergeRx"]),
.library(name: "VergeClassic", targets: ["VergeClassic"]),
.library(name: "VergeMacros", targets: ["VergeMacros"])
],
dependencies: [
.package(url: "https://github.com/ReactiveX/RxSwift.git", from: "6.0.0"),
Expand All @@ -26,25 +27,27 @@ let package = Package(

/// for testing
.package(url: "https://github.com/nalexn/ViewInspector.git", from: "0.9.3"),

.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0-swift-5.9-DEVELOPMENT-SNAPSHOT-2023-04-25-b")
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0-swift-5.9-DEVELOPMENT-SNAPSHOT-2023-04-25-b")
],
targets: [

// compiler plugin
.macro(
name: "VergeMacros",
name: "VergeMacrosPlugin",
dependencies: [
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax")
]
),

.target(name: "VergeMacrosExports", dependencies: ["VergeMacros"]),
// macro exports
.target(name: "VergeMacros", dependencies: ["VergeMacrosPlugin"]),

.target(name: "VergeTiny", dependencies: []),
.target(
name: "Verge",
dependencies: [
"VergeMacrosExports",
"VergeMacros",
.product(name: "Atomics", package: "swift-atomics"),
.product(name: "DequeModule", package: "swift-collections"),
.product(name: "ConcurrencyTaskManager", package: "swift-concurrency-task-manager"),
Expand Down Expand Up @@ -92,6 +95,10 @@ let package = Package(
name: "VergeTinyTests",
dependencies: ["VergeTiny"]
),
.testTarget(name: "VergeMacrosTests", dependencies: [
"VergeMacros",
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
])
],
swiftLanguageVersions: [.v5]
)
29 changes: 20 additions & 9 deletions Sources/VergeMacros/Source.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import SwiftCompilerPlugin
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros

@main
struct VergeMacroPlugin: CompilerPlugin {
let providingMacros: [Macro.Type] = [
//@freestanding(expression) public macro
// #externalMacro(module: "MacroExamplesPlugin", type: "FontLiteralMacro")

]
}
/**
struct Database: DatabaseType {

struct Schema: EntitySchemaType {
let author = Author.EntityTableKey()
}

struct Indexes: IndexesType {
let allBooks = HashIndex<Schema, String, Author>.Key()
}

var _backingStorage: BackingStorage = .init()
}
*/

@attached(member, names: named(_backingStorage))
@attached(conformance)
public macro DatabaseState() = #externalMacro(module: "VergeMacrosPlugin", type: "DatabaseStateMacro")
48 changes: 48 additions & 0 deletions Sources/VergeMacrosPlugin/DatabaseStateMacro.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros

public struct DatabaseStateMacro {

}

extension DatabaseStateMacro: ConformanceMacro {
public static func expansion<Declaration, Context>(
of node: SwiftSyntax.AttributeSyntax,
providingConformancesOf declaration: Declaration,
in context: Context
) throws -> [(SwiftSyntax.TypeSyntax, SwiftSyntax.GenericWhereClauseSyntax?)]
where Declaration: SwiftSyntax.DeclGroupSyntax, Context: SwiftSyntaxMacros.MacroExpansionContext {

// Decode the expansion arguments.
guard let structDecl = declaration.as(StructDeclSyntax.self) else {
// context.diagnose(OptionSetMacroDiagnostic.requiresStruct.diagnose(at: decl))
return []
}

// If there is an explicit conformance to OptionSet already, don't add one.
if let inheritedTypes = structDecl.inheritanceClause?.inheritedTypeCollection,
inheritedTypes.contains(where: { inherited in inherited.typeName.trimmedDescription == "DatabaseType" }) {
return []
}

return [("DatabaseType", nil)]
}

}

extension DatabaseStateMacro: MemberMacro {

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

return [
"var _backingStorage: BackingStorage = .init()"
]

}
}
16 changes: 16 additions & 0 deletions Sources/VergeMacrosPlugin/VergeMacrosPlugin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import SwiftCompilerPlugin
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros

struct _Macro: AttachedMacro {

}


@main
struct VergeMacroPlugin: CompilerPlugin {
let providingMacros: [Macro.Type] = [
DatabaseStateMacro.self,
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// File.swift
//
//
// Created by Muukii on 2023/06/12.
// Created by Muukii on 2023/06/17.
//

import Foundation
8 changes: 4 additions & 4 deletions Tests/VergeORMTests/RootState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Foundation

import Verge
import VergeORM
import VergeMacros

struct Book: EntityType, Hashable {

Expand Down Expand Up @@ -39,8 +40,9 @@ struct Author: EntityType {
}

struct RootState: StateType {

struct Database: DatabaseType {

@DatabaseState
struct Database {

struct Schema: EntitySchemaType {
let book = Book.EntityTableKey()
Expand All @@ -62,8 +64,6 @@ struct RootState: StateType {
})
]
}

var _backingStorage: BackingStorage = .init()
}

struct Other: Equatable {
Expand Down

0 comments on commit 88f19c3

Please sign in to comment.