writeClassDecode method

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

Writes a single class decode method to indent.

Implementation

@override
void writeClassDecode(
  ObjcOptions generatorOptions,
  Root root,
  Indent indent,
  Class classDefinition, {
  required String dartPackageName,
}) {
  final String className =
      _className(generatorOptions.prefix, classDefinition.name);
  indent.write('+ ($className *)fromList:(NSArray<id> *)list ');
  indent.addScoped('{', '}', () {
    const String resultName = 'pigeonResult';
    indent.writeln('$className *$resultName = [[$className alloc] init];');
    enumerate(getFieldsInSerializationOrder(classDefinition),
        (int index, final NamedType field) {
      final String valueGetter = 'GetNullableObjectAtIndex(list, $index)';
      final String? primitiveExtractionMethod =
          _nsnumberExtractionMethod(field.type);
      final String ivarValueExpression;
      if (field.type.isEnum && !field.type.isNullable) {
        final String varName =
            'boxed${_enumName(field.type.baseName, prefix: generatorOptions.prefix)}';
        _writeEnumBoxToEnum(
          indent,
          field,
          varName,
          valueGetter,
          prefix: generatorOptions.prefix,
        );
        ivarValueExpression = '$varName.value';
      } else if (primitiveExtractionMethod != null) {
        ivarValueExpression = '[$valueGetter $primitiveExtractionMethod]';
      } else {
        ivarValueExpression = valueGetter;
      }
      indent.writeln('$resultName.${field.name} = $ivarValueExpression;');
    });
    indent.writeln('return $resultName;');
  });

  indent.write(
      '+ (nullable $className *)nullableFromList:(NSArray<id> *)list ');
  indent.addScoped('{', '}', () {
    indent.writeln('return (list) ? [$className fromList:list] : nil;');
  });
}