function codeForBranchingOnSubtypeWithSubtypeMapper()

in src/plugins/coding.ts [1079:1125]


function codeForBranchingOnSubtypeWithSubtypeMapper(
  algebraicType: AlgebraicType.Type,
  subtypeValueAccessor: string,
  subtypeMapper: (
    algebraicType: AlgebraicType.Type,
    subtype: AlgebraicType.Subtype,
  ) => string[],
  fallbackDecodingFunction: string | null,
): string[] {
  const subtypeBranches: string[] = algebraicType.subtypes.reduce(
    (soFar, subtype) =>
      codeForSubtypeBranchesWithSubtypeMapper(
        algebraicType,
        subtypeValueAccessor,
        subtypeMapper,
        soFar,
        subtype,
      ),
    [],
  );

  if (fallbackDecodingFunction != null) {
    const fallbackDecodingFunctionInvocation: string[] = [
      'else {',
      StringUtils.indent(2)(
        'return ' + fallbackDecodingFunction + '(aDecoder);',
      ),
      '}',
    ];
    return subtypeBranches.concat(fallbackDecodingFunctionInvocation);
  } else {
    const failureCase: string[] = [
      'else {',
      StringUtils.indent(2)(
        '[aDecoder failWithError:[NSError errorWithDomain:NSCocoaErrorDomain code:NSCoderReadCorruptError ' +
          'userInfo:@{NSDebugDescriptionErrorKey:[NSString stringWithFormat:@"*** [%@ %@]: Invalid subtype provided: %@", ' +
          '[self class], NSStringFromSelector(_cmd), ' +
          codeableAttributeForSubtypePropertyOfAlgebraicType().valueAccessor +
          ']}]];',
      ),
      StringUtils.indent(2)('return nil;'),
      '}',
    ];

    return subtypeBranches.concat(failureCase);
  }
}