generate method

void generate(
  1. IndentingWriter out
)

Implementation

void generate(IndentingWriter out) {
  final comment = fileGen?.commentBlock(fieldPath!);
  if (comment != null) {
    out.println(comment);
  }
  out.addAnnotatedBlock(
      'class $classname extends $protobufImportPrefix.ProtobufEnum {',
      '}\n', [
    NamedLocation(
        name: classname!,
        fieldPathSegment: fieldPath!,
        start: 'class '.length)
  ], () {
    // -----------------------------------------------------------------
    // Define enum types.
    for (var i = 0; i < _canonicalValues.length; i++) {
      final val = _canonicalValues[i];
      final name = dartNames[val.name]!;
      final omitEnumNames = ConditionalConstDefinition('omit_enum_names');
      out.addSuffix(
          omitEnumNames.constFieldName, omitEnumNames.constDefinition);
      final conditionalValName = omitEnumNames.createTernary(val.name);
      out.printlnAnnotated(
          'static const $classname $name = '
          '$classname._(${val.number}, $conditionalValName);',
          [
            NamedLocation(
                name: name,
                fieldPathSegment: List.from(fieldPath!)
                  ..addAll([_enumValueTag, _originalCanonicalIndices[i]]),
                start: 'static const $classname '.length)
          ]);
    }
    if (_aliases.isNotEmpty) {
      out.println();
      for (var i = 0; i < _aliases.length; i++) {
        final alias = _aliases[i];
        final name = dartNames[alias.value.name]!;
        out.printlnAnnotated(
            'static const $classname $name ='
            ' ${dartNames[alias.canonicalValue.name]};',
            [
              NamedLocation(
                  name: name,
                  fieldPathSegment: List.from(fieldPath!)
                    ..addAll([_enumValueTag, _originalAliasIndices[i]]),
                  start: 'static const $classname '.length)
            ]);
      }
    }
    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(
        'const $classname._($coreImportPrefix.int v, $coreImportPrefix.String n) '
        ': super(v, n);');
  });
}