writeDataClass method

  1. @override
void writeDataClass(
  1. GObjectOptions generatorOptions,
  2. Root root,
  3. Indent indent,
  4. Class classDefinition, {
  5. required String dartPackageName,
})
override

Writes a single data class to indent.

Implementation

@override
void writeDataClass(
  GObjectOptions generatorOptions,
  Root root,
  Indent indent,
  Class classDefinition, {
  required String dartPackageName,
}) {
  final String module = _getModule(generatorOptions, dartPackageName);
  final String className = _getClassName(module, classDefinition.name);
  final String methodPrefix = _getMethodPrefix(module, classDefinition.name);

  indent.newln();
  addDocumentationComments(
      indent,
      <String>[
        '$className:',
        '',
        ...classDefinition.documentationComments,
      ],
      _docCommentSpec);

  indent.newln();
  _writeDeclareFinalType(indent, module, classDefinition.name);

  indent.newln();
  final List<String> constructorArgs = <String>[];
  for (final NamedType field in classDefinition.fields) {
    final String fieldName = _getFieldName(field.name);
    final String type = _getType(module, field.type);
    constructorArgs.add('$type $fieldName');
    if (_isNumericListType(field.type)) {
      constructorArgs.add('size_t ${fieldName}_length');
    }
  }
  final List<String> constructorFieldCommentLines = <String>[];
  for (final NamedType field in classDefinition.fields) {
    final String fieldName = _getFieldName(field.name);
    constructorFieldCommentLines.add('$fieldName: field in this object.');
    if (_isNumericListType(field.type)) {
      constructorFieldCommentLines
          .add('${fieldName}_length: length of @$fieldName.');
    }
  }
  addDocumentationComments(
      indent,
      <String>[
        '${methodPrefix}_new:',
        ...constructorFieldCommentLines,
        '',
        'Creates a new #${classDefinition.name} object.',
        '',
        'Returns: a new #$className',
      ],
      _docCommentSpec);

  indent.writeln(
      "$className* ${methodPrefix}_new(${constructorArgs.join(', ')});");

  for (final NamedType field in classDefinition.fields) {
    final String fieldName = _getFieldName(field.name);
    final String returnType = _getType(module, field.type);

    indent.newln();
    addDocumentationComments(
        indent,
        <String>[
          '${methodPrefix}_get_$fieldName',
          '@object: a #$className.',
          if (_isNumericListType(field.type))
            '@length: location to write the length of this value.',
          '',
          if (field.documentationComments.isNotEmpty)
            ...field.documentationComments
          else
            'Gets the value of the ${field.name} field of @object.',
          '',
          'Returns: the field value.',
        ],
        _docCommentSpec);
    final List<String> getterArgs = <String>[
      '$className* object',
      if (_isNumericListType(field.type)) 'size_t* length'
    ];
    indent.writeln(
        '$returnType ${methodPrefix}_get_$fieldName(${getterArgs.join(', ')});');
  }
}