generate method

void generate(
  1. IndentingWriter out
)

Implementation

void generate(IndentingWriter out) {
  checkResolved();

  for (final m in _messageGenerators) {
    // Don't output the generated map entry type. Instead, the `PbMap` type
    // from the protobuf library is used to hold the keys and values.
    if (m._descriptor.options.hasMapEntry()) continue;
    m.generate(out);
  }

  for (final oneof in _oneofNames) {
    OneofEnumGenerator.generate(
        out, oneof.oneofEnumName, _oneofFields[oneof.index]);
  }

  var mixinClause = '';
  if (mixin != null) {
    final mixinNames =
        mixin!.findMixinsToApply().map((m) => '$mixinImportPrefix.${m.name}');
    mixinClause = ' with ${mixinNames.join(", ")}';
  }

  final omitMessageNames = ConditionalConstDefinition('omit_message_names');
  out.addSuffix(
      omitMessageNames.constFieldName, omitMessageNames.constDefinition);

  final conditionalPackageName = 'const $protobufImportPrefix.PackageName'
      '(${omitMessageNames.createTernary(package)})';

  final packageClause =
      package == '' ? '' : ', package: $conditionalPackageName';
  final proto3JsonClause = (mixin?.hasProto3JsonHelpers ?? false)
      ? ', toProto3Json: $mixinImportPrefix.${mixin!.name}.toProto3JsonHelper, '
          'fromProto3Json: $mixinImportPrefix.${mixin!.name}.fromProto3JsonHelper'
      : '';

  final String extendedClass;
  if (_descriptor.options.messageSetWireFormat) {
    extendedClass = '\$_MessageSet';
  } else {
    extendedClass = 'GeneratedMessage';
  }

  var commentBlock = fileGen.commentBlock(fieldPath) ?? '';
  if (commentBlock.isNotEmpty) {
    commentBlock = '$commentBlock\n';
  }

  out.addAnnotatedBlock(
      '${commentBlock}class $classname extends $protobufImportPrefix.$extendedClass$mixinClause {',
      '}', [
    NamedLocation(
        name: classname, fieldPathSegment: fieldPath, start: 'class '.length)
  ], () {
    _generateFactory(out);

    out.printlnAnnotated('$classname._() : super();', [
      NamedLocation(name: classname, fieldPathSegment: fieldPath, start: 0)
    ]);
    out.println(
        'factory $classname.fromBuffer($coreImportPrefix.List<$coreImportPrefix.int> i,'
        ' [$protobufImportPrefix.ExtensionRegistry r = $protobufImportPrefix.ExtensionRegistry.EMPTY])'
        ' => create()..mergeFromBuffer(i, r);');
    out.println('factory $classname.fromJson($coreImportPrefix.String i,'
        ' [$protobufImportPrefix.ExtensionRegistry r = $protobufImportPrefix.ExtensionRegistry.EMPTY])'
        ' => create()..mergeFromJson(i, r);');

    out.println();
    for (final oneof in _oneofNames) {
      out.addBlock(
          'static const $coreImportPrefix.Map<$coreImportPrefix.int, ${oneof.oneofEnumName}> ${oneof.byTagMapName} = {',
          '};', () {
        for (final field in _oneofFields[oneof.index]) {
          final oneofMemberName =
              oneofEnumMemberName(field.memberNames!.fieldName);
          out.println(
              '${field.number} : ${oneof.oneofEnumName}.$oneofMemberName,');
        }
        out.println('0 : ${oneof.oneofEnumName}.notSet');
      });
    }

    final omitMessageNames = ConditionalConstDefinition('omit_message_names');
    out.addSuffix(
        omitMessageNames.constFieldName, omitMessageNames.constDefinition);

    out.addBlock(
        'static final $protobufImportPrefix.BuilderInfo _i = '
            '$protobufImportPrefix.BuilderInfo(${omitMessageNames.createTernary(messageName)}'
            '$packageClause'
            ', createEmptyInstance: create'
            '$proto3JsonClause)',
        ';', () {
      for (var oneof = 0; oneof < _oneofFields.length; oneof++) {
        final tags =
            _oneofFields[oneof].map((ProtobufField f) => f.number).toList();
        out.println('..oo($oneof, $tags)');
      }

      for (final field in _fieldList) {
        field.generateBuilderInfoCall(out, package);
      }

      if (_descriptor.extensionRange.isNotEmpty) {
        out.println('..hasExtensions = true');
      }
      if (!_hasRequiredFields(this, {})) {
        out.println('..hasRequiredFields = false');
      }
    });

    for (final x in _extensionGenerators) {
      x.generate(out);
    }

    out.println();
    out.println('''@$coreImportPrefix.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.deepCopy] instead. '
'Will be removed in next major version')''');
    out.println('$classname clone() =>'
        ' $classname()..mergeFromMessage(this);');
    out.println('''@$coreImportPrefix.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.rebuild] instead. '
'Will be removed in next major version')''');
    out.println('$classname copyWith(void Function($classname) updates) =>'
        ' super.copyWith((message) => updates(message as $classname))'
        ' as $classname;');

    out.println('');
    out.println('$protobufImportPrefix.BuilderInfo get info_ => _i;');

    // Factory functions which can be used as default value closures.
    out.println('');
    out.println("@$coreImportPrefix.pragma('dart2js:noInline')");
    out.println('static $classname create() => $classname._();');
    out.println('$classname createEmptyInstance() => create();');

    out.println(
        'static $protobufImportPrefix.PbList<$classname> createRepeated() =>'
        ' $protobufImportPrefix.PbList<$classname>();');
    out.println("@$coreImportPrefix.pragma('dart2js:noInline')");
    out.println('static $classname getDefault() =>'
        ' _defaultInstance ??='
        ' $protobufImportPrefix.GeneratedMessage.\$_defaultFor<$classname>'
        '(create);');
    out.println('static $classname? _defaultInstance;');

    generateFieldsAccessorsMutators(out);
    mixin?.injectHelpers(out);
  });
  out.println();
}