celFieldsRead function

CelFieldsRead celFieldsRead(
  1. CelProgram program,
  2. String variable
)

The top-level fields program reads under variable (record or newRecord; any other root name, such as params, reads its members the same way). size(newRecord.title) <= 80 reads {title} under newRecord and nothing under record (size is a function in FFX-CEL, never a method: newRecord.title.size() does not parse). A changedFields() call reads the whole of both record and newRecord.

Implementation

CelFieldsRead celFieldsRead(CelProgram program, String variable) {
  final fields = <String>{};
  var whole = false;
  void path(List<String> path) {
    if (path.first != variable) return;
    if (path.length < 2) {
      whole = true;
    } else {
      fields.add(path[1]);
    }
  }

  void visit(_Node node) {
    switch (node) {
      case _Variable(path: final p):
        path(p);
      case _Has(:final selector):
        path(selector.path);
      case _ChangedFields():
        if (variable == 'record' || variable == 'newRecord') whole = true;
      case _Lookup(:final id):
        visit(id);
      case _Logical(:final left, :final right) ||
          _Compare(:final left, :final right) ||
          _Add(:final left, :final right):
        visit(left);
        visit(right);
      case _Method(:final receiver, :final argument):
        visit(receiver);
        visit(argument);
      case _Not(:final operand) ||
          _Negate(:final operand) ||
          _Size(:final operand):
        visit(operand);
      case _Literal() ||
          _ListLiteral() ||
          _Owns() ||
          _Spend() ||
          _TimestampLiteral() ||
          _DurationLiteral():
        break;
    }
  }

  visit(program._root);
  return CelFieldsRead._(whole ? const {} : Set.unmodifiable(fields), whole);
}