Skip to content

Commit

Permalink
feat: add handling for extension range options (#1990)
Browse files Browse the repository at this point in the history
* Add handling for extension range options

* Fix CI failures

* Fix more test issues
  • Loading branch information
mkruskal-google committed May 10, 2024
1 parent 2f846fe commit 2d58011
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/parse.js
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
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
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

0 comments on commit 2d58011

Please sign in to comment.