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

feat: add handling for extension range options #1990

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 18 additions & 1 deletion src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,24 @@ function parse(source, root, options) {
else
target.push([ start = parseId(next()), skip("to", true) ? parseId(next()) : start ]);
} while (skip(",", true));
skip(";");
var dummy = {options: undefined};
dummy.setOption = function(name, value) {
if (this.options === undefined) this.options = {};
this.options[name] = value;
};
ifBlock(
dummy,
function parseRange_block(token) {
/* istanbul ignore else */
if (token === "option") {
parseOption(dummy, token); // skip
skip(";");
} else
throw illegal(token);
},
function parseRange_line() {
parseInlineOptions(dummy); // skip
});
}

function parseNumber(token, insideTryCatch) {
Expand Down
61 changes: 60 additions & 1 deletion tests/data/google/protobuf/descriptor.proto
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ message DescriptorProto {
message ExtensionRange {
optional int32 start = 1;
optional int32 end = 2;

optional ExtensionRangeOptions options = 3;
}
repeated ExtensionRange extension_range = 5;

Expand All @@ -121,6 +123,63 @@ message DescriptorProto {
repeated string reserved_name = 10;
}


message ExtensionRangeOptions {
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;

message Declaration {
// The extension number declared within the extension range.
optional int32 number = 1;

// The fully-qualified name of the extension field. There must be a leading
// dot in front of the full name.
optional string full_name = 2;

// The fully-qualified type name of the extension field. Unlike
// Metadata.type, Declaration.type must have a leading dot for messages
// and enums.
optional string type = 3;

// If true, indicates that the number is reserved in the extension range,
// and any extension field with the number will fail to compile. Set this
// when a declared extension field is deleted.
optional bool reserved = 5;

// If true, indicates that the extension must be defined as repeated.
// Otherwise the extension must be defined as optional.
optional bool repeated = 6;

reserved 4; // removed is_repeated
}

// For external users: DO NOT USE. We are in the process of open sourcing
// extension declaration and executing internal cleanups before it can be
// used externally.
repeated Declaration declaration = 2 [retention = RETENTION_SOURCE];

// The verification state of the extension range.
enum VerificationState {
// All the extensions of the range must be declared.
DECLARATION = 0;
UNVERIFIED = 1;
}

// The verification state of the range.
// TODO(b/278783756): flip the default to DECLARATION once all empty ranges
// are marked as UNVERIFIED.
optional VerificationState verification = 3
[default = UNVERIFIED, retention = RETENTION_SOURCE];

// Clients can define custom options in extensions of this message. See above.
// BEGIN GOOGLE-INTERNAL
// Extension numbers > 530,000,000 must be forward-declared in
// google3/third_party/protobuf/extdecl_extension_range_options.h. See
// go/extension-declaration-reserved-numbers for more information.
// END GOOGLE-INTERNAL
extensions 1000 to max;
}

// Describes a field within a message.
message FieldDescriptorProto {
enum Type {
Expand Down Expand Up @@ -813,4 +872,4 @@ message GeneratedCodeInfo {
// the last relevant byte (so the length of the text = end - begin).
optional int32 end = 4;
}
}
}
7 changes: 7 additions & 0 deletions tests/data/uncommon.proto
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ message Test2 {
extend Test;
extend Test{};
extend Test{required int32 a=1;} // not validated by the parser
extend Test{Test inner_ext=1000;} // not validated by the parser

extensions 1000 to 1999 [declaration = {
number: 1000
full_name: 'uncommon.Test.inner_ext'
type: 'uncommon.Test'
}];
};

enum Test3;
Expand Down
Loading