generateBuilderInfoCall method

void generateBuilderInfoCall(
  1. IndentingWriter out,
  2. String package
)

Returns Dart code adding this field to a BuilderInfo object. The call will start with ".." and a method name.

Implementation

void generateBuilderInfoCall(IndentingWriter out, String package) {
  assert(descriptor.hasJsonName());

  final omitFieldNames = ConditionalConstDefinition('omit_field_names');
  out.addSuffix(
      omitFieldNames.constFieldName, omitFieldNames.constDefinition);
  final quotedName = omitFieldNames.createTernary(descriptor.jsonName);

  final type = baseType.getDartType(parent.fileGen!);

  String invocation;

  final args = <String>[];
  final named = <String, String?>{'protoName': quotedProtoName};
  args.add('$number');
  args.add(quotedName);

  if (isMapField) {
    final generator = baseType.generator as MessageGenerator;
    final key = generator._fieldList[0];
    final value = generator._fieldList[1];

    // Key type is an integer type or string. No need to specify the default
    // value as the library knows the defaults for integer and string fields.
    final keyType = key.baseType.getDartType(parent.fileGen!);

    // Value type can be anything other than another map.
    final valueType = value.baseType.getDartType(parent.fileGen!);

    invocation = 'm<$keyType, $valueType>';

    named['entryClassName'] = "'${generator.messageName}'";
    named['keyFieldType'] = key.typeConstant;
    named['valueFieldType'] = value.typeConstant;
    if (value.baseType.isMessage || value.baseType.isGroup) {
      named['valueCreator'] = '$valueType.create';
      named['valueDefaultOrMaker'] = value.generateDefaultFunction();
    }
    if (value.baseType.isEnum) {
      named['valueOf'] = '$valueType.valueOf';
      named['enumValues'] = '$valueType.values';
      named['valueDefaultOrMaker'] = value.generateDefaultFunction();
      named['defaultEnumValue'] = value.generateDefaultFunction();
    }
    if (package != '') {
      named['packageName'] =
          'const $protobufImportPrefix.PackageName(\'$package\')';
    }
  } else if (isRepeated) {
    if (typeConstant == '$protobufImportPrefix.PbFieldType.PS') {
      invocation = 'pPS';
    } else {
      args.add(typeConstant);
      if (baseType.isMessage || baseType.isGroup || baseType.isEnum) {
        invocation = 'pc<$type>';
      } else {
        invocation = 'p<$type>';
      }

      if (baseType.isMessage || baseType.isGroup) {
        named['subBuilder'] = '$type.create';
      } else if (baseType.isEnum) {
        named['valueOf'] = '$type.valueOf';
        named['enumValues'] = '$type.values';
        named['defaultEnumValue'] = generateDefaultFunction();
      }
    }
  } else {
    // Singular field.
    final makeDefault = generateDefaultFunction();

    if (baseType.isEnum) {
      args.add(typeConstant);
      named['defaultOrMaker'] = makeDefault;
      named['valueOf'] = '$type.valueOf';
      named['enumValues'] = '$type.values';
      invocation = 'e<$type>';
    } else if (makeDefault == null) {
      switch (type) {
        case '$coreImportPrefix.String':
          if (typeConstant == '$protobufImportPrefix.PbFieldType.OS') {
            invocation = 'aOS';
          } else if (typeConstant == '$protobufImportPrefix.PbFieldType.QS') {
            invocation = 'aQS';
          } else {
            invocation = 'a<$type>';
            args.add(typeConstant);
          }
          break;
        case '$coreImportPrefix.bool':
          if (typeConstant == '$protobufImportPrefix.PbFieldType.OB') {
            invocation = 'aOB';
          } else {
            invocation = 'a<$type>';
            args.add(typeConstant);
          }
          break;
        default:
          invocation = 'a<$type>';
          args.add(typeConstant);
          break;
      }
    } else {
      if (makeDefault == '$_fixnumImportPrefix.Int64.ZERO' &&
          type == '$_fixnumImportPrefix.Int64' &&
          typeConstant == '$protobufImportPrefix.PbFieldType.O6') {
        invocation = 'aInt64';
      } else {
        if (baseType.isMessage || baseType.isGroup) {
          named['subBuilder'] = '$type.create';
        }
        if (baseType.isMessage) {
          invocation = isRequired ? 'aQM<$type>' : 'aOM<$type>';
        } else {
          invocation = 'a<$type>';
          named['defaultOrMaker'] = makeDefault;
          args.add(typeConstant);
        }
      }
    }
  }

  final result = '..$invocation(${_formatArguments(args, named)})';
  out.println(result);
}