generate method

void generate(
  1. IndentingWriter out
)

Implementation

void generate(IndentingWriter out) {
  if (!_resolved) throw StateError('resolve not called');

  final name = _extensionName;
  final type = _field.baseType;
  final dartType = type.getDartType(fileGen!);

  final omitFieldNames = ConditionalConstDefinition('omit_field_names');
  out.addSuffix(
      omitFieldNames.constFieldName, omitFieldNames.constDefinition);
  final conditionalName = omitFieldNames.createTernary(_extensionName);
  final omitMessageNames = ConditionalConstDefinition('omit_message_names');
  out.addSuffix(
      omitMessageNames.constFieldName, omitMessageNames.constDefinition);
  final conditionalExtendedName =
      omitMessageNames.createTernary(_extendedFullName);

  String invocation;
  final positionals = <String>[];
  positionals.add(conditionalExtendedName);
  positionals.add(conditionalName);
  positionals.add('${_field.number}');
  positionals.add(_field.typeConstant);

  final named = <String, String?>{};
  named['protoName'] = _field.quotedProtoName;
  if (_field.isRepeated) {
    invocation = '$protobufImportPrefix.Extension<$dartType>.repeated';
    named['check'] =
        '$protobufImportPrefix.getCheckFunction(${_field.typeConstant})';
    if (type.isMessage || type.isGroup) {
      named['subBuilder'] = '$dartType.create';
    } else if (type.isEnum) {
      named['valueOf'] = '$dartType.valueOf';
      named['enumValues'] = '$dartType.values';
    }
  } else {
    invocation = '$protobufImportPrefix.Extension<$dartType>';
    named['defaultOrMaker'] = _field.generateDefaultFunction();
    if (type.isMessage || type.isGroup) {
      named['subBuilder'] = '$dartType.create';
    } else if (type.isEnum) {
      final dartEnum = type.getDartType(fileGen!);
      named['valueOf'] = '$dartEnum.valueOf';
      named['enumValues'] = '$dartEnum.values';
    }
  }
  final fieldDefinition = 'static final ';
  out.printAnnotated(
      '$fieldDefinition$name = '
      '$invocation(${ProtobufField._formatArguments(positionals, named)});\n',
      [
        NamedLocation(
            name: name,
            fieldPathSegment: List.from(fieldPath),
            start: fieldDefinition.length)
      ]);
}