Skip to content

Commit

Permalink
No more builders
Browse files Browse the repository at this point in the history
  • Loading branch information
dfed committed Nov 28, 2023
1 parent b20f198 commit 8ed7e5a
Show file tree
Hide file tree
Showing 26 changed files with 2,178 additions and 1,680 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ let package = Package(
"SafeDIMacros",
"SafeDICore",
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
.product(name: "MacroTesting", package: "swift-macro-testing"), // TODO: write tests that use this!
.product(name: "MacroTesting", package: "swift-macro-testing"),
]
),
.target(
Expand Down
24 changes: 14 additions & 10 deletions Sources/SafeDI/SafeDI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,26 @@

#else

/// Marks a builder utilized by SafeDI.
/// Marks a `class`, `struct`, or `actor` as capable of having properties that conform to this type decorated with `@constructed` or `@singleton`.
///
/// - Parameter propertyName: The name of the property that can be injected into other SafeDI builders.
/// - Parameter fulfillingAdditionalTypes: The types (in addition to the type decorated with this macro) that can be decorated with `@constructed` or `@singleton` and yield a result of this type. The types provided *must* be either superclasses of this type or protocols to which this type conforms.
@attached(member, names: arbitrary)
public macro builder(_ propertyName: StaticString) = #externalMacro(module: "SafeDIMacros", type: "BuilderMacro")
public macro constructable(fulfillingAdditionalTypes: [Any.Type] = []) = #externalMacro(module: "SafeDIMacros", type: "ConstructableMacro")

/// Marks a collection of dependencies used by a SafeDI builder.
@attached(member, names: arbitrary)
public macro dependencies() = #externalMacro(module: "SafeDIMacros", type: "DependenciesMacro")
/// Marks a SafeDI dependency that is instantiated when its parent object is instantiated.
@attached(peer)
public macro constructed() = #externalMacro(module: "SafeDIMacros", type: "InjectableMacro")

/// Marks a SafeDI dependency that is constructed by an object higher up in the dependency tree.
@attached(peer)
public macro provided() = #externalMacro(module: "SafeDIMacros", type: "InjectableMacro")

/// Marks a SafeDI dependency that is instantiated when its associated builder is instantiated.
/// Marks a SafeDI dependency that will only ever have one instance instantiated at a given time. Singleton dependencies may deallocate when all of the objects that use it deallocate. Singleton dependencies can not be marked with @constructed.
@attached(peer)
public macro constructed() = #externalMacro(module: "SafeDIMacros", type: "ConstructedMacro")
public macro singleton() = #externalMacro(module: "SafeDIMacros", type: "InjectableMacro")

/// Marks a SafeDI dependency that will only ever have one instance instantiated at a given time. Singleton dependencies may deallocate when the built products that use it deallocate. Singleton dependencies can not be marked with @constructed.
/// Marks a SafeDI dependency that is injected into the parent object's initializer and provided to objects further down in the dependency tree.
@attached(peer)
public macro singleton() = #externalMacro(module: "SafeDIMacros", type: "SingletonMacro")
public macro propagated() = #externalMacro(module: "SafeDIMacros", type: "InjectableMacro")

#endif
160 changes: 0 additions & 160 deletions Sources/SafeDICore/BuilderVisitor.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,22 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

/// A representation of a property.
/// e.g. `let myDependency: MyDependency`
public struct Property: Codable, Equatable {
/// The label by which the property is referenced.
public let label: String
/// The type to which the property conforms.
public let type: String
import SwiftSyntax

public var asPropertyDeclaration: String {
"let \(label): \(type)"
}

public var asParameterDeclaration: String {
"\(label): \(type)"
}

public var asLabeledParameterExpression: String {
"\(label): \(label)"
}
public protocol ConcreteDeclSyntaxProtocol: SyntaxProtocol {
var attributes: AttributeListSyntax { get set }
var modifiers: DeclModifierListSyntax { get set }
var inheritanceClause: InheritanceClauseSyntax? { get set }
var name: TokenSyntax { get set }
var isClass: Bool { get }
}

public var asSelfAssignment: String {
"self.\(label) = \(label)"
}
extension ActorDeclSyntax: ConcreteDeclSyntaxProtocol {
public var isClass: Bool { false }
}
extension ClassDeclSyntax: ConcreteDeclSyntaxProtocol {
public var isClass: Bool { true }
}
extension StructDeclSyntax: ConcreteDeclSyntaxProtocol {
public var isClass: Bool { false }
}
Loading

0 comments on commit 8ed7e5a

Please sign in to comment.