deserialize static method

Context deserialize(
  1. Map<String, dynamic> values
)

Deserializes a previously serialized context map back into a Context object.

This is used when receiving context from another isolate.

Implementation

static Context deserialize(Map<String, dynamic> values) {
  _getAndCacheOTelFactory();
  var context = _otelFactory!.context();

  // Handle baggage if present and not empty
  if (values.containsKey('baggage')) {
    final baggageValue = values['baggage'];
    if (baggageValue is Map<String, dynamic> && baggageValue.isNotEmpty) {
      final baggage = Baggage.fromJson(baggageValue);
      context = context.copyWith(_baggageKey, baggage);
    }
  }

  // Handle span context if present and not null. Round-trip-faithful:
  // isRemote is preserved as serialized. The remote-on-receiving-side
  // override for cross-isolate transfer happens in [runIsolate].
  if (values.containsKey('spanContext')) {
    final spanContextValue = values['spanContext'];
    if (spanContextValue is Map<String, dynamic>) {
      final spanContext = SpanContext.fromJson(spanContextValue);
      context = context.copyWith(_spanContextKey, spanContext);
    }
  }

  // Handle remaining values
  values.forEach((key, value) {
    if (key != 'baggage' && key != 'spanContext') {
      if (value is Map<String, dynamic> && value.containsKey('uniqueId')) {
        final uniqueIdList =
            Uint8List.fromList((value['uniqueId'] as List).cast<int>());

        // Get the original key name if available, otherwise use the key
        final keyName = value.containsKey('originalKeyName')
            ? value['originalKeyName'] as String
            : key;

        // Note: Two keys with same name but different uniqueIds are valid and distinct

        // Recreate the context key with the same name and uniqueId
        final contextKey = OTelFactory.otelFactory!.contextKey<Object?>(
          keyName,
          uniqueIdList,
        );
        context = context.copyWith(contextKey, value['value']);
      }
    }
  });

  return context;
}