protocolbuffers / protocolbuffers.github.io

Other
38 stars 107 forks source link

Dart Generated Code: Enumerations #129

Closed luangong closed 3 months ago

luangong commented 6 months ago

Given the following .proto definition:

syntax = "proto3";

enum Color {
  COLOR_UNSPECIFIED = 0;
  COLOR_RED = 1;
  COLOR_GREEN = 2;
  COLOR_BLUE = 3;
}

The official documentation says that:

The class will include a static const Color for each of the three values defined as well as a static const List<Color> containing all the three non-unspecified values.

But the protobuf compiler generates Dart code like this:

class Color extends $pb.ProtobufEnum {
  static const Color COLOR_UNSPECIFIED = Color._(0, _omitEnumNames ? '' : 'COLOR_UNSPECIFIED');
  static const Color COLOR_RED = Color._(1, _omitEnumNames ? '' : 'COLOR_RED');
  static const Color COLOR_GREEN = Color._(2, _omitEnumNames ? '' : 'COLOR_GREEN');
  static const Color COLOR_BLUE = Color._(3, _omitEnumNames ? '' : 'COLOR_BLUE');

  static const $core.List<Color> values = <Color> [
    COLOR_UNSPECIFIED,
    COLOR_RED,
    COLOR_GREEN,
    COLOR_BLUE,
  ];

  static final $core.Map<$core.int, Color> _byValue = $pb.ProtobufEnum.initByValue(values);
  static Color? valueOf($core.int value) => _byValue[value];

  const Color._($core.int v, $core.String n) : super(v, n);
}

which contains all four values, including COLOR_UNSPECIFIED.

Related: https://github.com/google/protobuf.dart/issues/919

Logofile commented 4 months ago

Thanks for catching that. I was retrofitting the enum examples throughout the docs to add *_UNSPECIFIED values to those that didn't have them, and missed updating the text that went with it. I'm going to leave this issue open until the updated docs are published.

Logofile commented 3 months ago

Updated docs are published. Thanks again for catching this!