inferLegacyFieldFeatures function
Infers the field-level feature overrides implied by a proto2/proto3 field's shape, so a legacy field resolves identically to its editions equivalent.
labelis the FieldDescriptorProto label:"LABEL_OPTIONAL","LABEL_REQUIRED", or"LABEL_REPEATED".typeis the field type;"TYPE_GROUP"implies delimited encoding.proto3Optionalis true for a proto3optionalfield.packedis the explicit[packed=...]option:"true","false", ornullif unset.editionis 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;
}