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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions Sources/Knit/Module/Container+AbstractRegistration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ extension Container {
abstractRegistrations().abstractRegistrations.append(registration)
}

/// Register that a service is expected to exist but no implementation is currently available
/// If no concrete registration is available then nil will be resolved
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 +102,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
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)
}
}