describeStorageValue static method

Map<String, Object?> describeStorageValue(
  1. String key,
  2. String? raw
)

Describes a raw NyStorage value, decoding its {_v, _t, _d, _e} envelope.

Implementation

static Map<String, Object?> describeStorageValue(String key, String? raw) {
  if (raw == null) {
    return {'key': key, 'exists': false, 'type': null, 'value': null};
  }
  try {
    final dynamic decoded = jsonDecode(raw);
    if (decoded is Map &&
        decoded.containsKey('_v') &&
        decoded.containsKey('_t') &&
        decoded.containsKey('_d')) {
      final String type = decoded['_t'].toString().toLowerCase();
      final dynamic data = decoded['_d'];
      final dynamic expiresAt = decoded['_e'];
      return {
        'key': key,
        'exists': true,
        'type': type,
        'value': _decodeEnvelopeValue(type, data),
        if (expiresAt is int)
          'expiresAt': DateTime.fromMillisecondsSinceEpoch(
            expiresAt,
          ).toIso8601String(),
      };
    }
  } catch (_) {
    // Not an envelope; fall through to the raw value.
  }
  return {'key': key, 'exists': true, 'type': 'raw', 'value': raw};
}