decodeOps function

DecodedOps decodeOps(
  1. Object? input
)

Reads a batch of ops: the ui_apply tool input ({"ops": [...], "baseRev": 42, "atomic": false}), a bare list of ops, or one op. Total: what is not an op becomes a diagnostic with its index.

Implementation

DecodedOps decodeOps(Object? input) {
  final diagnostics = <EditDiagnostic>[];
  Object? list = input;
  int? baseRev;
  var atomic = false;
  final m = readMap(input);
  if (m != null && !m.containsKey('op')) {
    list = m['ops'];
    baseRev = readInt(m['baseRev']);
    atomic = readBool(m['atomic']) ?? false;
    if (list == null) {
      diagnostics.add(EditDiagnostic.of(EditCodes.missingField, -1, 'The tool input is {"ops": [...]}: a list of ops, got no "ops".', path: '/ops'));
      return DecodedOps(const [], diagnostics: diagnostics, baseRev: baseRev, atomic: atomic);
    }
  } else if (m != null) {
    list = [m];
  }
  final items = readList(list);
  if (items == null) {
    diagnostics.add(EditDiagnostic.of(EditCodes.wrongType, -1, '"ops" is a list of op objects, got ${_describe(list)}.', path: '/ops'));
    return DecodedOps(const [], diagnostics: diagnostics, baseRev: baseRev, atomic: atomic);
  }
  final ops = <(int, EditOp)>[];
  for (var i = 0; i < items.length; i++) {
    for (final op in _decodeOne(items[i], i, diagnostics)) {
      ops.add((i, op));
    }
  }
  return DecodedOps(ops, diagnostics: diagnostics, baseRev: baseRev, atomic: atomic);
}