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 *)list ');
  indent.addScoped('{', '}', () {
    const String resultName = 'pigeonResult';
    indent.writeln('$className *$resultName = [[$className alloc] init];');
    enumerate(getFieldsInSerializationOrder(classDefinition),
        (int index, final NamedType field) {
      final bool isEnumType = field.type.isEnum;
      final String valueGetter = 'GetNullableObjectAtIndex(list, $index)';
      final String? primitiveExtractionMethod =
          _nsnumberExtractionMethod(field.type);
      final String ivarValueExpression;
      if (primitiveExtractionMethod != null) {
        ivarValueExpression = '[$valueGetter $primitiveExtractionMethod]';
      } else if (isEnumType) {
        indent.writeln('NSNumber *${field.name}AsNumber = $valueGetter;');
        indent.writeln(
            '${_enumName(field.type.baseName, suffix: ' *', prefix: generatorOptions.prefix, box: true)}${field.name} = ${field.name}AsNumber == nil ? nil : [[${_enumName(field.type.baseName, prefix: generatorOptions.prefix, box: true)} alloc] initWithValue:[${field.name}AsNumber integerValue]];');
        ivarValueExpression = field.name;
      } else {
        ivarValueExpression = valueGetter;
      }
      indent.writeln('$resultName.${field.name} = $ivarValueExpression;');
    });
    indent.writeln('return $resultName;');
  });

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