Skip to content

Commit 3cfb832

Browse files
committed
Change JSON parsing
Since ActionLogger will only be responsible for logging, the parsing part will be done here, so it involves slightly more complicated logic by building the tree structure from the ground up
1 parent b6bac19 commit 3cfb832

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

workflow-trace-viewer/src/jvmMain/kotlin/com/squareup/workflow1/traceviewer/ui/FrameSelectTab.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public fun FrameSelectTab(
3838
) {
3939
items(frames.size) { index ->
4040
Text(
41-
text = "State ${index + 1}",
41+
text = "Frame ${index + 1}",
4242
color = if (index == currentIndex) Color.Black else Color.LightGray,
4343
modifier = Modifier
4444
.clip(RoundedCornerShape(16.dp))

workflow-trace-viewer/src/jvmMain/kotlin/com/squareup/workflow1/traceviewer/ui/WorkflowTree.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ public fun RenderDiagram(
6464
if (!isLoading) {
6565
DrawTree(frames[frameInd], onNodeSelect)
6666
}
67-
68-
// TODO: catch errors and display UI here
6967
}
7068

7169
/**

workflow-trace-viewer/src/jvmMain/kotlin/com/squareup/workflow1/traceviewer/util/JsonParser.kt

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import io.github.vinceglb.filekit.PlatformFile
99
import io.github.vinceglb.filekit.readString
1010

1111
/**
12-
* Parses a given file's JSON String into [Node] with Moshi adapters.
12+
* Parses a given file's JSON String into a list of [Node]s with Moshi adapters. Each of these nodes
13+
* count as the root of a tree which forms a Frame.
1314
*
1415
* @return A [ParseResult] representing result of parsing, either an error related to the
1516
* format of the JSON, or a success and a parsed trace.
@@ -19,18 +20,58 @@ public suspend fun parseTrace(
1920
): ParseResult {
2021
return try {
2122
val jsonString = file.readString()
22-
val moshi = Moshi.Builder()
23-
.add(KotlinJsonAdapterFactory())
24-
.build()
25-
val workflowList = Types.newParameterizedType(List::class.java, Node::class.java)
26-
val workflowAdapter: JsonAdapter<List<Node>> = moshi.adapter(workflowList)
27-
val trace = workflowAdapter.fromJson(jsonString)
28-
ParseResult.Success(trace)
23+
val workflowAdapter = createMoshiAdapter()
24+
val unParsedTrace = workflowAdapter.fromJson(jsonString)
25+
26+
val parsedTrace = mutableListOf<Node>()
27+
unParsedTrace?.forEach { renderPass ->
28+
val parsed = getFrameFromRenderPass(renderPass)
29+
parsedTrace.add(parsed)
30+
}
31+
32+
ParseResult.Success(parsedTrace)
2933
} catch (e: Exception) {
3034
ParseResult.Failure(e)
3135
}
3236
}
3337

38+
/**
39+
* Creates a Moshi adapter for parsing the JSON trace file.
40+
*/
41+
private fun createMoshiAdapter(): JsonAdapter<List<List<Node>>> {
42+
val moshi = Moshi.Builder()
43+
.add(KotlinJsonAdapterFactory())
44+
.build()
45+
val workflowList = Types.newParameterizedType(List::class.java,
46+
Types.newParameterizedType(List::class.java, Node::class.java))
47+
val adapter: JsonAdapter<List<List<Node>>> = moshi.adapter(workflowList)
48+
return adapter
49+
}
50+
51+
/**
52+
* We take an unparsed render pass and build up a tree structure from it to form a Frame.
53+
*/
54+
private fun getFrameFromRenderPass(renderPass: List<Node>): Node {
55+
val childrenByParent = renderPass.groupBy { it.parent }
56+
val root = childrenByParent["root"]?.single()
57+
return buildTree(root!!, childrenByParent)
58+
}
59+
60+
/**
61+
* Recursively builds a tree using each node's children.
62+
*/
63+
private fun buildTree(node: Node, childrenByParent: Map<String, List<Node>>): Node {
64+
val children = (childrenByParent[node.name] ?: emptyList())
65+
return Node(
66+
name = node.name,
67+
parent = node.parent,
68+
props = node.props,
69+
state = node.state,
70+
children = children.map { buildTree(it, childrenByParent) },
71+
id = node.id
72+
)
73+
}
74+
3475
sealed interface ParseResult {
3576
class Success(val trace: List<Node>?) : ParseResult
3677
class Failure(val error: Throwable) : ParseResult

0 commit comments

Comments
 (0)