-
Notifications
You must be signed in to change notification settings - Fork 195
Follow Protobuf Enum style through a new cli option "protobuf-enum-style" #1045
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
base: master
Are you sure you want to change the base?
Changes from all commits
f473e49
8bdd3e1
0f41caf
4f8f6ca
f9fb48e
f97ba90
7e6d158
2b78a1f
bb6c8ea
0d9c258
ed1c210
a9dd9f0
ecb159d
e34c90e
e1d8792
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,11 +54,13 @@ class GenerationOptions { | |
final bool useGrpc; | ||
final bool generateMetadata; | ||
final bool disableConstructorArgs; | ||
final bool protobufEnumStyle; | ||
|
||
GenerationOptions({ | ||
this.useGrpc = false, | ||
this.generateMetadata = false, | ||
this.disableConstructorArgs = false, | ||
this.protobufEnumStyle = false, | ||
}); | ||
} | ||
|
||
|
@@ -112,6 +114,19 @@ class DisableConstructorArgsParser implements SingleOptionParser { | |
} | ||
} | ||
|
||
class ProtobufEnumStyleParser implements SingleOptionParser { | ||
bool protobufEnumStyleEnabled = false; | ||
|
||
@override | ||
void parse(String name, String? value, OnError onError) { | ||
if (value != null) { | ||
onError('Invalid protobuf-enum-style option. No value expected.'); | ||
return; | ||
} | ||
protobufEnumStyleEnabled = true; | ||
} | ||
} | ||
|
||
Comment on lines
+117
to
+129
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New argument to enable protobuf enum style parsing. By default, the value is false to not incorporate breaking change! |
||
/// Parser used by the compiler, which supports the `rpc` option (see | ||
/// [GrpcOptionParser]) and any additional option added in [parsers]. If | ||
/// [parsers] has a key for `rpc`, it will be ignored. | ||
|
@@ -132,11 +147,15 @@ GenerationOptions? parseGenerationOptions( | |
final disableConstructorArgsParser = DisableConstructorArgsParser(); | ||
newParsers['disable_constructor_args'] = disableConstructorArgsParser; | ||
|
||
final protobufEnumStyleParser = ProtobufEnumStyleParser(); | ||
newParsers['protobuf-enum-style'] = protobufEnumStyleParser; | ||
|
||
if (genericOptionsParser(request, response, newParsers)) { | ||
return GenerationOptions( | ||
useGrpc: grpcOptionParser.grpcEnabled, | ||
generateMetadata: generateMetadataParser.generateKytheInfo, | ||
disableConstructorArgs: disableConstructorArgsParser.value, | ||
protobufEnumStyle: protobufEnumStyleParser.protobufEnumStyleEnabled, | ||
); | ||
} | ||
return null; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding a new golden test to validate protobuf enum style |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class HTTPStatusCode extends $pb.ProtobufEnum { | ||
static const HTTPStatusCode unspecified = HTTPStatusCode._(0, _omitEnumNames ? '' : 'HTTP_STATUS_CODE_UNSPECIFIED'); | ||
static const HTTPStatusCode notFound = HTTPStatusCode._(1, _omitEnumNames ? '' : 'HTTP_STATUS_CODE_NOT_FOUND'); | ||
static const HTTPStatusCode internalServerError = HTTPStatusCode._(2, _omitEnumNames ? '' : 'HTTP_STATUS_CODE_INTERNAL_SERVER_ERROR'); | ||
static const HTTPStatusCode ok = HTTPStatusCode._(3, _omitEnumNames ? '' : 'HTTP_STATUS_CODE_OK'); | ||
|
||
static const $core.List<HTTPStatusCode> values = <HTTPStatusCode> [ | ||
unspecified, | ||
notFound, | ||
internalServerError, | ||
ok, | ||
]; | ||
|
||
static final $core.List<HTTPStatusCode?> _byValue = $pb.ProtobufEnum.$_initByValueList(values, 3); | ||
static HTTPStatusCode? valueOf($core.int value) => value < 0 || value >= _byValue.length ? null : _byValue[value]; | ||
|
||
const HTTPStatusCode._(super.value, super.name); | ||
} | ||
|
||
|
||
const $core.bool _omitEnumNames = $core.bool.fromEnvironment('protobuf.omit_enum_names'); |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding a new golden test to validate protobuf enum style |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
annotation: { | ||
path: 5 | ||
path: 0 | ||
sourceFile: sample.proto | ||
begin: 6 | ||
end: 20 | ||
} | ||
annotation: { | ||
path: 5 | ||
path: 0 | ||
path: 2 | ||
path: 0 | ||
sourceFile: sample.proto | ||
begin: 78 | ||
end: 89 | ||
} | ||
annotation: { | ||
path: 5 | ||
path: 0 | ||
path: 2 | ||
path: 1 | ||
sourceFile: sample.proto | ||
begin: 197 | ||
end: 205 | ||
} | ||
annotation: { | ||
path: 5 | ||
path: 0 | ||
path: 2 | ||
path: 2 | ||
sourceFile: sample.proto | ||
begin: 311 | ||
end: 330 | ||
} | ||
annotation: { | ||
path: 5 | ||
path: 0 | ||
path: 2 | ||
path: 3 | ||
sourceFile: sample.proto | ||
begin: 448 | ||
end: 450 | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding a new golden test to validate protobuf enum style |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class PhoneType extends $pb.ProtobufEnum { | ||
static const PhoneType unspecified = PhoneType._(0, _omitEnumNames ? '' : 'PHONE_TYPE_UNSPECIFIED'); | ||
static const PhoneType mobile = PhoneType._(1, _omitEnumNames ? '' : 'PHONE_TYPE_MOBILE'); | ||
static const PhoneType home = PhoneType._(2, _omitEnumNames ? '' : 'PHONE_TYPE_HOME'); | ||
static const PhoneType work = PhoneType._(3, _omitEnumNames ? '' : 'PHONE_TYPE_WORK'); | ||
|
||
static const $core.List<PhoneType> values = <PhoneType> [ | ||
unspecified, | ||
mobile, | ||
home, | ||
work, | ||
]; | ||
|
||
static final $core.List<PhoneType?> _byValue = $pb.ProtobufEnum.$_initByValueList(values, 3); | ||
static PhoneType? valueOf($core.int value) => value < 0 || value >= _byValue.length ? null : _byValue[value]; | ||
|
||
const PhoneType._(super.value, super.name); | ||
} | ||
|
||
|
||
const $core.bool _omitEnumNames = $core.bool.fromEnvironment('protobuf.omit_enum_names'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the ProtobufEnumStyle is not defined, I keep the orignal way of doing to not incorporate breaking change