writeClassEncode method

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

Writes a single class encode method to indent.

Implementation

@override
void writeClassEncode(
  SwiftOptions generatorOptions,
  Root root,
  Indent indent,
  Class classDefinition, {
  required String dartPackageName,
}) {
  indent.write('func toList() -> [Any?] ');
  indent.addScoped('{', '}', () {
    indent.write('return ');
    indent.addScoped('[', ']', () {
      // Follow swift-format style, which is to use a trailing comma unless
      // there is only one element.
      final String separator = classDefinition.fields.length > 1 ? ',' : '';
      for (final NamedType field
          in getFieldsInSerializationOrder(classDefinition)) {
        String toWriteValue = '';
        final String fieldName = field.name;
        final String nullsafe = field.type.isNullable ? '?' : '';
        if (field.type.isEnum) {
          toWriteValue = '$fieldName$nullsafe.rawValue';
        } else {
          toWriteValue = field.name;
        }

        indent.writeln('$toWriteValue$separator');
      }
    });
  });
}