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

Case Paths + Key Paths = Optional Paths #190

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/swiftlang/swift-syntax", "509.0.0"..<"601.0.0-prerelease"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.2.2"),
.package(url: "https://github.com/pointfreeco/swift-macro-testing", from: "0.2.0"),
.package(url: "https://github.com/pointfreeco/swift-macro-testing", from: "0.5.2"),
],
targets: [
.target(
Expand Down
4 changes: 2 additions & 2 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/swiftlang/swift-syntax", "509.0.0"..<"601.0.0-prerelease"),
.package(url: "https://github.com/pointfreeco/swift-macro-testing", from: "0.5.2"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.2.2"),
.package(url: "https://github.com/pointfreeco/swift-macro-testing", from: "0.2.0"),
],
targets: [
.target(
Expand Down Expand Up @@ -50,7 +50,7 @@ let package = Package(
]
),
],
swiftLanguageVersions: [.v6]
swiftLanguageModes: [.v6]
)

#if !os(Windows)
Expand Down
67 changes: 67 additions & 0 deletions Sources/CasePaths/AnyOptionalPath.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import Foundation

/// A type-erased optional path that supports extracting an optional value from a root, and
/// non-optionally updating a value when present.
///
/// This type defines key path-like semantics for optional-chaining.
public struct AnyOptionalPath<Root, Value>: Sendable {
private let _get: @Sendable (Root) -> Value?
private let _set: @Sendable (inout Root, Value) -> Void

/// Creates a type-erased optional path from a pair of functions.
///
/// - Parameters:
/// - get: A function that can optionally fail in extracting a value from a root.
/// - set: A function that always succeeds in updating a value in a root when present.
public init(
get: @escaping @Sendable (Root) -> Value?,
set: @escaping @Sendable (inout Root, Value) -> Void
) {
self._get = get
self._set = set
}

/// Creates a type-erased optional path from a type-erased case path.
///
/// - Parameters:
/// - get: A function that can optionally fail in extracting a value from a root.
/// - set: A function that always succeeds in updating a value in a root when present.
public init(_ casePath: AnyCasePath<Root, Value>) {
self.init(get: casePath.extract) { $0 = casePath.embed($1) }
}

/// Attempts to extract a value from a root.
///
/// - Parameter root: A root to extract from.
/// - Returns: A value if it can be extracted from the given root, otherwise `nil`.
public func extract(from root: Root) -> Value? {
self._get(root)
}

/// Returns a root by embedding a value.
///
/// - Parameters:
/// - root: A root to modify.
/// - value: A value to update in the root when an existing value is present.
public func set(into root: inout Root, _ value: Value) {
self._set(&root, value)
}
}

extension AnyOptionalPath where Root == Value {
/// The identity optional path.
///
/// An optional path that:
///
/// * Given a value to extract, returns the given value.
/// * Given a value to update, replaces the given value.
public init() where Root == Value {
self.init(get: { $0 }, set: { $0 = $1 })
}
}

extension AnyOptionalPath: CustomDebugStringConvertible {
public var debugDescription: String {
"AnyOptionalPath<\(typeName(Root.self)), \(typeName(Value.self))>"
}
}
2 changes: 1 addition & 1 deletion Sources/CasePaths/CasePathIterable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
/// Array(Field.allCasePaths) // [\.title, \.body, \.isLive]
/// ```
public protocol CasePathIterable: CasePathable
where AllCasePaths: Sequence, AllCasePaths.Element == PartialCaseKeyPath<Self> {}
where AllCasePaths: Sequence, AllCasePaths.Element == PartialOptionalKeyPath<Self> {}
2 changes: 1 addition & 1 deletion Sources/CasePaths/CasePathReflectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ public protocol CasePathReflectable<Root> {
///
/// - Parameter root: An root value.
/// - Returns: A case path to the root value.
subscript(root: Root) -> PartialCaseKeyPath<Root> { get }
subscript(root: Root) -> PartialOptionalKeyPath<Root> { get }
}
Loading