Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix proto3 repeated field encoding without packed option #635

Merged
merged 5 commits into from
May 17, 2022

Conversation

osa1
Copy link
Member

@osa1 osa1 commented Apr 27, 2022

proto3 repeated fields are packed by default, see
https://developers.google.com/protocol-buffers/docs/encoding#packed.

Fixes #345

@osa1 osa1 requested a review from sigurdm April 27, 2022 14:26
/// proto2 requires `[packed=true]` option.
bool get isPacked {
if (!isRepeated) {
return false; // only repeated fields can be packed
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this instead throw an argument-error (is it not an error to ask if a non-repeated field is packed?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call site is this:

String get typeConstant {
var prefix = 'O';
if (isRequired) {
prefix = 'Q';
} else if (isPacked) {
prefix = 'K';
} else if (isRepeated) {
prefix = 'P';
}
return '$protobufImportPrefix.PbFieldType.' +
prefix +
baseType.typeConstantSuffix;
}

We don't check whether the field is repeated before checking whether it's packed. We could update the checks in the call site and expect repeated field here. I don't have an opinion on whether that would be an improvement or not because there's only one use site. We could even inline this getter.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the call-site it makes sense to just return false here I think.

I would prefer not inlining this amount of logic.

Copy link
Collaborator

@sigurdm sigurdm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@osa1 osa1 requested a review from sigurdm May 11, 2022 06:58
@osa1
Copy link
Member Author

osa1 commented May 11, 2022

@sigurdm PTAL. ProtobufField is refactored to use the new parent field.

Copy link
Collaborator

@sigurdm sigurdm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@osa1 osa1 merged commit eed09c4 into google:master May 17, 2022
@osa1 osa1 deleted the issue_345 branch May 17, 2022 13:00
osa1 added a commit to osa1/protobuf.dart that referenced this pull request May 17, 2022
@osa1
Copy link
Member Author

osa1 commented May 19, 2022

Ported to internal in cl/449702819.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proto3 repeated field not packed by default
2 participants