UnknownType.fromJson constructor

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

Decodes an unknown type from its protocol JSON form.

Implementation

factory UnknownType.fromJson(Map<String, Object?> json) {
  final kind = UnknownKind.values.firstWhere(
    (kind) => kind.name == json['unknownKind'],
    orElse: () => throw FormatException(
      'Unknown Polars unknownKind: ${json['unknownKind']}',
    ),
  );
  final rawValue = json['value'];
  if (kind == UnknownKind.integer) {
    if (rawValue is! String) {
      throw const FormatException('Integer unknown requires a decimal value');
    }
    final value = BigInt.tryParse(rawValue);
    if (value == null || value.toString() != rawValue) {
      throw const FormatException(
        'Unknown integer value must be canonical decimal',
      );
    }
    try {
      return UnknownType.integer(value);
    } on ArgumentError catch (error) {
      throw FormatException('Unknown integer is outside i128: $error');
    }
  }
  if (rawValue != null || json.containsKey('value')) {
    throw FormatException('${kind.name} unknown must not have a value');
  }
  return switch (kind) {
    UnknownKind.any => const UnknownType.any(),
    UnknownKind.float => const UnknownType.float(),
    UnknownKind.string => const UnknownType.string(),
    UnknownKind.integer => throw StateError('handled above'),
  };
}