decodeSemanticAction function

({SemanticAction action, SemanticNodeId id, String? targetToken, Object? value}) decodeSemanticAction(
  1. Uint8List bytes
)

Decodes a semantic-action request. Throws RemoteCodecException on an unrecognized action name or a malformed payload so the caller rejects it rather than misinterpreting it.

Implementation

({SemanticNodeId id, SemanticAction action, Object? value, String? targetToken})
decodeSemanticAction(Uint8List bytes) {
  final r = _Reader(bytes);
  final id = r.vstr(
    maxBytes: maxRemoteSemanticNodeIdBytes,
    field: 'semantic node id',
    allowMalformed: false,
  );
  final actionName = r.vstr();
  Object? value;
  if (r.boolean()) {
    final raw = r.vstr();
    try {
      value = jsonDecode(raw);
    } on FormatException {
      throw const RemoteCodecException('invalid setValue payload JSON');
    }
  }
  String? targetToken;
  // Absent for stable-id actions; a positional id without a token fails
  // closed at dispatch.
  if (r.boolean()) {
    targetToken = r.vstr(
      maxBytes: 64,
      field: 'semantic action target token',
      allowMalformed: false,
    );
  }
  r.expectEnd();
  for (final action in SemanticAction.values) {
    if (action.name == actionName) {
      return (
        id: SemanticNodeId(id),
        action: action,
        value: value,
        targetToken: targetToken,
      );
    }
  }
  throw RemoteCodecException('unknown semantic action "$actionName"');
}