Skip to content

Commit

Permalink
Inherited -> Received
Browse files Browse the repository at this point in the history
  • Loading branch information
dfed committed Dec 8, 2023
1 parent e794ded commit 626b968
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 62 deletions.
6 changes: 3 additions & 3 deletions Sources/SafeDI/ExternalMacros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
/// Marks a SafeDI dependency that is instantiated when its parent object is instantiated.
@attached(peer) public macro Instantiated() = #externalMacro(module: "SafeDIMacros", type: "InjectableMacro")

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

/// Marks a SafeDI dependency that is injected into the parent object's initializer and forwarded to objects further down in the dependency tree.
@attached(peer) public macro Forwarded() = #externalMacro(module: "SafeDIMacros", type: "InjectableMacro")

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

#endif
2 changes: 1 addition & 1 deletion Sources/SafeDI/ForwardingInstantiator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

/// A SafeDI dependency designed for the deferred instantiation of an `@Instantiable` type that contains
/// `@Forwarded` properties. This class enables instantiation with specific arguments, facilitating
/// the inheritance of these arguments by `@Instantiable` types that are `@Instantiated` within
/// the receiving of these arguments by `@Instantiable` types that are `@Instantiated` within
/// the `InstantiableType`, as well as by all types `@Instantiated` downstream.
/// Instantiation is thread-safe.
///
Expand Down
2 changes: 1 addition & 1 deletion Sources/SafeDICore/Errors/FixableInstantiableError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public enum FixableInstantiableError: DiagnosticError {
public var description: String {
switch self {
case .dependencyHasTooManyAttributes:
return "Dependency can have at most one of @\(Dependency.Source.instantiated), @\(Dependency.Source.inherited), or @\(Dependency.Source.forwarded) attached macro"
return "Dependency can have at most one of @\(Dependency.Source.instantiated), @\(Dependency.Source.received), or @\(Dependency.Source.forwarded) attached macro"
case .dependencyHasInitializer:
return "Dependency must not have hand-written initializer"
case .missingPublicOrOpenAttribute:
Expand Down
30 changes: 15 additions & 15 deletions Sources/SafeDICore/Generators/DependencyTreeGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class DependencyTreeGenerator {
try validateReachableTypeDescriptions()

let typeDescriptionToScopeMap = try createTypeDescriptionToScopeMapping()
try validateUndeclaredInheritedProperties(typeDescriptionToScopeMap: typeDescriptionToScopeMap)
try validateUndeclaredReceivedProperties(typeDescriptionToScopeMap: typeDescriptionToScopeMap)
let rootCombinedScopes = try rootInstantiableTypes
.sorted()
.compactMap { try typeDescriptionToScopeMap[$0]?.createCombinedScope() }
Expand Down Expand Up @@ -94,7 +94,7 @@ public final class DependencyTreeGenerator {
"No `@\(InstantiableVisitor.macroName)`-decorated type found to fulfill `@\(Dependency.Source.instantiated.rawValue)` or `@\(Dependency.Source.lazyInstantiated.rawValue)`-decorated property with type `\(typeDescription.asSource)`"
case let .unfulfillableProperties(unfulfillableProperties):
"""
The following inherited properties were never instantiated:
The following received properties were never instantiated:
\(unfulfillableProperties.map {
"""
`\($0.property.asSource)` is not instantiated in chain: \(([$0.instantiable] + $0.parentStack)
Expand Down Expand Up @@ -130,7 +130,7 @@ public final class DependencyTreeGenerator {
.joined(separator: "\n")
}

/// A collection of `@Instantiable`-decorated types that do not explicitly inherit dependencies.
/// A collection of `@Instantiable`-decorated types that do not explicitly receive dependencies.
/// - Note: These are not necessarily roots in the build graph, since these types may be instantiated by another `@Instantiable`.
private lazy var possibleRootInstantiableTypes: Set<TypeDescription> = Set(
typeDescriptionToFulfillingInstantiableMap
Expand All @@ -140,7 +140,7 @@ public final class DependencyTreeGenerator {
)

/// A collection of `@Instantiable`-decorated types that are instantiated by at least one other
/// `@Instantiable`-decorated type or do not explicitly inherit dependencies.
/// `@Instantiable`-decorated type or do not explicitly receive dependencies.
private lazy var reachableTypeDescriptions: Set<TypeDescription> = {
var reachableTypeDescriptions = Set<TypeDescription>()

Expand Down Expand Up @@ -245,18 +245,18 @@ public final class DependencyTreeGenerator {
return typeDescriptionToScopeMap
}

private func validateUndeclaredInheritedProperties(typeDescriptionToScopeMap: [TypeDescription: Scope]) throws {
private func validateUndeclaredReceivedProperties(typeDescriptionToScopeMap: [TypeDescription: Scope]) throws {
var unfulfillableProperties = Set<DependencyTreeGeneratorError.UnfulfillableProperty>()
func propagateUndeclaredInheritedProperties(
func propagateUndeclaredReceivedProperties(
on scope: Scope,
inheritableProperties: Set<Property>,
receivableProperties: Set<Property>,
instantiables: OrderedSet<Instantiable>
) {
for inheritedProperty in scope.inheritedProperties {
let parentContainsProperty = inheritableProperties.contains(inheritedProperty)
for receivedProperty in scope.receivedProperties {
let parentContainsProperty = receivableProperties.contains(receivedProperty)
if !parentContainsProperty {
unfulfillableProperties.insert(.init(
property: inheritedProperty,
property: receivedProperty,
instantiable: scope.instantiable,
parentStack: instantiables.elements)
)
Expand All @@ -273,18 +273,18 @@ public final class DependencyTreeGenerator {
var instantiables = instantiables
instantiables.insert(scope.instantiable, at: 0)

propagateUndeclaredInheritedProperties(
propagateUndeclaredReceivedProperties(
on: childScope,
inheritableProperties: inheritableProperties.union(scope.properties),
receivableProperties: receivableProperties.union(scope.properties),
instantiables: instantiables
)
}
}

for rootScope in rootInstantiableTypes.compactMap({ typeDescriptionToScopeMap[$0] }) {
propagateUndeclaredInheritedProperties(
propagateUndeclaredReceivedProperties(
on: rootScope,
inheritableProperties: Set(rootScope.properties),
receivableProperties: Set(rootScope.properties),
instantiables: []
)
}
Expand All @@ -310,7 +310,7 @@ extension Dependency {
switch source {
case .instantiated, .lazyInstantiated:
return true
case .forwarded, .inherited:
case .forwarded, .received:
return false
}
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/SafeDICore/Models/CombinedScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ actor CombinedScope {
instantiable: Instantiable,
childPropertyToInstantiableConstant: [Property: Instantiable],
childPropertyToCombinedScopeMap: [Property: CombinedScope],
inheritedProperties: Set<Property>
receivedProperties: Set<Property>
) {
self.instantiable = instantiable
self.childPropertyToInstantiableConstant = childPropertyToInstantiableConstant
self.childPropertyToCombinedScopeMap = childPropertyToCombinedScopeMap
self.inheritedProperties = inheritedProperties
self.receivedProperties = receivedProperties

propertiesToFulfill = (
Array<Property>(childPropertyToInstantiableConstant.keys)
Expand Down Expand Up @@ -131,7 +131,7 @@ actor CombinedScope {

private let childPropertyToInstantiableConstant: [Property: Instantiable]
private let childPropertyToCombinedScopeMap: [Property: CombinedScope]
private let inheritedProperties: Set<Property>
private let receivedProperties: Set<Property>

private let propertiesToFulfill: [Property]
private var resolvedProperties = Set<Property>()
Expand Down Expand Up @@ -174,13 +174,13 @@ actor CombinedScope {

private func hasResolvedAllPropertiesRequired(for combinedScope: CombinedScope) -> Bool {
!combinedScope
.inheritedProperties
.receivedProperties
.contains(where: { !isPropertyResolved($0) })
}

private func isPropertyResolved(_ property: Property) -> Bool {
resolvedProperties.contains(property)
|| inheritedProperties.contains(property)
|| receivedProperties.contains(property)
|| forwardedProperties.contains(property)
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/SafeDICore/Models/Dependency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public struct Dependency: Codable, Hashable {
public enum Source: String, CustomStringConvertible, Codable, Hashable {
case instantiated = "Instantiated"
case lazyInstantiated = "LazyInstantiated"
case inherited = "Inherited"
case received = "Received"
case forwarded = "Forwarded"

public var description: String {
Expand All @@ -45,7 +45,7 @@ public struct Dependency: Codable, Hashable {
/// The label by which this property is referenced inside the `init` method.
var propertyLabelInInit: String {
switch source {
case .instantiated, .inherited, .forwarded:
case .instantiated, .received, .forwarded:
return property.label
case .lazyInstantiated:
return """
Expand Down
2 changes: 1 addition & 1 deletion Sources/SafeDICore/Models/Initializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public struct Initializer: Codable, Hashable {
for dependency in dependencies {
switch dependency.source {
case .instantiated,
.inherited,
.received,
.forwarded:
CodeBlockItemSyntax(
item: .expr(ExprSyntax(InfixOperatorExprSyntax(
Expand Down
6 changes: 3 additions & 3 deletions Sources/SafeDICore/Models/Scope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ final class Scope {
.map(\.property)
}

var inheritedProperties: [Property] {
var receivedProperties: [Property] {
instantiable
.dependencies
.filter {
Expand All @@ -58,7 +58,7 @@ final class Scope {
.instantiated,
.lazyInstantiated:
return false
case .inherited:
case .received:
return true
}
}
Expand Down Expand Up @@ -121,7 +121,7 @@ final class Scope {
instantiable: instantiable,
childPropertyToInstantiableConstant: childPropertyToInstantiableConstant,
childPropertyToCombinedScopeMap: childPropertyToCombinedScopeMap,
inheritedProperties: Set(
receivedProperties: Set(
instantiableStack
.flatMap(\.dependencies)
.filter {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SafeDICore/Visitors/InstantiableVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public final class InstantiableVisitor: SyntaxVisitor {
property: {
switch dependencySource {
case .instantiated,
.inherited,
.received,
.forwarded:
Property(
label: label,
Expand Down
8 changes: 4 additions & 4 deletions Tests/SafeDICoreTests/FileVisitorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final class FileVisitorTests: XCTestCase {
@Forwarded
private let user: User
@Inherited
@Received
let networkService: NetworkService
}
"""))
Expand Down Expand Up @@ -71,7 +71,7 @@ final class FileVisitorTests: XCTestCase {
label: "networkService",
typeDescription: .simple(name: "NetworkService")
),
source: .inherited
source: .received
)
],
isClass: true)
Expand All @@ -97,7 +97,7 @@ final class FileVisitorTests: XCTestCase {
@Forwarded
private let user: User
@Inherited
@Received
let networkService: NetworkService
}
Expand Down Expand Up @@ -131,7 +131,7 @@ final class FileVisitorTests: XCTestCase {
label: "networkService",
typeDescription: .simple(name: "NetworkService")
),
source: .inherited
source: .received
)
],
isClass: true
Expand Down
8 changes: 4 additions & 4 deletions Tests/SafeDIMacrosTests/InjectableMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class InjectableMacroTests: XCTestCase {

let testMacros: [String: Macro.Type] = [
Dependency.Source.instantiated.rawValue: InjectableMacro.self,
Dependency.Source.inherited.rawValue: InjectableMacro.self,
Dependency.Source.received.rawValue: InjectableMacro.self,
Dependency.Source.forwarded.rawValue: InjectableMacro.self,
]

Expand Down Expand Up @@ -107,7 +107,7 @@ final class InjectableMacroTests: XCTestCase {
self.invariantA = invariantA
}
@Inherited
@Received
static let invariantA: InvariantA
}
"""
Expand All @@ -118,8 +118,8 @@ final class InjectableMacroTests: XCTestCase {
self.invariantA = invariantA
}
@Inherited
┬────────
@Received
┬────────
╰─ 🛑 This macro can not decorate `static` variables
static let invariantA: InvariantA
}
Expand Down
16 changes: 8 additions & 8 deletions Tests/SafeDIMacrosTests/InstantiableMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class InstantiableMacroTests: XCTestCase {
let testMacros: [String: Macro.Type] = [
InstantiableVisitor.macroName: InstantiableMacro.self,
Dependency.Source.instantiated.rawValue: InjectableMacro.self,
Dependency.Source.inherited.rawValue: InjectableMacro.self,
Dependency.Source.received.rawValue: InjectableMacro.self,
Dependency.Source.forwarded.rawValue: InjectableMacro.self,
]

Expand Down Expand Up @@ -73,7 +73,7 @@ final class InstantiableMacroTests: XCTestCase {
self.invariantA = invariantA
}
@Inherited
@Received
@Instantiated
let invariantA: InvariantA
}
Expand All @@ -88,8 +88,8 @@ final class InstantiableMacroTests: XCTestCase {
self.invariantA = invariantA
}
@Inherited
╰─ 🛑 Dependency can have at most one of @Instantiated, @Inherited, or @Forwarded attached macro
@Received
╰─ 🛑 Dependency can have at most one of @Instantiated, @Received, or @Forwarded attached macro
✏️ Remove excessive attached macros
@Instantiated
let invariantA: InvariantA
Expand All @@ -105,7 +105,7 @@ final class InstantiableMacroTests: XCTestCase {
self.invariantA = invariantA
}
@Inherited
@Received
@Instantiated
let invariantA: InvariantA
}
Expand Down Expand Up @@ -312,7 +312,7 @@ final class InstantiableMacroTests: XCTestCase {
let variantA: VariantA
@Forwarded
let variantB: VariantB
@Inherited
@Received
let invariantA: InvariantA
}
"""
Expand All @@ -332,7 +332,7 @@ final class InstantiableMacroTests: XCTestCase {
let variantA: VariantA
@Forwarded
let variantB: VariantB
@Inherited
@Received
let invariantA: InvariantA
}
"""
Expand All @@ -356,7 +356,7 @@ final class InstantiableMacroTests: XCTestCase {
let variantA: VariantA
@Forwarded
let variantB: VariantB
@Inherited
@Received
let invariantA: InvariantA
}
"""
Expand Down
Loading

0 comments on commit 626b968

Please sign in to comment.