generateFieldAccessorsMutators method

void generateFieldAccessorsMutators(
  1. ProtobufField field,
  2. IndentingWriter out,
  3. List<int> memberFieldPath
)

Implementation

void generateFieldAccessorsMutators(
    ProtobufField field, IndentingWriter out, List<int> memberFieldPath) {
  final fieldTypeString = field.getDartType();
  final defaultExpr = field.getDefaultExpr();
  final names = field.memberNames;

  final commentBlock = fileGen.commentBlock(memberFieldPath);
  if (commentBlock != null) {
    out.println(commentBlock.trim());
  }

  _emitDeprecatedIf(field.isDeprecated, out);
  _emitOverrideIf(field.overridesGetter, out);
  _emitIndexAnnotation(field.number, out);
  final getterExpr = _getterExpression(fieldTypeString, field.index!,
      defaultExpr, field.isRepeated, field.isMapField);

  out.printlnAnnotated(
      '$fieldTypeString get ${names!.fieldName} => $getterExpr;', [
    NamedLocation(
        name: names.fieldName,
        fieldPathSegment: memberFieldPath,
        start: '$fieldTypeString get '.length)
  ]);

  if (field.isRepeated) {
    if (field.overridesSetter) {
      throw 'Field ${field.fullName} cannot override a setter for '
          '${names.fieldName} because it is repeated.';
    }
    if (field.overridesHasMethod) {
      throw 'Field ${field.fullName} cannot override '
          '${names.hasMethodName}() because it is repeated.';
    }
    if (field.overridesClearMethod) {
      throw 'Field ${field.fullName} cannot override '
          '${names.clearMethodName}() because it is repeated.';
    }
  } else {
    final fastSetter = field.baseType.setter;
    _emitDeprecatedIf(field.isDeprecated, out);
    _emitOverrideIf(field.overridesSetter, out);
    _emitIndexAnnotation(field.number, out);
    if (fastSetter != null) {
      out.printlnAnnotated(
          'set ${names.fieldName}'
          '($fieldTypeString v) { '
          '$fastSetter(${field.index}, v);'
          ' }',
          [
            NamedLocation(
                name: names.fieldName,
                fieldPathSegment: memberFieldPath,
                start: 'set '.length)
          ]);
    } else {
      out.printlnAnnotated(
          'set ${names.fieldName}'
          '($fieldTypeString v) { '
          'setField(${field.number}, v);'
          ' }',
          [
            NamedLocation(
                name: names.fieldName,
                fieldPathSegment: memberFieldPath,
                start: 'set '.length)
          ]);
    }
    if (field.hasPresence) {
      _emitDeprecatedIf(field.isDeprecated, out);
      _emitOverrideIf(field.overridesHasMethod, out);
      _emitIndexAnnotation(field.number, out);
      out.printlnAnnotated(
          '$coreImportPrefix.bool ${names.hasMethodName}() =>'
          ' \$_has(${field.index});',
          [
            NamedLocation(
                name: names.hasMethodName!,
                fieldPathSegment: memberFieldPath,
                start: '$coreImportPrefix.bool '.length)
          ]);
    }
    _emitDeprecatedIf(field.isDeprecated, out);
    _emitOverrideIf(field.overridesClearMethod, out);
    _emitIndexAnnotation(field.number, out);
    out.printlnAnnotated(
        'void ${names.clearMethodName}() =>'
        ' clearField(${field.number});',
        [
          NamedLocation(
              name: names.clearMethodName!,
              fieldPathSegment: memberFieldPath,
              start: 'void '.length)
        ]);
    if (field.baseType.isMessage) {
      _emitDeprecatedIf(field.isDeprecated, out);
      _emitIndexAnnotation(field.number, out);
      out.printlnAnnotated(
          '$fieldTypeString ${names.ensureMethodName}() => '
          '\$_ensure(${field.index});',
          <NamedLocation>[
            NamedLocation(
                name: names.ensureMethodName!,
                fieldPathSegment: memberFieldPath,
                start: '$fieldTypeString '.length)
          ]);
    }
  }
}