Skip to content

Commit

Permalink
Merge pull request #2 from riiid/feature/reducer-support
Browse files Browse the repository at this point in the history
Add Reducer parsing
  • Loading branch information
korJAEYOUNGYUN authored Oct 6, 2023
2 parents 14840cf + 7748ff1 commit 1c1d933
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Sources/TCADiagramLib/Internal/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ extension SourceFileSyntax {
private func predicateReducerProtocol(_ node: Syntax) throws -> String? {
if
let node = StructDeclSyntax(node),
node.inheritanceClause?.tokens(viewMode: .fixedUp).contains(where: { $0.tokenKind == .identifier("ReducerProtocol") }) == true
node.inheritanceClause?.tokens(viewMode: .fixedUp)
.contains(where: {
$0.tokenKind == .identifier("ReducerProtocol")
|| $0.tokenKind == .identifier("Reducer")
}) == true
{
return node.identifier.text
}
Expand Down
22 changes: 22 additions & 0 deletions Tests/TCADiagramLibTests/DiagramTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,26 @@ final class DiagramTests: XCTestCase {
"""
XCTAssertEqual(result, expected)
}

func testReducerExample() throws {
let result = try Diagram.dump(reducerSampleSource)
let expected = """
```mermaid
%%{ init : { "theme" : "default", "flowchart" : { "curve" : "monotoneY" }}}%%
graph LR
SelfLessonDetail -- optional --> DoubleIfLetChild
SelfLessonDetail ---> DoubleScopeChild
SelfLessonDetail ---> Payment
SelfLessonDetail -- optional --> SantaWeb
SelfLessonDetail -- optional --> SelfLessonDetailFilter
DoubleIfLetChild(DoubleIfLetChild: 1)
DoubleScopeChild(DoubleScopeChild: 1)
Payment(Payment: 1)
SantaWeb(SantaWeb: 1)
SelfLessonDetailFilter(SelfLessonDetailFilter: 1)
```
"""
XCTAssertEqual(result, expected)
}
}
63 changes: 63 additions & 0 deletions Tests/TCADiagramLibTests/Resources/Sources.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,66 @@ let reducerProtocolSampleSource: [String] = [
}
"""
]

let reducerSampleSource: [String] = [
"""
public struct SelfLessonDetail: Reducer {
@Dependency(\\.environmentSelfLessonDetail) private var environment
public init() {}
public var body: some Reducer<State, Action> {
BindingReducer()
Scope(state: \\State.payment, action: /Action.payment) {
Payment()
}
Scope(state: \\.subState, action: .self) {
Scope(
state: /State.SubState.promotionWeb,
action: /Action.promotionWeb
) {
DoubleScopeChild()
}
}
Reduce { state, action in
switch action {
case default:
return .none
}
}
.ifLet(\\.filter, action: /Action.filter) {
SelfLessonDetailFilter()
}
.ifLet(\\.selection, action: /Action.web) {
SantaWeb()
}
.ifLet(\\SelfLessonDetail.State.selection, action: /SelfLessonDetail.Action.webView) {
EmptyReducer()
.ifLet(\\Identified.value, action: .self) {
DoubleIfLetChild()
}
}
}
}
""",
"""
extension SelfLessonDetail {
public enum Action: Equatable {
}
}
extension Payment {
public enum Action: Equatable {
}
}
extension SantaWeb {
public enum Action: Equatable {
}
}
extension SelfLessonDetailFilter {
public enum Action: Equatable {
}
}
"""
]

0 comments on commit 1c1d933

Please sign in to comment.