Remove class type prefix from enums code generation for class-based enums languages

26 views
Skip to first unread message

Daniel Fernandes

unread,
Jul 14, 2021, 8:13:54 AMJul 14
to Protocol Buffers

Hello guys,

I'm generating some enums from proto files to be used in a flutter/dart application using the default "protoc-gen-dart" plugin. A Sample proto definition looks like this:

syntax = "proto3";
enum TransactionType {
    TransactionTypeNotSet = 0;
    TransactionTypeFirstType = 1;
    TransactionTypeSecondType= 2;
    TransactionTypeThirdType = 3;
}

And the generated code from the plugin looks like this:
class TransactionType extends $pb.ProtobufEnum {
     static const TransactionType TransactionTypeNotSet = TransactionType._(0, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'TransactionTypeNotSet');
    static const TransactionType TransactionTypeFirstType = TransactionType._(0, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'TransactionTypeFirstType');
    static const TransactionType TransactionTypeSecondType = TransactionType._(0, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'TransactionTypeSecondType');
    static const TransactionType TransactionTypeThirdType = TransactionType._(0, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'TransactionTypeThirdType');
}

So currently I have to specify the enums like this, "TransactionType.TransactionTypeFirstType",
which adds some unwanted redundancy, when I could just have it like this, "TransactionType.FirstType". So what I would like is to have some kind of class name prefix removal from the generated enums, which would also be useful for other languages which follow a class-based enum approach. Is this possible?

Some final remarks: The reason why I just don't specify the enums as "FirstType", "SecondType" instead of "TransactionTypeFirstType" in the protos is due to the fact that i have some others enum types whose values share the same naming (ex: FirstType), which would have re-declaration problems.

Thanks and Regards.
Daniel Fernandes

David Raleigh

unread,
Jul 14, 2021, 12:56:12 PMJul 14
to Daniel Fernandes, Protocol Buffers
if you place this enum definition within a message scope you could avoid these conflicts.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/7af5114f-25b7-4e21-a917-83aeb5c66e34n%40googlegroups.com.

Daniel Fernandes

unread,
Jul 19, 2021, 7:43:12 AMJul 19
to Protocol Buffers
That's a good approach to overcome the problem. Thank you for the tip!
Reply all
Reply to author
Forward
0 new messages