-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforcement of customizable type constraints for DependencyBuilder (#93)
- Loading branch information
1 parent
092eabc
commit d56ed07
Showing
8 changed files
with
156 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
Sources/Spezi/Dependencies/DependencyCollectionBuilder.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
|
||
/// A protocol enabling the implementation of a result builder to build a ``DependencyCollection``. | ||
/// Enables the simple construction of a result builder accepting ``Module``s with additional type constraints (useful for DSL implementations). | ||
/// | ||
/// Upon conformance, developers are required to implement a single result builder function transforming an arbitrary ``Module`` type constraint (M in the example below) to a ``DependencyCollection``. | ||
/// All other result builder functions for constructing a ``DependencyCollection`` are provided as a default protocol implementation. | ||
/// ```swift | ||
/// static func buildExpression<M: Module>(_ expression: @escaping @autoclosure () -> M) -> DependencyCollection | ||
/// ``` | ||
/// | ||
/// See ``DependencyCollection/init(for:singleEntry:)`` for an example conformance implementation of the ``DependencyCollectionBuilder``. | ||
public protocol DependencyCollectionBuilder {} | ||
|
||
|
||
/// Default protocol implementations of a result builder constructing a ``DependencyCollection``. | ||
extension DependencyCollectionBuilder { | ||
/// Build a block of ``DependencyCollection``s. | ||
public static func buildBlock(_ components: DependencyCollection...) -> DependencyCollection { | ||
buildArray(components) | ||
} | ||
|
||
/// Build the first block of an conditional ``DependencyCollection`` component. | ||
public static func buildEither(first component: DependencyCollection) -> DependencyCollection { | ||
component | ||
} | ||
|
||
/// Build the second block of an conditional ``DependencyCollection`` component. | ||
public static func buildEither(second component: DependencyCollection) -> DependencyCollection { | ||
component | ||
} | ||
|
||
/// Build an optional ``DependencyCollection`` component. | ||
public static func buildOptional(_ component: DependencyCollection?) -> DependencyCollection { | ||
component ?? DependencyCollection() | ||
} | ||
|
||
/// Build an ``DependencyCollection`` component with limited availability. | ||
public static func buildLimitedAvailability(_ component: DependencyCollection) -> DependencyCollection { | ||
component | ||
} | ||
|
||
/// Build an array of ``DependencyCollection`` components. | ||
public static func buildArray(_ components: [DependencyCollection]) -> DependencyCollection { | ||
DependencyCollection(components.reduce(into: []) { result, component in | ||
result.append(contentsOf: component.entries) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
Tests/SpeziTests/DependenciesTests/DependencyBuilderTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import Spezi | ||
import XCTRuntimeAssertions | ||
|
||
private protocol ExampleTypeConstraint: Module {} | ||
|
||
private final class ExampleDependentModule: ExampleTypeConstraint {} | ||
|
||
@resultBuilder | ||
private enum ExampleDependencyBuilder: DependencyCollectionBuilder { | ||
/// An auto-closure expression, providing the default dependency value, building the ``DependencyCollection``. | ||
static func buildExpression<L: ExampleTypeConstraint>(_ expression: @escaping @autoclosure () -> L) -> DependencyCollection { | ||
DependencyCollection(singleEntry: expression) | ||
} | ||
} | ||
|
||
class ExampleDependencyModule: Module { | ||
@Dependency var dependencies: [any Module] | ||
|
||
|
||
init( | ||
@ExampleDependencyBuilder _ dependencies: () -> DependencyCollection | ||
) { | ||
self._dependencies = Dependency(using: dependencies()) | ||
} | ||
} | ||
|
||
|
||
final class DependencyBuilderTests: XCTestCase { | ||
func testDependencyBuilder() throws { | ||
let module = ExampleDependencyModule { | ||
ExampleDependentModule() | ||
} | ||
let sortedModules = DependencyManager.resolve([module]) | ||
XCTAssertEqual(sortedModules.count, 2) | ||
_ = try XCTUnwrap(sortedModules[0] as? ExampleDependentModule) | ||
_ = try XCTUnwrap(sortedModules[1] as? ExampleDependencyModule) | ||
} | ||
} |