private typeHandlers()

in tools/@aws-cdk/spec2cdk/lib/cdk/cloudformation-mapping.ts [102:204]


  private typeHandlers(type: Type): TypeHandlers {
    if (type.equals(CDK_CORE.CfnTag)) {
      return {
        produce: CDK_CORE.cfnTagToCloudFormation,
        parse: CDK_CORE.helpers.FromCloudFormation.getCfnTag,
        validate: CDK_CORE.validateCfnTag,
      };
    }

    if (type.equals(CDK_CORE.IResolvable)) {
      return {
        produce: CDK_CORE.objectToCloudFormation,
        parse: CDK_CORE.helpers.FromCloudFormation.getAny,
        validate: CDK_CORE.validateObject,
      };
    }

    switch (type.primitive) {
      case PrimitiveType.String:
        return {
          produce: CDK_CORE.stringToCloudFormation,
          parse: CDK_CORE.helpers.FromCloudFormation.getString,
          validate: CDK_CORE.validateString,
        };
      case PrimitiveType.DateTime:
        return {
          produce: CDK_CORE.dateToCloudFormation,
          parse: CDK_CORE.helpers.FromCloudFormation.getDate,
          validate: CDK_CORE.validateDate,
        };
      case PrimitiveType.Number:
        return {
          produce: CDK_CORE.numberToCloudFormation,
          parse: CDK_CORE.helpers.FromCloudFormation.getNumber,
          validate: CDK_CORE.validateNumber,
        };
      case PrimitiveType.Json:
        return {
          produce: CDK_CORE.objectToCloudFormation,
          parse: CDK_CORE.helpers.FromCloudFormation.getAny,
          validate: CDK_CORE.validateObject,
        };
      case PrimitiveType.Any:
        return {
          produce: CDK_CORE.objectToCloudFormation,
          parse: CDK_CORE.helpers.FromCloudFormation.getAny,
          validate: CDK_CORE.validateObject,
        };
      case PrimitiveType.Boolean:
        return {
          produce: CDK_CORE.booleanToCloudFormation,
          parse: CDK_CORE.helpers.FromCloudFormation.getBoolean,
          validate: CDK_CORE.validateBoolean,
        };
    }

    if (type.arrayOfType) {
      const innerHandler = this.typeHandlers(type.arrayOfType);
      return {
        produce: CDK_CORE.listMapper(innerHandler.produce),
        parse: CDK_CORE.helpers.FromCloudFormation.getArray(innerHandler.parse),
        validate: CDK_CORE.listValidator.call(innerHandler.validate),
      };
    }

    if (type.mapOfType) {
      const innerHandler = this.typeHandlers(type.mapOfType);
      return {
        produce: CDK_CORE.hashMapper(innerHandler.produce),
        parse: CDK_CORE.helpers.FromCloudFormation.getMap(innerHandler.parse),
        validate: CDK_CORE.hashValidator.call(innerHandler.validate),
      };
    }

    if (type.symbol) {
      const struct = StructType.assertStruct(type.symbol.findDeclaration());
      return {
        produce: expr.sym(new ThingSymbol(cfnProducerNameFromType(struct), this.mapperFunctionsScope)),
        parse: expr.sym(new ThingSymbol(cfnParserNameFromType(struct), this.mapperFunctionsScope)),
        validate: expr.sym(new ThingSymbol(cfnPropsValidatorNameFromType(struct), this.mapperFunctionsScope)),
      };
    }

    if (type.unionOfTypes) {
      // Need access to the PropertyTypes to order these
      const originalTypes = type.unionOfTypes.map((t) => this.converter.originalType(t));
      const orderedTypes = new UnionOrdering(this.converter.db).orderTypewriterTypes(type.unionOfTypes, originalTypes);
      const innerProducers = orderedTypes.map((t) => this.typeHandlers(t));
      const validators = innerProducers.map((p) => p.validate);

      return {
        produce: CDK_CORE.unionMapper(expr.list(validators), expr.list(innerProducers.map((p) => p.produce))),
        parse: CDK_CORE.helpers.FromCloudFormation.getTypeUnion(
          expr.list(validators),
          expr.list(innerProducers.map((p) => p.parse)),
        ),
        validate: CDK_CORE.unionValidator.call(...validators),
      };
    }

    const todo = expr.ident(`/* @todo typeHandlers(${type}) */`);
    return { produce: todo, parse: todo, validate: todo };
  }