function getCastingTemplate()

in libs/designer/src/lib/core/utils/parameters/casting.ts [82:171]


function getCastingTemplate(fromType: string, fromFormat: string, toType: string, toFormat: string): string | undefined {
  /* eslint-disable no-param-reassign */
  fromType = fromType ? fromType.toLowerCase() : '';
  fromFormat = fromFormat ? fromFormat.toLowerCase() : '';
  toType = toType ? toType.toLowerCase() : '';
  toFormat = toFormat ? toFormat.toLowerCase() : '';

  // Note: If toType is any, we should not add any casting function.
  if (toType === Constants.SWAGGER.TYPE.ANY) {
    return undefined;
  }

  // Note: Below code assumed the type is string, thus handle file type here.
  if (fromType === Constants.SWAGGER.TYPE.FILE || toType === Constants.SWAGGER.TYPE.FILE) {
    if (toType === Constants.SWAGGER.TYPE.FILE) {
      if (fromType === Constants.SWAGGER.TYPE.STRING) {
        switch (fromFormat) {
          case Constants.SWAGGER.FORMAT.BYTE:
            return 'base64ToBinary({0})';
          case Constants.SWAGGER.FORMAT.DATAURI:
            return 'decodeDataUri({0})';
          default:
            return undefined;
        }
      }
      return undefined;
    }

    if (fromType === Constants.SWAGGER.TYPE.FILE) {
      if (toType === Constants.SWAGGER.TYPE.STRING) {
        switch (toFormat) {
          case Constants.SWAGGER.FORMAT.BYTE:
            return 'base64({0})';
          case Constants.SWAGGER.FORMAT.DATAURI:
            return `concat('data:;base64,',base64({0}))`;
          default:
            return undefined;
        }
      }
      return undefined;
    }

    return undefined;
  }

  if (equals(fromFormat, toFormat)) {
    return undefined;
  }

  if (toFormat === Constants.SWAGGER.FORMAT.BINARY) {
    switch (fromFormat) {
      case Constants.SWAGGER.FORMAT.BYTE:
        return 'base64ToBinary({0})';
      case Constants.SWAGGER.FORMAT.DATAURI:
        return 'decodeDataUri({0})';
      default:
        return undefined;
    }
  }

  if (toFormat === Constants.SWAGGER.FORMAT.BYTE) {
    switch (fromFormat) {
      case Constants.SWAGGER.FORMAT.DATAURI:
        return 'base64(decodeDataUri({0}))';
      default:
        return 'base64({0})';
    }
  }

  if (toFormat === Constants.SWAGGER.FORMAT.DATAURI) {
    switch (fromFormat) {
      case Constants.SWAGGER.FORMAT.BINARY:
        return `concat('data:;base64,',base64({0}))`;
      case Constants.SWAGGER.FORMAT.BYTE:
        return `concat('data:;base64,',{0})`;
      default:
        return `concat('data:,',encodeURIComponent({0}))`;
    }
  }

  // Converting to string
  switch (fromFormat) {
    case Constants.SWAGGER.FORMAT.BYTE:
      return 'base64ToString({0})';
    case Constants.SWAGGER.FORMAT.DATAURI:
      return 'decodeDataUri({0})';
    default:
      return undefined;
  }
}