diff --git a/Sources/SafeDI/LazyForwardingInstantiator.swift b/Sources/SafeDI/ForwardingInstantiator.swift similarity index 87% rename from Sources/SafeDI/LazyForwardingInstantiator.swift rename to Sources/SafeDI/ForwardingInstantiator.swift index 07a0d589..a2a27673 100644 --- a/Sources/SafeDI/LazyForwardingInstantiator.swift +++ b/Sources/SafeDI/ForwardingInstantiator.swift @@ -18,17 +18,17 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -/// A SafeDI dependency designed for the lazy instantiation of an `@Instantiable` type that contains +/// 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 `InstantiableType`, as well as by all types `@Instantiated` downstream. /// Instantiation is thread-safe. /// -/// - SeeAlso: `LazyInstantiator` +/// - SeeAlso: `Instantiator` /// - Note: This class is the sole means for instantiating an `@Instantiable` type with `@Forwarded` /// properties within the SafeDI framework. -public final class LazyForwardingInstantiator { - /// Initializes a new lazy forwarding instantiator with the provided instantiation closure. +public final class ForwardingInstantiator { + /// Initializes a new forwarding instantiator with the provided instantiation closure. /// /// - Parameter instantiator: A closure that takes `ArgumentsToForward` and returns an instance of `InstantiableType`. public init(_ instantiator: @escaping (ArgumentsToForward) -> InstantiableType) { diff --git a/Sources/SafeDI/LazyInstantiator.swift b/Sources/SafeDI/Instantiator.swift similarity index 89% rename from Sources/SafeDI/LazyInstantiator.swift rename to Sources/SafeDI/Instantiator.swift index 92a1d052..384b64f1 100644 --- a/Sources/SafeDI/LazyInstantiator.swift +++ b/Sources/SafeDI/Instantiator.swift @@ -18,13 +18,13 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -/// A SafeDI dependency responsible for the lazy instantiation of an `@Instantiable` type. +/// A SafeDI dependency responsible for the deferred instantiation of an `@Instantiable` type. /// This class facilitates the delayed creation of an `@Instantiable` instance, making it particularly -/// useful in scenarios where immediate instantiation is not necessary or desirable. `LazyInstantiator` +/// useful in scenarios where immediate instantiation is not necessary or desirable. `Instantiator` /// facilitates control over memory usage and enables just-in-time instantiation. Instantiation is thread-safe. /// -/// - SeeAlso: `LazyForwardingInstantiator` -public final class LazyInstantiator { +/// - SeeAlso: `ForwardingInstantiator` +public final class Instantiator { /// - Parameter instantiator: A closure that returns an instance of `InstantiableType`. public init(_ instantiator: @escaping () -> InstantiableType) { self.instantiator = instantiator diff --git a/Sources/SafeDI/LazyInstantiated.swift b/Sources/SafeDI/LazyInstantiated.swift index ce241187..e845855f 100644 --- a/Sources/SafeDI/LazyInstantiated.swift +++ b/Sources/SafeDI/LazyInstantiated.swift @@ -25,8 +25,8 @@ import Foundation // MARK: Initialization - public init(synchronization: SynchronizationBehavior = .main, _ builder: LazyInstantiator) { - self.lazyInstantiator = builder + public init(synchronization: SynchronizationBehavior = .main, _ builder: Instantiator) { + self.instantiator = builder self.synchronization = synchronization } @@ -37,7 +37,7 @@ import Foundation if let instantiated = self._unsafeInstantiated { return instantiated } else { - let instantiated = self.lazyInstantiator.instantiate() + let instantiated = self.instantiator.instantiate() self._unsafeInstantiated = instantiated return instantiated } @@ -46,7 +46,7 @@ import Foundation // MARK: Private - private let lazyInstantiator: LazyInstantiator + private let instantiator: Instantiator private let synchronization: SynchronizationBehavior private var _unsafeInstantiated: InstantiableType? diff --git a/Sources/SafeDICore/Generators/DependencyTreeGenerator.swift b/Sources/SafeDICore/Generators/DependencyTreeGenerator.swift index 1aec0032..5d503a7b 100644 --- a/Sources/SafeDICore/Generators/DependencyTreeGenerator.swift +++ b/Sources/SafeDICore/Generators/DependencyTreeGenerator.swift @@ -332,9 +332,9 @@ extension Property { fileprivate var nonLazyPropertyType: Scope.PropertyToInstantiate.PropertyType { switch typeDescription { case let .simple(name, _): - if name == Dependency.lazyInstantiatorType { + if name == Dependency.instantiatorType { return .instantiator - } else if name == Dependency.lazyForwardingInstantiatorType { + } else if name == Dependency.forwardingInstantiatorType { return .forwardingInstantiator } else { return .constant @@ -368,11 +368,11 @@ extension TypeDescription { fileprivate var asInstantiatedType: TypeDescription { switch self { case let .simple(name, generics): - if name == Dependency.lazyInstantiatorType, let builtType = generics.first { + if name == Dependency.instantiatorType, let builtType = generics.first { // This is a type that is lazily instantiated. // The first generic is the built type. return builtType - } else if name == Dependency.lazyForwardingInstantiatorType, let builtType = generics.last { + } else if name == Dependency.forwardingInstantiatorType, let builtType = generics.last { // This is a type that is lazily instantiated with forwarded arguments. // The last generic is the built type. return builtType diff --git a/Sources/SafeDICore/Models/Dependency.swift b/Sources/SafeDICore/Models/Dependency.swift index 61a78fba..3af87401 100644 --- a/Sources/SafeDICore/Models/Dependency.swift +++ b/Sources/SafeDICore/Models/Dependency.swift @@ -65,7 +65,7 @@ public struct Dependency: Codable, Hashable { case .instantiated, .inherited, .singleton, .forwarded: return property.label case .lazyInstantiated: - return "\(property.label)\(Self.lazyInstantiatorType)" + return "\(property.label)\(Self.instantiatorType)" } } @@ -77,12 +77,12 @@ public struct Dependency: Codable, Hashable { case .lazyInstantiated: // TODO: fully qualify this type with `SafeDI.` member prefix return .simple( - name: Self.lazyInstantiatorType, + name: Self.instantiatorType, generics: [property.typeDescription] ) } } - static let lazyInstantiatorType = "LazyInstantiator" - static let lazyForwardingInstantiatorType = "LazyForwardingInstantiator" + static let instantiatorType = "Instantiator" + static let forwardingInstantiatorType = "ForwardingInstantiator" } diff --git a/Tests/SafeDICoreTests/InitializerTests.swift b/Tests/SafeDICoreTests/InitializerTests.swift index a69a6f00..02128c12 100644 --- a/Tests/SafeDICoreTests/InitializerTests.swift +++ b/Tests/SafeDICoreTests/InitializerTests.swift @@ -755,9 +755,9 @@ final class InitializerTests: XCTestCase { hasGenericWhereClause: false, arguments: [ .init( - innerLabel: "invariantLazyInstantiator", + innerLabel: "invariantInstantiator", typeDescription: .simple( - name: "LazyInstantiator", + name: "Instantiator", generics: [.simple(name: "Invariant")] ) ), @@ -769,9 +769,9 @@ final class InitializerTests: XCTestCase { fulfilling: [ .init( property: .init( - label: "invariantLazyInstantiator", + label: "invariantInstantiator", typeDescription: .simple( - name: "LazyInstantiator", + name: "Instantiator", generics: [.simple(name: "Invariant")] ) ), @@ -781,9 +781,9 @@ final class InitializerTests: XCTestCase { typeIsClass: false, trailingNewline: true).description, """ - public init(buildSafeDIDependencies: () -> (LazyInstantiator)) { + public init(buildSafeDIDependencies: () -> (Instantiator)) { let dependencies = buildSafeDIDependencies() - self.init(invariantLazyInstantiator: dependencies) + self.init(invariantInstantiator: dependencies) } """ ) @@ -798,9 +798,9 @@ final class InitializerTests: XCTestCase { hasGenericWhereClause: false, arguments: [ .init( - innerLabel: "invariantLazyInstantiator", + innerLabel: "invariantInstantiator", typeDescription: .simple( - name: "LazyInstantiator", + name: "Instantiator", generics: [.simple(name: "Invariant")] ) ), @@ -821,9 +821,9 @@ final class InitializerTests: XCTestCase { typeIsClass: false, trailingNewline: true).description, """ - public init(buildSafeDIDependencies: () -> (LazyInstantiator)) { + public init(buildSafeDIDependencies: () -> (Instantiator)) { let dependencies = buildSafeDIDependencies() - self.init(invariantLazyInstantiator: dependencies) + self.init(invariantInstantiator: dependencies) } """ ) @@ -838,9 +838,9 @@ final class InitializerTests: XCTestCase { hasGenericWhereClause: false, arguments: [ .init( - innerLabel: "invariantLazyInstantiator", + innerLabel: "invariantInstantiator", typeDescription: .simple( - name: "LazyInstantiator", + name: "Instantiator", generics: [.simple(name: "Invariant")] ) ), @@ -852,9 +852,9 @@ final class InitializerTests: XCTestCase { fulfilling: [ .init( property: .init( - label: "invariantLazyInstantiator", + label: "invariantInstantiator", typeDescription: .simple( - name: "LazyInstantiator", + name: "Instantiator", generics: [.simple(name: "Invariant")] ) ), @@ -871,9 +871,9 @@ final class InitializerTests: XCTestCase { typeIsClass: false, trailingNewline: true).description, """ - public init(buildSafeDIDependencies: () -> (LazyInstantiator)) { + public init(buildSafeDIDependencies: () -> (Instantiator)) { let dependencies = buildSafeDIDependencies() - self.init(invariantLazyInstantiator: dependencies) + self.init(invariantInstantiator: dependencies) } """ ) diff --git a/Tests/SafeDIMacrosTests/InstantiableMacroTests.swift b/Tests/SafeDIMacrosTests/InstantiableMacroTests.swift index e6a71e2e..80037fa3 100644 --- a/Tests/SafeDIMacrosTests/InstantiableMacroTests.swift +++ b/Tests/SafeDIMacrosTests/InstantiableMacroTests.swift @@ -129,8 +129,8 @@ final class InstantiableMacroTests: XCTestCase { """ @Instantiable public struct ExampleService { - init(invariantALazyInstantiator: LazyInstantiator) { - _invariantA = LazyInstantiated(invariantALazyInstantiator) + init(invariantAInstantiator: Instantiator) { + _invariantA = LazyInstantiated(invariantAInstantiator) } @LazyInstantiated @@ -139,16 +139,16 @@ final class InstantiableMacroTests: XCTestCase { """, expandedSource: """ public struct ExampleService { - init(invariantALazyInstantiator: LazyInstantiator) { - _invariantA = LazyInstantiated(invariantALazyInstantiator) + init(invariantAInstantiator: Instantiator) { + _invariantA = LazyInstantiated(invariantAInstantiator) } @LazyInstantiated private var invariantA: InvariantA - public init(buildSafeDIDependencies: () -> (LazyInstantiator)) { + public init(buildSafeDIDependencies: () -> (Instantiator)) { let dependencies = buildSafeDIDependencies() - self.init(invariantALazyInstantiator: dependencies) + self.init(invariantAInstantiator: dependencies) } } """, @@ -791,13 +791,13 @@ final class InstantiableMacroTests: XCTestCase { } } - func test_fixit_addsFixitMissingRequiredInitializerWhenLazyInstantiatorDependencyMissingFromInit() { + func test_fixit_addsFixitMissingRequiredInitializerWhenInstantiatorDependencyMissingFromInit() { assertMacro { """ @Instantiable public struct ExampleService { @Instantiated - private let invariantALazyInstantiator: LazyInstantiator + private let invariantAInstantiator: Instantiator } """ } diagnostics: { @@ -807,32 +807,32 @@ final class InstantiableMacroTests: XCTestCase { ╰─ 🛑 @Instantiable-decorated type must have initializer for all injected parameters ✏️ Add required initializer @Instantiated - private let invariantALazyInstantiator: LazyInstantiator + private let invariantAInstantiator: Instantiator } """ } fixes: { """ @Instantiable public struct ExampleService { - init(invariantALazyInstantiator: LazyInstantiator) { - self.invariantALazyInstantiator = invariantALazyInstantiator + init(invariantAInstantiator: Instantiator) { + self.invariantAInstantiator = invariantAInstantiator } @Instantiated - private let invariantALazyInstantiator: LazyInstantiator + private let invariantAInstantiator: Instantiator } """ } expansion: { """ public struct ExampleService { - init(invariantALazyInstantiator: LazyInstantiator) { - self.invariantALazyInstantiator = invariantALazyInstantiator + init(invariantAInstantiator: Instantiator) { + self.invariantAInstantiator = invariantAInstantiator } - private let invariantALazyInstantiator: LazyInstantiator + private let invariantAInstantiator: Instantiator - public init(buildSafeDIDependencies: () -> (LazyInstantiator)) { + public init(buildSafeDIDependencies: () -> (Instantiator)) { let dependencies = buildSafeDIDependencies() - self.init(invariantALazyInstantiator: dependencies) + self.init(invariantAInstantiator: dependencies) } } """ @@ -863,8 +863,8 @@ final class InstantiableMacroTests: XCTestCase { """ @Instantiable public struct ExampleService { - init(invariantALazyInstantiator: LazyInstantiator) { - _invariantA = LazyInstantiated(invariantALazyInstantiator) + init(invariantAInstantiator: Instantiator) { + _invariantA = LazyInstantiated(invariantAInstantiator) } @LazyInstantiated @@ -874,23 +874,23 @@ final class InstantiableMacroTests: XCTestCase { } expansion: { """ public struct ExampleService { - init(invariantALazyInstantiator: LazyInstantiator) { - _invariantA = LazyInstantiated(invariantALazyInstantiator) + init(invariantAInstantiator: Instantiator) { + _invariantA = LazyInstantiated(invariantAInstantiator) } @LazyInstantiated private var invariantA: InvariantA - public init(buildSafeDIDependencies: () -> (LazyInstantiator)) { + public init(buildSafeDIDependencies: () -> (Instantiator)) { let dependencies = buildSafeDIDependencies() - self.init(invariantALazyInstantiator: dependencies) + self.init(invariantAInstantiator: dependencies) } } """ } } - func test_fixit_addsFixitMissingRequiredInitializerWhenLazyConstructedAndLazyInstantiatorDependencyOfSameTypeMissingFromInit() { + func test_fixit_addsFixitMissingRequiredInitializerWhenLazyConstructedAndInstantiatorDependencyOfSameTypeMissingFromInit() { assertMacro { """ @Instantiable @@ -898,7 +898,7 @@ final class InstantiableMacroTests: XCTestCase { @LazyInstantiated private var invariantA: InvariantA @Instantiated - let invariantALazyInstantiator: LazyInstantiator + let invariantAInstantiator: Instantiator } """ } diagnostics: { @@ -910,39 +910,39 @@ final class InstantiableMacroTests: XCTestCase { @LazyInstantiated private var invariantA: InvariantA @Instantiated - let invariantALazyInstantiator: LazyInstantiator + let invariantAInstantiator: Instantiator } """ } fixes: { """ @Instantiable public struct ExampleService { - init(invariantALazyInstantiator: LazyInstantiator) { - _invariantA = LazyInstantiated(invariantALazyInstantiator) - self.invariantALazyInstantiator = invariantALazyInstantiator + init(invariantAInstantiator: Instantiator) { + _invariantA = LazyInstantiated(invariantAInstantiator) + self.invariantAInstantiator = invariantAInstantiator } @LazyInstantiated private var invariantA: InvariantA @Instantiated - let invariantALazyInstantiator: LazyInstantiator + let invariantAInstantiator: Instantiator } """ } expansion: { """ public struct ExampleService { - init(invariantALazyInstantiator: LazyInstantiator) { - _invariantA = LazyInstantiated(invariantALazyInstantiator) - self.invariantALazyInstantiator = invariantALazyInstantiator + init(invariantAInstantiator: Instantiator) { + _invariantA = LazyInstantiated(invariantAInstantiator) + self.invariantAInstantiator = invariantAInstantiator } @LazyInstantiated private var invariantA: InvariantA - let invariantALazyInstantiator: LazyInstantiator + let invariantAInstantiator: Instantiator - public init(buildSafeDIDependencies: () -> (LazyInstantiator)) { + public init(buildSafeDIDependencies: () -> (Instantiator)) { let dependencies = buildSafeDIDependencies() - self.init(invariantALazyInstantiator: dependencies) + self.init(invariantAInstantiator: dependencies) } } """ diff --git a/Tests/SafeDITests/LazyForwardingInstantiatorTests.swift b/Tests/SafeDITests/ForwardingInstantiatorTests.swift similarity index 91% rename from Tests/SafeDITests/LazyForwardingInstantiatorTests.swift rename to Tests/SafeDITests/ForwardingInstantiatorTests.swift index ff4cc878..b28f2c56 100644 --- a/Tests/SafeDITests/LazyForwardingInstantiatorTests.swift +++ b/Tests/SafeDITests/ForwardingInstantiatorTests.swift @@ -22,9 +22,9 @@ import XCTest @testable import SafeDI -final class LazyForwardingInstantiatorTests: XCTestCase { +final class ForwardingInstantiatorTests: XCTestCase { func test_instantiate_returnsNewObjectEachTime() { - let systemUnderTest = LazyForwardingInstantiator() { id in BuiltProduct(id: id) } + let systemUnderTest = ForwardingInstantiator() { id in BuiltProduct(id: id) } let id = UUID().uuidString let firstBuiltProduct = systemUnderTest.instantiate(id) let secondBuiltProduct = systemUnderTest.instantiate(id) diff --git a/Tests/SafeDITests/LazyInstantiatorTests.swift b/Tests/SafeDITests/InstantiatorTests.swift similarity index 93% rename from Tests/SafeDITests/LazyInstantiatorTests.swift rename to Tests/SafeDITests/InstantiatorTests.swift index 9ea19a6f..d2f66e6d 100644 --- a/Tests/SafeDITests/LazyInstantiatorTests.swift +++ b/Tests/SafeDITests/InstantiatorTests.swift @@ -22,9 +22,9 @@ import XCTest @testable import SafeDI -final class LazyInstantiatorTests: XCTestCase { +final class InstantiatorTests: XCTestCase { func test_instantiate_returnsNewObjectEachTime() { - let systemUnderTest = LazyInstantiator() { BuiltProduct() } + let systemUnderTest = Instantiator() { BuiltProduct() } let firstBuiltProduct = systemUnderTest.instantiate() let secondBuiltProduct = systemUnderTest.instantiate() XCTAssertNotEqual(firstBuiltProduct, secondBuiltProduct) diff --git a/Tests/SafeDITests/LazyInstantiatedTests.swift b/Tests/SafeDITests/LazyInstantiatedTests.swift index 5649170a..4a5f6f8e 100644 --- a/Tests/SafeDITests/LazyInstantiatedTests.swift +++ b/Tests/SafeDITests/LazyInstantiatedTests.swift @@ -24,7 +24,7 @@ import XCTest final class LazyInstantiatedTests: XCTestCase { func test_wrappedValue_whenSynchronizingOnMainQueue_returnsSameInstanceEachTime() { - let systemUnderTest = LazyInstantiated(synchronization: .main, LazyInstantiator { BuiltProduct() }) + let systemUnderTest = LazyInstantiated(synchronization: .main, Instantiator { BuiltProduct() }) let firstBuiltProduct = systemUnderTest.wrappedValue let secondBuiltProduct = systemUnderTest.wrappedValue XCTAssertEqual(firstBuiltProduct, secondBuiltProduct) @@ -32,7 +32,7 @@ final class LazyInstantiatedTests: XCTestCase { } func test_wrappedValue_whenSynchronizingWithLock_returnsSameInstanceEachTime() { - let systemUnderTest = LazyInstantiated(synchronization: .lock(), LazyInstantiator { BuiltProduct() }) + let systemUnderTest = LazyInstantiated(synchronization: .lock(), Instantiator { BuiltProduct() }) let firstBuiltProduct = systemUnderTest.wrappedValue let secondBuiltProduct = systemUnderTest.wrappedValue XCTAssertEqual(firstBuiltProduct, secondBuiltProduct)