inferLegacyFieldFeatures function

Map<String, String> inferLegacyFieldFeatures(
  1. String label,
  2. String type,
  3. bool proto3Optional,
  4. String? packed,
  5. int edition,
)

Infers the field-level feature overrides implied by a proto2/proto3 field's shape, so a legacy field resolves identically to its editions equivalent.

  • label is the FieldDescriptorProto label: "LABEL_OPTIONAL", "LABEL_REQUIRED", or "LABEL_REPEATED".
  • type is the field type; "TYPE_GROUP" implies delimited encoding.
  • proto3Optional is true for a proto3 optional field.
  • packed is the explicit [packed=...] option: "true", "false", or null if unset.
  • edition is the field's file edition sentinel (editionProto2 or editionProto3).

Returns only the keys this shape pins; everything else inherits the edition base. Mirrors descriptor.cc's InferLegacyProtoFeatures.

Implementation

Map<String, String> inferLegacyFieldFeatures(
  String label,
  String type,
  bool proto3Optional,
  String? packed,
  int edition,
) {
  final inferred = <String, String>{};

  if (label == 'LABEL_REQUIRED') {
    inferred[featureFieldPresence] = fieldPresenceLegacyRequired;
  } else if (edition == editionProto3 &&
      label == 'LABEL_OPTIONAL' &&
      !proto3Optional) {
    // proto3 singular scalar: implicit presence (unless explicitly `optional`).
    inferred[featureFieldPresence] = fieldPresenceImplicit;
  } else if (proto3Optional) {
    inferred[featureFieldPresence] = fieldPresenceExplicit;
  }

  if (type == 'TYPE_GROUP') {
    inferred[featureMessageEncoding] = messageEncodingDelimited;
  }

  if (packed == 'true') {
    inferred[featureRepeatedFieldEncoding] = repeatedFieldEncodingPacked;
  } else if (packed == 'false') {
    inferred[featureRepeatedFieldEncoding] = repeatedFieldEncodingExpanded;
  }

  return inferred;
}