Skip to content

Commit

Permalink
EntityModel Macro - Extension Access Level attributes (#60)
Browse files Browse the repository at this point in the history
* added access attributes enum

* updated macro to extend type with provided access attributes
  • Loading branch information
KazaiMazai authored Sep 11, 2024
1 parent 600fc72 commit 0a930cd
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 20 deletions.
21 changes: 21 additions & 0 deletions Sources/SwiftletModelMacros/AccessAttributes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// File.swift
//
//
// Created by Sergey Kazakov on 11/09/2024.
//

import Foundation

enum AccessAttribute: String {
case `private`
case `fileprivate`
case `internal`
case `public`
case `open`
case missingAttribute = ""

var name: String {
rawValue
}
}
75 changes: 55 additions & 20 deletions Sources/SwiftletModelMacros/EntityModelMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,34 @@ import SwiftSyntaxMacros
#endif

public struct EntityModelMacro: ExtensionMacro {

}

public extension EntityModelMacro {
static func expansion(
public static func expansion(
of node: SwiftSyntax.AttributeSyntax,
attachedTo declaration: some SwiftSyntax.DeclGroupSyntax,
providingExtensionsOf type: some SwiftSyntax.TypeSyntaxProtocol,
conformingTo protocols: [SwiftSyntax.TypeSyntax],
in context: some SwiftSyntaxMacros.MacroExpansionContext) throws -> [SwiftSyntax.ExtensionDeclSyntax] {

let syntax = try ExtensionDeclSyntax.entityModelMacro(
of: node,
attachedTo: declaration,
providingExtensionsOf: type,
conformingTo: protocols,
in: context
)
return [syntax]
}
}

extension SwiftSyntax.ExtensionDeclSyntax {
static func entityModelMacro(
of node: SwiftSyntax.AttributeSyntax,
attachedTo declaration: some SwiftSyntax.DeclGroupSyntax,
providingExtensionsOf type: some SwiftSyntax.TypeSyntaxProtocol,
conformingTo protocols: [SwiftSyntax.TypeSyntax],
in context: some SwiftSyntaxMacros.MacroExpansionContext) throws -> SwiftSyntax.ExtensionDeclSyntax {

let accessAttribute = declaration.accessAttribute()

let variableDeclarations = declaration
.memberBlock
.members
Expand All @@ -42,18 +59,21 @@ public extension EntityModelMacro {
type: type,
conformingTo: protocols,
relationshipAttributes: relationshipAttributes,
optionalProperties: optionalProperties
optionalProperties: optionalProperties,
accessAttribute: accessAttribute
)

return [syntax]
return syntax
}
}

extension ExtensionDeclSyntax {
static func entityModelProtocol(type: some SwiftSyntax.TypeSyntaxProtocol,
conformingTo protocols: [SwiftSyntax.TypeSyntax],
relationshipAttributes: [RelationshipAttributes],
optionalProperties: [PropertyAttributes]
conformingTo protocols: [SwiftSyntax.TypeSyntax],
relationshipAttributes: [RelationshipAttributes],
optionalProperties: [PropertyAttributes],
accessAttribute: AccessAttribute

) throws -> ExtensionDeclSyntax {
let protocolsString = protocols.map { "\($0)" }
.joined(separator: ",")
Expand All @@ -62,11 +82,11 @@ extension ExtensionDeclSyntax {
return try ExtensionDeclSyntax(
"""
extension \(raw: type): \(raw: protocolsString) {
\(raw: FunctionDeclSyntax.save(relationshipAttributes))
\(raw: FunctionDeclSyntax.delete(relationshipAttributes))
\(raw: FunctionDeclSyntax.normalize(relationshipAttributes))
\(raw: FunctionDeclSyntax.batchQuery(relationshipAttributes))
\(raw: VariableDeclSyntax.patch(optionalProperties))
\(raw: FunctionDeclSyntax.save(accessAttribute, relationshipAttributes))
\(raw: FunctionDeclSyntax.delete(accessAttribute, relationshipAttributes))
\(raw: FunctionDeclSyntax.normalize(accessAttribute, relationshipAttributes))
\(raw: FunctionDeclSyntax.batchQuery(accessAttribute, relationshipAttributes))
\(raw: VariableDeclSyntax.patch(accessAttribute, optionalProperties))
}
"""
)
Expand All @@ -75,13 +95,14 @@ extension ExtensionDeclSyntax {

extension FunctionDeclSyntax {
static func save(
_ accessAttributes: AccessAttribute,
_ attributes: [RelationshipAttributes]
) throws -> FunctionDeclSyntax {

try FunctionDeclSyntax(
"""
func save(to context: inout Context, options: MergeStrategy<Self> = .default) throws {
\(raw: accessAttributes.name) func save(to context: inout Context, options: MergeStrategy<Self> = .default) throws {
try willSave(to: &context)
context.insert(self, options: options)
\(raw: attributes
Expand All @@ -95,13 +116,14 @@ extension FunctionDeclSyntax {
}

static func delete(
_ accessAttributes: AccessAttribute,
_ attributes: [RelationshipAttributes]
) throws -> FunctionDeclSyntax {

try FunctionDeclSyntax(
"""
func delete(from context: inout Context) throws {
\(raw: accessAttributes.name) func delete(from context: inout Context) throws {
try willDelete(from: &context)
context.remove(Self.self, id: id)
\(raw: attributes
Expand All @@ -122,13 +144,14 @@ extension FunctionDeclSyntax {
}

static func normalize(
_ accessAttributes: AccessAttribute,
_ attributes: [RelationshipAttributes]
) throws -> FunctionDeclSyntax {

try FunctionDeclSyntax(
"""
mutating func normalize() {
\(raw: accessAttributes.name) mutating func normalize() {
\(raw: attributes
.map { "$\($0.propertyName).normalize()" }
.joined(separator: "\n")
Expand All @@ -139,13 +162,14 @@ extension FunctionDeclSyntax {
}

static func batchQuery(
_ accessAttributes: AccessAttribute,
_ attributes: [RelationshipAttributes]
) throws -> FunctionDeclSyntax {

try FunctionDeclSyntax(
"""
static func batchQuery(in context: Context) -> [Query<Self>] {
\(raw: accessAttributes.name) static func batchQuery(in context: Context) -> [Query<Self>] {
Self.query(in: context)
\(raw: attributes
.map { "\\.$\($0.propertyName)" }
Expand All @@ -161,13 +185,14 @@ extension FunctionDeclSyntax {
extension VariableDeclSyntax {

static func patch(
_ accessAttributes: AccessAttribute,
_ attributes: [PropertyAttributes]
) throws -> VariableDeclSyntax {

try VariableDeclSyntax(
"""
static var patch: MergeStrategy<Self> {
\(raw: accessAttributes.name) static var patch: MergeStrategy<Self> {
MergeStrategy(
\(raw: attributes
.map { ".patch(\\.\($0.propertyName))"}
Expand All @@ -180,7 +205,17 @@ extension VariableDeclSyntax {
}
}

private extension DeclGroupSyntax {
func accessAttribute() -> AccessAttribute {
modifiers
.compactMap { $0.name.text }
.compactMap { AccessAttribute(rawValue: $0) }
.first ?? .missingAttribute
}
}

private extension VariableDeclSyntax {


func relationshipAttributes() -> RelationshipAttributes? {
for attribute in self.attributes {
Expand Down

0 comments on commit 0a930cd

Please sign in to comment.