Skip to content

Commit

Permalink
Generate actual enums (google#931)
Browse files Browse the repository at this point in the history
commit 382993b
Author: fischerscode <[email protected]>
Date:   Sat May 11 03:49:05 2024 +0200

    tests

commit 2689dd1
Author: fischerscode <[email protected]>
Date:   Sat May 11 03:48:53 2024 +0200

    also test protobuf.omit_enum_names=true

commit 96d7cad
Author: fischerscode <[email protected]>
Date:   Sat May 11 03:48:38 2024 +0200

    regenerate

commit 64850a2
Author: fischerscode <[email protected]>
Date:   Sat May 11 03:47:46 2024 +0200

    generator
  • Loading branch information
fischerscode committed May 11, 2024
1 parent ccf104d commit d556e3a
Show file tree
Hide file tree
Showing 33 changed files with 384 additions and 345 deletions.
2 changes: 1 addition & 1 deletion protoc_plugin/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ update-pregenerated: $(PLUGIN_PATH) $(PREGENERATED_SRCS)
protos: $(PLUGIN_PATH) $(TEST_PROTO_LIBS)

run-tests: protos
dart test
dart test && dart -Dprotobuf.omit_enum_names=true test

update-goldens: protos
rm -rf test/goldens
Expand Down
46 changes: 23 additions & 23 deletions protoc_plugin/lib/src/enum_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ class EnumGenerator extends ProtobufContainer {
out.println('@$coreImportPrefix.Deprecated(\'This enum is deprecated\')');
}
out.addAnnotatedBlock(
'class $classname extends $protobufImportPrefix.ProtobufEnum {',
'enum $classname implements $protobufImportPrefix.ProtobufEnum {',
'}\n', [
NamedLocation(
name: classname, fieldPathSegment: fieldPath, start: 'class '.length)
name: classname, fieldPathSegment: fieldPath, start: 'enum '.length)
], () {
// -----------------------------------------------------------------
// Define enum types.
Expand All @@ -137,16 +137,14 @@ class EnumGenerator extends ProtobufContainer {
'@$coreImportPrefix.Deprecated(\'This enum value is deprecated\')');
}

out.printlnAnnotated(
'static const $classname $name = '
'$classname._(${val.number}, $conditionalValName);',
[
NamedLocation(
name: name,
fieldPathSegment: fieldPathSegment,
start: 'static const $classname '.length)
]);
out.printlnAnnotated('$name(${val.number}, $conditionalValName),', [
NamedLocation(
name: name, fieldPathSegment: fieldPathSegment, start: 0)
]);
out.println();
}
out.println(';');

if (_aliases.isNotEmpty) {
out.println();
for (var i = 0; i < _aliases.length; i++) {
Expand All @@ -164,27 +162,29 @@ class EnumGenerator extends ProtobufContainer {
]);
}
}
out.println();

out.println('static const $coreImportPrefix.List<$classname> values ='
' <$classname> [');
for (final val in _canonicalValues) {
final name = dartNames[val.name];
out.println(' $name,');
}
out.println('];');
out.println();

out.println(
'static final $coreImportPrefix.Map<$coreImportPrefix.int, $classname> _byValue ='
' $protobufImportPrefix.ProtobufEnum.initByValue(values);');
out.println('static $classname? valueOf($coreImportPrefix.int value) =>'
' _byValue[value];');
out.println();

out.println();
out.println('@$coreImportPrefix.override');
out.println('final $coreImportPrefix.int value;');
out.println();
out.println('@$coreImportPrefix.override');
out.println('final $coreImportPrefix.String name;');
out.println();
out.println('const $classname(this.value, this.name);');
out.println();
out.println(
'const $classname._($coreImportPrefix.int v, $coreImportPrefix.String n) '
': super(v, n);');
"/// Returns this enum's [name] or the [value] if names are not"
' represented.');
out.println('@$coreImportPrefix.override');
out.println("$coreImportPrefix.String toString() => name == '' ? "
'value.toString() : name;');
});
}

Expand Down
1 change: 0 additions & 1 deletion protoc_plugin/lib/src/file_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,6 @@ class FileGenerator extends ProtobufContainer {
// Generated code. Do not modify.
// source: ${descriptor.name}
//
// @dart = 2.12
''');
ignorelines.forEach(out.println);
out.println('');
Expand Down
5 changes: 3 additions & 2 deletions protoc_plugin/lib/src/generated/dart_options.pb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Generated code. Do not modify.
// source: dart_options.proto
//
// @dart = 2.12

// ignore_for_file: annotate_overrides, camel_case_types, comment_references
// ignore_for_file: constant_identifier_names, library_prefixes
Expand All @@ -13,6 +12,8 @@ import 'dart:core' as $core;

import 'package:protobuf/protobuf.dart' as $pb;

export 'package:protobuf/protobuf.dart' show GeneratedMessageGenericExtensions;

/// A mixin that can be used in the 'with' clause of the generated Dart class
/// for a proto message.
class DartMixin extends $pb.GeneratedMessage {
Expand Down Expand Up @@ -171,7 +172,7 @@ class Imports extends $pb.GeneratedMessage {
/// so the generated code may contain errors. Therefore, running dartanalyzer
/// on the generated file is a good idea.
@$pb.TagNumber(1)
$core.List<DartMixin> get mixins => $_getList(0);
$pb.PbList<DartMixin> get mixins => $_getList(0);
}

class Dart_options {
Expand Down
Loading

0 comments on commit d556e3a

Please sign in to comment.