writeClassDecode method

  1. @override
void writeClassDecode(
  1. SwiftOptions generatorOptions,
  2. Root root,
  3. Indent indent,
  4. Class klass,
  5. Set<String> customClassNames,
  6. Set<String> customEnumNames, {
  7. required String dartPackageName,
})
override

Writes a single class decode method to indent.

Implementation

@override
void writeClassDecode(
  SwiftOptions generatorOptions,
  Root root,
  Indent indent,
  Class klass,
  Set<String> customClassNames,
  Set<String> customEnumNames, {
  required String dartPackageName,
}) {
  final String className = klass.name;
  indent.write('static func fromList(_ list: [Any?]) -> $className? ');

  indent.addScoped('{', '}', () {
    enumerate(getFieldsInSerializationOrder(klass),
        (int index, final NamedType field) {
      final String listValue = 'list[$index]';

      _writeDecodeCasting(
        root: root,
        indent: indent,
        value: listValue,
        variableName: field.name,
        type: field.type,
        listEncodedClassNames: customClassNames,
        listEncodedEnumNames: customEnumNames,
      );
    });

    indent.newln();
    indent.write('return ');
    indent.addScoped('$className(', ')', () {
      for (final NamedType field in getFieldsInSerializationOrder(klass)) {
        final String comma =
            getFieldsInSerializationOrder(klass).last == field ? '' : ',';
        indent.writeln('${field.name}: ${field.name}$comma');
      }
    });
  });
}