jsonToOpenUI function

String jsonToOpenUI(
  1. Object json,
  2. OpenUiLibrary library, {
  3. Map<String, Object?>? stateDeclarations,
})

lang-core jsonToOpenUI: an element tree back to program text that mergeStatements accepts. json is an ElementNode (a parse result's root) or its JSON form (ElementNode.toJson).

Implementation

String jsonToOpenUI(
  Object json,
  OpenUiLibrary library, {
  Map<String, Object?>? stateDeclarations,
}) {
  final root = json is ElementNode
      ? json
      : ElementNode.fromJson(json is Map ? json : const {});
  final collector = _Collector(
    library.compiled.components,
    library.compiled.calls.keys.toSet(),
  );
  final rootExpr = collector.elementExpr(root);
  final rootId = (root.statementId ?? '').isEmpty ? 'root' : root.statementId!;
  final lines = ['$rootId = $rootExpr'];
  for (final entry in collector.statements.entries) {
    if (entry.key != rootId) lines.add(entry.value);
  }
  if (stateDeclarations != null) {
    for (final entry in stateDeclarations.entries) {
      if (entry.value != null) {
        lines.add('${entry.key} = ${collector.value(entry.value)}');
      }
    }
  }
  return lines.join('\n');
}