fromJson static method

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

Implementation

static ComponentPropertyDef fromJson(Map<String, Object?> json) {
  final kindName = json['kind'];
  final kind = ComponentPropertyKind.values.asNameMap()[kindName];
  if (json['name'] is! String || kind == null) {
    throw FormatException('Malformed property descriptor: $json');
  }
  return ComponentPropertyDef(
    json['name'] as String,
    kind,
    defaultValue: json['default'] == null
        ? null
        : decodePropertyValue(json['default']),
    doc: json['doc'] as String?,
    group: json['group'] as String?,
    resourceKind: json['resourceKind'] as String?,
    // Materialize eagerly; a lazy cast view defers its element TypeError
    // to whoever iterates it (the inspector, at render time).
    options: json['options'] is List
        ? [for (final option in json['options'] as List) option as String]
        : null,
    constraints: [
      if (json['constraints'] is List)
        for (final entry in json['constraints'] as List)
          if (entry is Map)
            PropertyConstraint.fromJson(entry.cast<String, Object?>()),
    ],
    formerNames: json['formerNames'] is List
        ? [for (final name in json['formerNames'] as List) name as String]
        : const [],
    transient: json['transient'] == true,
    itemDef: json['item'] is Map
        ? fromJson((json['item'] as Map).cast<String, Object?>())
        : null,
    objectFields: json['fields'] is List
        ? [
            for (final entry in json['fields'] as List)
              if (entry is Map) fromJson(entry.cast<String, Object?>()),
          ]
        : null,
    unionTag: json['unionTag'] as String? ?? 'kind',
    unionVariants: json['variants'] is Map
        ? {
            for (final entry in (json['variants'] as Map).entries)
              entry.key as String: [
                if (entry.value is List)
                  for (final field in entry.value as List)
                    if (field is Map) fromJson(field.cast<String, Object?>()),
              ],
          }
        : null,
  );
}