CockpitLocator.fromJson constructor

CockpitLocator.fromJson(
  1. Map<String, Object?> json
)

Decodes a CockpitLocator from a JSON object.

Implementation

factory CockpitLocator.fromJson(Map<String, Object?> json) {
  if (json.containsKey('kind') || json.containsKey('value')) {
    throw const FormatException(
      'CockpitLocator JSON no longer supports legacy kind/value fields.',
    );
  }
  final fallbacks = (json['fallbacks'] as List<Object?>? ?? const <Object?>[])
      .cast<Map<Object?, Object?>>()
      .map((item) => CockpitLocator.fromJson(Map<String, Object?>.from(item)))
      .toList(growable: false);
  final ancestorJson = json['ancestor'] as Map<Object?, Object?>?;
  final matchMode = json['matchMode'] == null
      ? CockpitTextMatchMode.exact
      : CockpitTextMatchMode.fromJson(json['matchMode']);
  final text = json['text'] as String?;
  final tooltip = json['tooltip'] as String?;
  if (matchMode != CockpitTextMatchMode.exact &&
      text == null &&
      tooltip == null) {
    throw const FormatException(
      'Locator matchMode requires a text or tooltip signal.',
    );
  }
  if (matchMode == CockpitTextMatchMode.regex) {
    for (final pattern in <String?>[text, tooltip]) {
      if (pattern != null) RegExp(pattern);
    }
  }

  return CockpitLocator(
    cockpitId: json['cockpitId'] as String?,
    semanticId: json['semanticId'] as String?,
    key: json['key'] as String?,
    text: text,
    tooltip: tooltip,
    type: json['type'] as String?,
    route: json['route'] as String?,
    registrationId: json['registrationId'] as String?,
    path: json['path'] as String?,
    matchMode: matchMode,
    index: (json['index'] as num?)?.toInt(),
    ancestor: ancestorJson == null
        ? null
        : CockpitLocator.fromJson(Map<String, Object?>.from(ancestorJson)),
    fallbacks: fallbacks,
  );
}