44 lines
1.2 KiB
Dart
44 lines
1.2 KiB
Dart
import '../protocol/runtime_protocol.dart';
|
|
|
|
class RuntimeCommand {
|
|
const RuntimeCommand({
|
|
required this.type,
|
|
this.target,
|
|
this.payload = const {},
|
|
});
|
|
|
|
final String type;
|
|
final String? target;
|
|
final Map<String, Object?> payload;
|
|
|
|
static RuntimeCommand fromMap(Map<String, Object?> map) {
|
|
final type = map[RuntimeProtocolField.type];
|
|
if (type is! String || type.isEmpty) {
|
|
throw const FormatException('RuntimeCommand.type must be a string');
|
|
}
|
|
|
|
if (!RuntimeCommandType.isSupported(type)) {
|
|
throw FormatException('RuntimeCommand.type is unsupported: $type');
|
|
}
|
|
RuntimeProtocolSchema.ensureKnownKeys(
|
|
map,
|
|
allowed: RuntimeProtocolSchema.allowedCommandFields(type),
|
|
context: 'RuntimeCommand.$type',
|
|
);
|
|
|
|
final targetValue = map[RuntimeProtocolField.target];
|
|
if (targetValue != null && targetValue is! String) {
|
|
throw const FormatException('RuntimeCommand.target must be a string');
|
|
}
|
|
|
|
final payload = Map<String, Object?>.from(map)
|
|
..remove(RuntimeProtocolField.type)
|
|
..remove(RuntimeProtocolField.target);
|
|
return RuntimeCommand(
|
|
type: type,
|
|
target: targetValue as String?,
|
|
payload: payload,
|
|
);
|
|
}
|
|
}
|