Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create OptionalAbstractRegistration type #216

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Sources/Knit/Module/Container+AbstractRegistration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ extension Container {
abstractRegistrations().abstractRegistrations.append(registration)
}

/// Register that a service is expected to exist but no implementation is currently available
/// The concrete implementation must be registered or the dependency graph is considered invalid
/// - NOTE: We don't currently support abstract registrations with arguments
/// As this is an `Optional` Service type this allows special handling of the abstract registration for test environments:
/// If during testing and no concrete registration is available, then `nil` will be resolved automatically.
public func registerAbstract<Service>(
_ serviceType: Optional<Service>.Type,
name: String? = nil,
file: String = #fileID
) {
let registration = OptionalAbstractRegistration<Service>(name: name, file: file)
abstractRegistrations().abstractRegistrations.append(registration)
}

// Must be called before using `registerAbstract`
func registerAbstractContainer() -> AbstractRegistrationContainer {
let registrations = AbstractRegistrationContainer()
Expand Down Expand Up @@ -91,6 +105,29 @@ fileprivate struct RealAbstractRegistration<ServiceType>: AbstractRegistration {
}
}

/// An abstract registration for an optional service
fileprivate struct OptionalAbstractRegistration<ServiceType>: AbstractRegistration {
let name: String?
// Source file used for debugging. Not included in hash calculation or equality
let file: String

var serviceType: ServiceType.Type { ServiceType.self }

var key: RegistrationKey {
return .init(typeIdentifier: ObjectIdentifier(ServiceType.self), name: name)
}

func registerPlaceholder(
container: Container,
errorFormatter: ModuleAssemblerErrorFormatter,
dependencyTree: DependencyTree
) {
container.register(Optional<ServiceType>.self, name: name) { _ in
return nil
}
}
}

// MARK: - Inner types

extension Container {
Expand Down
18 changes: 18 additions & 0 deletions Tests/KnitTests/AbstractRegistrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ final class AbstractRegistrationTests: XCTestCase {
)
}

@MainActor
func testOptionalAbstractRegistrations() {
let assembler = ModuleAssembler([Assembly3()])
let string = assembler.resolver.resolve(String?.self) ?? nil
XCTAssertNil(string)

let int = assembler.resolver.resolve(Optional<Int>.self) ?? nil
Comment on lines +79 to +82
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are there ?? nil on the resolution calls?

Copy link
Collaborator Author

@skorulis-ap skorulis-ap Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It converts from Int?? to Int?

XCTAssertNil(int)
}

}

private struct Assembly1: AutoInitModuleAssembly {
Expand All @@ -86,3 +96,11 @@ private struct Assembly2: AutoInitModuleAssembly {
container.registerAbstract(String.self)
}
}

private struct Assembly3: AutoInitModuleAssembly {
static var dependencies: [any ModuleAssembly.Type] { [] }
func assemble(container: Container) {
container.registerAbstract(Optional<String>.self)
container.registerAbstract(Int?.self)
}
}