-
Notifications
You must be signed in to change notification settings - Fork 47
[task]: attempt to improve debugging info for AnyWorkflowAction #194
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -32,11 +32,13 @@ public protocol WorkflowAction<WorkflowType> { | |
|
||
/// A type-erased workflow action. | ||
/// | ||
/// The `AnyWorkflowAction` type forwards `apply` to an underlying workflow action, hiding its specific underlying type, | ||
/// or to a closure that implements the `apply` logic. | ||
/// The `AnyWorkflowAction` type forwards `apply` to an underlying workflow action, hiding its specific underlying type. | ||
public struct AnyWorkflowAction<WorkflowType: Workflow>: WorkflowAction { | ||
private let _apply: (inout WorkflowType.State) -> WorkflowType.Output? | ||
|
||
/// The underlying type-erased `WorkflowAction` | ||
public let base: Any | ||
|
||
/// Creates a type-erased workflow action that wraps the given instance. | ||
/// | ||
/// - Parameter base: A workflow action to wrap. | ||
|
@@ -46,13 +48,30 @@ public struct AnyWorkflowAction<WorkflowType: Workflow>: WorkflowAction { | |
return | ||
} | ||
self._apply = { return base.apply(toState: &$0) } | ||
self.base = base | ||
} | ||
|
||
/// Creates a type-erased workflow action with the given `apply` implementation. | ||
/// | ||
/// - Parameter apply: the apply function for the resulting action. | ||
public init(_ apply: @escaping (inout WorkflowType.State) -> WorkflowType.Output?) { | ||
self._apply = apply | ||
public init( | ||
_ apply: @escaping (inout WorkflowType.State) -> WorkflowType.Output?, | ||
fileID: StaticString = #fileID, | ||
line: UInt = #line | ||
) { | ||
let closureAction = ClosureAction<WorkflowType>( | ||
_apply: apply, | ||
fileID: fileID, | ||
line: line | ||
) | ||
self.init(closureAction: closureAction) | ||
} | ||
|
||
/// Private initializer forwarded to via `init(_ apply:...)` | ||
/// - Parameter closureAction: The `ClosureAction` wrapping the underlying `apply` closure. | ||
fileprivate init(closureAction: ClosureAction<WorkflowType>) { | ||
self._apply = closureAction.apply(toState:) | ||
self.base = closureAction | ||
} | ||
|
||
public func apply(toState state: inout WorkflowType.State) -> WorkflowType.Output? { | ||
|
@@ -78,3 +97,34 @@ extension AnyWorkflowAction { | |
} | ||
} | ||
} | ||
|
||
// MARK: Closure Action | ||
|
||
/// A `WorkflowAction` that wraps an `apply(...)` implementation defined by a closure. | ||
/// Mainly used to provide more useful debugging/telemetry information for `AnyWorkflow` instances | ||
/// defined via a closure. | ||
struct ClosureAction<WorkflowType: Workflow>: WorkflowAction { | ||
private let _apply: (inout WorkflowType.State) -> WorkflowType.Output? | ||
let fileID: StaticString | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice touch! |
||
let line: UInt | ||
|
||
init( | ||
_apply: @escaping (inout WorkflowType.State) -> WorkflowType.Output?, | ||
fileID: StaticString, | ||
line: UInt | ||
) { | ||
self._apply = _apply | ||
self.fileID = fileID | ||
self.line = line | ||
} | ||
|
||
func apply(toState state: inout WorkflowType.State) -> WorkflowType.Output? { | ||
_apply(&state) | ||
} | ||
} | ||
|
||
extension ClosureAction: CustomStringConvertible { | ||
var description: String { | ||
"\(Self.self)(fileID: \(fileID), line: \(line))" | ||
} | ||
} |
This file contains hidden or 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,117 @@ | ||
/* | ||
* Copyright 2023 Square Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import XCTest | ||
@testable import Workflow | ||
|
||
final class AnyWorkflowActionTests: XCTestCase { | ||
func testRetainsBaseActionTypeInfo() { | ||
let action = ExampleAction() | ||
let erased = AnyWorkflowAction(action) | ||
|
||
XCTAssertEqual(action, erased.base as? ExampleAction) | ||
} | ||
|
||
func testRetainsClosureActionTypeInfo() throws { | ||
do { | ||
let erased = AnyWorkflowAction<ExampleWorkflow> { _ in | ||
nil | ||
} | ||
|
||
XCTAssertNotNil(erased.base as? ClosureAction<ExampleWorkflow>) | ||
} | ||
|
||
do { | ||
let fileID: StaticString = #fileID | ||
// must match line # the initializer is on | ||
let line: UInt = #line; let erased = AnyWorkflowAction<ExampleWorkflow> { _ in | ||
nil | ||
} | ||
|
||
let closureAction = try XCTUnwrap(erased.base as? ClosureAction<ExampleWorkflow>) | ||
XCTAssertEqual("\(closureAction.fileID)", "\(fileID)") | ||
XCTAssertEqual(closureAction.line, line) | ||
} | ||
} | ||
|
||
func testMultipleErasure() { | ||
// standard init | ||
do { | ||
let action = ExampleAction() | ||
let erasedOnce = AnyWorkflowAction(action) | ||
let erasedTwice = AnyWorkflowAction(erasedOnce) | ||
|
||
XCTAssertEqual( | ||
erasedOnce.base as? ExampleAction, | ||
erasedTwice.base as? ExampleAction | ||
) | ||
} | ||
|
||
// closure init | ||
do { | ||
let action = AnyWorkflowAction<ExampleWorkflow> { _ in nil } | ||
let erasedAgain = AnyWorkflowAction(action) | ||
|
||
XCTAssertEqual( | ||
"\(action.base.self)", | ||
"\(erasedAgain.base.self)" | ||
) | ||
} | ||
} | ||
|
||
func testApplyForwarding() { | ||
var log: [String] = [] | ||
let action = ObservableExampleAction { | ||
log.append("action invoked") | ||
} | ||
|
||
let erased = AnyWorkflowAction(action) | ||
|
||
XCTAssertEqual(log, []) | ||
|
||
var state: Void = () | ||
_ = erased.apply(toState: &state) | ||
|
||
XCTAssertEqual(log, ["action invoked"]) | ||
} | ||
} | ||
|
||
private struct ExampleWorkflow: Workflow { | ||
typealias State = Void | ||
typealias Output = Never | ||
typealias Rendering = Void | ||
|
||
func render(state: Void, context: RenderContext<ExampleWorkflow>) {} | ||
} | ||
|
||
private struct ExampleAction: WorkflowAction, Equatable { | ||
typealias WorkflowType = ExampleWorkflow | ||
|
||
func apply(toState state: inout WorkflowType.State) -> WorkflowType.Output? { | ||
return nil | ||
} | ||
} | ||
|
||
private struct ObservableExampleAction: WorkflowAction { | ||
typealias WorkflowType = ExampleWorkflow | ||
|
||
var block: () -> Void = {} | ||
|
||
func apply(toState state: inout WorkflowType.State) -> WorkflowType.Output? { | ||
block() | ||
return nil | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we expose a way for consumers who receive
AnyWorkflowAction
to know if this is wrapping a closure vs. a real action? could maybe makeClosureAction
public to achieve thatThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah we can do that. do you think full access to the underlying type is needed? or would a flag suffice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general my default is "provide more access" but I know as the SDK author its the opposite for you :p for now I suppose a flag would work