parseNodes function

TreeNode parseNodes(
  1. String path,
  2. TreeNode node
)

Phân tích và xử lý các node trong cây JSON Xử lý các tham chiếu (ref) bằng cách load file JSON tương ứng

path - Đường dẫn thư mục chứa các file JSON node - TreeNode hiện tại cần xử lý Trả về TreeNode đã được xử lý với tất cả tham chiếu được resolve

Implementation

TreeNode parseNodes(String path, TreeNode node) {
  // Xử lý children nếu có
  if (node.children.isNotEmpty) {
    List<TreeNode> processedChildren = [];

    // Xử lý từng child node
    for (var child in node.children) {
      if (child.ref != null) {
        // Nếu child có thuộc tính 'ref', load file JSON tương ứng
        String filePath = '$path/${child.ref}.json';
        String jsonString = File(filePath).readAsStringSync();
        Map<String, dynamic> data = json.decode(jsonString);
        TreeNode loadedNode = TreeNode.fromJson(data);
        // Đệ quy xử lý node được load
        processedChildren.add(parseNodes(path, loadedNode));
      } else {
        // Đệ quy xử lý child node thông thường
        processedChildren.add(parseNodes(path, child));
      }
    }

    // Tạo node mới với children đã được xử lý
    return TreeNode(
      key: node.key,
      label: node.label,
      options: node.options,
      children: processedChildren,
      leadsTo: node.leadsTo,
      script: node.script,
      ref: node.ref,
    );
  }

  return node;
}