HeapType.fromJson constructor

HeapType.fromJson(
  1. Object? json_
)

Returns a new instance from a JSON value. May throw if the value does not have the expected structure.

Implementation

factory HeapType.fromJson(Object? json_) {
  Object? json = json_;
  if (json is Map) {
    final MapEntry(:key, :value) =
        json.entries.firstWhere((e) => e.key != 'runtimeType');
    json = (
      key is int ? key : _spec.cases.indexWhere((c) => c.label == key),
      value,
    );
  }
  return switch (json) {
    (0, final value) || [0, final value] => HeapTypeIndexed(value! as int),
    (1, null) || [1, null] => const HeapTypeFunc(),
    (2, null) || [2, null] => const HeapTypeExtern(),
    (3, null) || [3, null] => const HeapTypeAny(),
    (4, null) || [4, null] => const HeapTypeNone(),
    (5, null) || [5, null] => const HeapTypeNoExtern(),
    (6, null) || [6, null] => const HeapTypeNoFunc(),
    (7, null) || [7, null] => const HeapTypeEq(),
    (8, null) || [8, null] => const HeapTypeStruct(),
    (9, null) || [9, null] => const HeapTypeArray(),
    (10, null) || [10, null] => const HeapTypeI31(),
    _ => throw Exception('Invalid JSON $json_'),
  };
}