Skip to content

Commit

Permalink
Default to packed repeated primitive fields in proto3.
Browse files Browse the repository at this point in the history
Fixes #704.

In proto3, repeated primitive fields should be packed by default:
https://protobuf.dev/programming-guides/encoding/#packed
  • Loading branch information
rcorre authored and stepancheg committed Jun 16, 2024
1 parent aadcdb2 commit ca999c9
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion protobuf-codegen/src/gen/field/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,36 @@ impl<'a> FieldGen<'a> {

FieldKind::Repeated(RepeatedField {
elem,
packed: field.field.proto().options.get_or_default().packed(),
packed: field
.field
.proto()
.options
.get_or_default()
.packed
.unwrap_or(match field.message.scope.file_scope.syntax() {
Syntax::Proto2 => false,
// in proto3, repeated primitive types are packed by default
Syntax::Proto3 => match field.field.proto().type_() {
Type::TYPE_DOUBLE
| Type::TYPE_FLOAT
| Type::TYPE_INT64
| Type::TYPE_UINT64
| Type::TYPE_INT32
| Type::TYPE_FIXED64
| Type::TYPE_FIXED32
| Type::TYPE_BOOL
| Type::TYPE_UINT32
| Type::TYPE_SFIXED32
| Type::TYPE_SFIXED64
| Type::TYPE_SINT32
| Type::TYPE_SINT64 => true,
Type::TYPE_STRING
| Type::TYPE_GROUP
| Type::TYPE_MESSAGE
| Type::TYPE_BYTES
| Type::TYPE_ENUM => false,
},
}),
})
}
RuntimeFieldType::Singular(..) => {
Expand Down

0 comments on commit ca999c9

Please sign in to comment.