-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `CasePathIterable` and `CasePathReflectable` protocols While the macro recently introduced iteration (#159) and "reflection" (#158), there's no way to abstract over it. Adding it directly to the `CasePathable` protocol would be potentially source breaking, so instead we can introduce some new protocols. * wip * wip * wip
- Loading branch information
1 parent
57580dd
commit b9ad266
Showing
10 changed files
with
401 additions
and
110 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/// A type that provides a collection of all of its case paths. | ||
/// | ||
/// The ``CasePathable()`` macro automatically generates a conformance to this protocol. | ||
/// | ||
/// You can iterate over ``CasePathable/allCasePaths`` to get access to each individual case path: | ||
/// | ||
/// ```swift | ||
/// @CasePathable enum Field { | ||
/// case title(String) | ||
/// case body(String | ||
/// case isLive | ||
/// } | ||
/// | ||
/// Array(Field.allCasePaths) // [\.title, \.body, \.isLive] | ||
/// ``` | ||
public protocol CasePathIterable: CasePathable | ||
where AllCasePaths: Sequence, AllCasePaths.Element == PartialCaseKeyPath<Self> {} |
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,27 @@ | ||
/// A type that can reflect a case path from a given case. | ||
/// | ||
/// The ``CasePathable()`` macro automatically generates a conformance to this protocol on the | ||
/// enum's ``CasePathable/AllCasePaths`` type. | ||
/// | ||
/// You can look up an enum's case path by passing it to ``subscript(root:)``: | ||
/// | ||
/// ```swift | ||
/// @CasePathable | ||
/// enum Field { | ||
/// case title(String) | ||
/// case body(String) | ||
/// case isLive | ||
/// } | ||
/// | ||
/// Field.allCasePaths[.title("Hello, Blob!")] // \.title | ||
/// ``` | ||
public protocol CasePathReflectable<Root> { | ||
/// The enum type that can be reflected. | ||
associatedtype Root: CasePathable | ||
|
||
/// Returns the case key path for a given root value. | ||
/// | ||
/// - Parameter root: An root value. | ||
/// - Returns: A case path to the root value. | ||
subscript(root: Root) -> PartialCaseKeyPath<Root> { get } | ||
} |
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
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
Oops, something went wrong.