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

Implement message set wire format #836

Merged
merged 30 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3732cc6
WIP message set stuff
osa1 Jun 6, 2023
f66c60c
Parsing
osa1 Jun 6, 2023
3c27f6b
Implement serialization
osa1 Jun 6, 2023
cf729a6
Use helpers
osa1 Jun 6, 2023
da75ae7
Minor refactoring
osa1 Jun 6, 2023
a67c4d2
Fix fields
osa1 Jun 6, 2023
a8851a1
Simplify extension message creation
osa1 Jun 7, 2023
fdf0659
Revert BuilderInfo changes
osa1 Jun 7, 2023
944371f
Comments
osa1 Jun 7, 2023
3b6ef30
Hide consts
osa1 Jun 7, 2023
030eef3
Formatting
osa1 Jun 7, 2023
772d26e
Merge remote-tracking branch 'origin/master' into osa1/message_set
osa1 Jun 10, 2023
2a29cff
Fix unknown field generation when the extension is unknown
osa1 Jun 10, 2023
8593b19
Use constants
osa1 Jun 10, 2023
2a988d1
Fix generation and parsing unknown fields
osa1 Jun 10, 2023
5511b92
Add one more test
osa1 Jun 10, 2023
ce223d9
Fix message set message name when searching for extensions
osa1 Jun 13, 2023
bc59c63
Merge branch 'master' into osa1/message_set
osa1 Jun 13, 2023
0405a01
Revert whitespace changes
osa1 Jun 13, 2023
f308706
Update changelog
osa1 Jun 13, 2023
1b22b28
One more test
osa1 Jun 13, 2023
60fe96f
Refactor unknown field handling
osa1 Jun 13, 2023
9646a5c
Revert unrelated changes
osa1 Jun 13, 2023
3adfb78
Handle empty message sets in reparse
osa1 Jun 13, 2023
d44f033
Only serialize known extensions for consistency normal extension repa…
osa1 Jun 13, 2023
5170138
Merge branch 'master' into osa1/message_set
osa1 Jun 13, 2023
11f1efa
Remove parsed items from the list
osa1 Jun 14, 2023
751cd80
Merge branch 'master' into osa1/message_set
osa1 Jun 16, 2023
6c6fe1e
Merge branch 'master' into osa1/message_set
osa1 Jun 26, 2023
0be7719
Address comments
osa1 Jun 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions protobuf/lib/src/protobuf/message_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,21 @@ const messageTag = 3;
abstract class MessageSet extends GeneratedMessage {
@override
void writeToCodedBufferWriter(CodedBufferWriter output) {
throw 'TODO';
final extensions = _fieldSet._ensureExtensions();

for (final ext in extensions._values.entries) {
final typeId = ext.key;
final message = ext.value as GeneratedMessage;

output._writeTag(1, WIRETYPE_START_GROUP);
output._writeTag(2, WIRETYPE_VARINT);
output._writeVarint32(typeId);
output._writeTag(3, WIRETYPE_LENGTH_DELIMITED);
final mark = output._startLengthDelimited();
message.writeToCodedBufferWriter(output);
output._endLengthDelimited(mark);
output._writeTag(1, WIRETYPE_END_GROUP);
}
}

@override
Expand All @@ -36,15 +50,13 @@ abstract class MessageSet extends GeneratedMessage {
// Parse items
while (true) {
final tag = input.readTag();
final wireType = tag & 0x7;
final tagNumber = tag >> 3;

if (tag == 0) {
break;
}

if (tagNumber == typeIdTag) {
assert(wireType == WIRETYPE_VARINT);
typeId = input.readInt32();
if (message != null) {
_parseExtension(typeId, message, extensionRegistry);
Expand All @@ -53,7 +65,6 @@ abstract class MessageSet extends GeneratedMessage {
continue outer;
}
} else if (tagNumber == messageTag) {
assert(wireType == WIRETYPE_LENGTH_DELIMITED);
message = input.readBytes();
if (typeId != null) {
_parseExtension(typeId, message, extensionRegistry);
Expand All @@ -74,12 +85,17 @@ abstract class MessageSet extends GeneratedMessage {
mergeFromCodedBufferReader(CodedBufferReader(input), extensionRegistry);
}

void _parseExtension(int typeId, List<int> message, ExtensionRegistry extensionRegistry) {
void _parseExtension(
int typeId, List<int> message, ExtensionRegistry extensionRegistry) {
final ext = extensionRegistry.getExtension('MessageSet', typeId);
if (ext == null) {
_fieldSet._ensureUnknownFields().addField(typeId, UnknownFieldSetField()..addLengthDelimited(message));
_fieldSet._ensureUnknownFields().addField(
typeId, UnknownFieldSetField()..addLengthDelimited(message));
} else {
setExtension(ext, (ext.makeDefault!() as GeneratedMessage).deepCopy()..mergeFromBuffer(message));
setExtension(
ext,
(ext.makeDefault!() as GeneratedMessage).deepCopy()
osa1 marked this conversation as resolved.
Show resolved Hide resolved
..mergeFromBuffer(message));
}
}
}
1 change: 1 addition & 0 deletions protoc_plugin/test/message_set_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ void main() {
.getExtension(TestMessage.messageSetExtension) as ExtensionMessage;
expect(extensionValue.a, 123);
expect(extensionValue.b, 'hi');
expect(msg.writeToBuffer(), encoded);
});
}