Google is committed to advancing racial equity for Black communities. See how.

encode method

  • @override

void encode (Encoder encoder, T value, int offset, int depth)

override

Implementation

@override
void encode(Encoder encoder, T value, int offset, int depth) {
  final unknownDataMap = value.$unknownData;

  // Determining max index
  int maxIndex = -1;
  for (int i = 0; i < members.length; i++) {
    final field = value.$field(i);
    if (field != null) {
      maxIndex = i;
    }
  }
  if (unknownDataMap != null) {
    // Update the max index based on the table's unknown fields. The unknown
    // data map is keyed by ordinal, not index, so subtract by one.
    maxIndex = max(maxIndex, unknownDataMap.keys.fold(0, max) - 1);
  }
  int maxOrdinal = maxIndex + 1;

  // Header.
  encoder
    ..encodeUint64(maxOrdinal, offset)
    ..encodeUint64(kAllocPresent, offset + 8);

  // Sizing
  int envelopeOffset =
      encoder.alloc(maxOrdinal * envelopeSize(encoder.wireFormat), depth);

  // Envelopes, and fields.
  for (int i = 0; i <= maxIndex; i++) {
    var field = value.$field(i);
    FidlType? fieldType;
    if (i < members.length) {
      fieldType = members[i];
    }
    if (fieldType == null && unknownDataMap != null) {
      // .$field is accessed by index, whereas the unknown data map is
      // accessed by ordinal
      final unknownData = unknownDataMap[i + 1];
      if (unknownData != null) {
        _maybeThrowOnUnknownHandles(resource, unknownData);
        field = unknownData;
        fieldType = UnknownRawDataType(
            numBytes: unknownData.data.length,
            numHandles: unknownData.handles.length);
      }
    }

    if (field != null && fieldType != null) {
      _encodeEnvelopePresent(
          encoder, envelopeOffset, depth + 1, field, fieldType);
    } else {
      _encodeEnvelopeAbsent(encoder, envelopeOffset);
    }
    envelopeOffset += envelopeSize(encoder.wireFormat);
  }
}