Skip to content

Commit 6718c23

Browse files
committed
Use workflow node sessionId to filter through parent/child structure when building tree
1 parent 7164671 commit 6718c23

File tree

2 files changed

+15
-5
lines changed
  • workflow-trace-viewer/src/jvmMain/kotlin/com/squareup/workflow1/traceviewer

2 files changed

+15
-5
lines changed

workflow-trace-viewer/src/jvmMain/kotlin/com/squareup/workflow1/traceviewer/model/Node.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ package com.squareup.workflow1.traceviewer.model
99
*/
1010
public class Node(
1111
val name: String,
12+
val id: String,
1213
val parent: String,
14+
val parentId: String,
1315
val props: Any? = null,
1416
val state: Any? = null,
1517
val renderings: Any? = null,
1618
val children: List<Node>,
17-
val id: String
1819
) {
1920
override fun toString(): String {
2021
return "Node(name='$name', parent='$parent', children=${children.size})"

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import com.squareup.workflow1.traceviewer.model.Node
88
import io.github.vinceglb.filekit.PlatformFile
99
import io.github.vinceglb.filekit.readString
1010

11+
/*
12+
The root workflow Node uses an ID of 0, and since we are filtering childrenByParent by the
13+
parentId, the root node has a parent of -1 ID. This is reflected seen inside android-register
14+
*/
15+
const val ROOT_ID: String = "-1"
16+
1117
/**
1218
* Parses a given file's JSON String into a list of [Node]s with Moshi adapters. Each of these nodes
1319
* count as the root of a tree which forms a Frame.
@@ -52,25 +58,28 @@ private fun createMoshiAdapter(): JsonAdapter<List<List<Node>>> {
5258

5359
/**
5460
* We take an unparsed render pass and build up a tree structure from it to form a Frame.
61+
*
62+
* @return Node the root node of the tree for that specific frame.
5563
*/
5664
private fun getFrameFromRenderPass(renderPass: List<Node>): Node {
57-
val childrenByParent = renderPass.groupBy { it.parent }
58-
val root = childrenByParent["root"]?.single()
65+
val childrenByParent: Map<String, List<Node>> = renderPass.groupBy { it.parentId }
66+
val root = childrenByParent[ROOT_ID]?.single()
5967
return buildTree(root!!, childrenByParent)
6068
}
6169

6270
/**
6371
* Recursively builds a tree using each node's children.
6472
*/
6573
private fun buildTree(node: Node, childrenByParent: Map<String, List<Node>>): Node {
66-
val children = (childrenByParent[node.name] ?: emptyList())
74+
val children = (childrenByParent[node.id] ?: emptyList())
6775
return Node(
6876
name = node.name,
77+
id = node.id,
6978
parent = node.parent,
79+
parentId = node.parentId,
7080
props = node.props,
7181
state = node.state,
7282
children = children.map { buildTree(it, childrenByParent) },
73-
id = node.id
7483
)
7584
}
7685

0 commit comments

Comments
 (0)