function getSchemaForScalar()

in packages/typespec-powershell/src/utils/modelUtils.ts [401:489]


function getSchemaForScalar(
  dpgContext: SdkContext,
  scalar: Scalar,
  options?: GetSchemaOptions
) {
  let result = {} as any;
  const isStd = dpgContext.program.checker.isStdType(scalar);
  const {
    relevantProperty,
    isRequestBody,
    isParentRequestBody,
    mediaTypes: contentTypes
  } = options ?? {};
  if (isStd) {
    result = getSchemaForStdScalar(dpgContext, scalar, {
      relevantProperty
    });
  } else if (scalar.baseScalar) {
    result = getSchemaForScalar(dpgContext, scalar.baseScalar);
  }
  result.language = {};
  result.language.default = {};
  result.language.default.description = getDoc(dpgContext.program, scalar) || "";
  result.language.default.name = scalar.name;
  if (isBinaryAsRequestBody()) {
    // bytes in the body of application/octet-stream is the raw binary payload/file
    result.typeName = BINARY_TYPE_UNION;
    result.outputTypeName = "Uint8Array";
    return result;
  } else if (isFormDataBytesInRequestBody()) {
    // bytes inside a multipart part (for now) is assumed to be file
    result.typeName = BINARY_AND_FILE_TYPE_UNION;
    result.outputTypeName = "Uint8Array";
    return result;
  } else {
    // for other cases we would trust the @encode decorator if not present we would treat it as string
    const withDecorators = applyEncoding(
      dpgContext,
      scalar,
      result
        ? applyIntrinsicDecorators(dpgContext, scalar, result)
        : undefined
    );
    if (
      withDecorators.type === "string" &&
      withDecorators.format === "binary"
    ) {
      withDecorators.typeName = BINARY_TYPE_UNION;
      withDecorators.outputTypeName = "Uint8Array";
    }
    if (withDecorators.type === "string") {
      handleFormat(withDecorators);
    }
    return withDecorators;
  }

  function handleFormat(schema: any) {
    switch (schema.format) {
      case "uri":
        schema.type = SchemaType.Uri;
        break;
      case "date-time":
        schema.type = SchemaType.DateTime;
        break;
      case "base64url":
        schema.type = SchemaType.ByteArray;
        break;
    }
  }
  function isBinaryAsRequestBody() {
    return false;
    // ToDO: by xiaogang
    // return (
    //   hasMediaType(KnownMediaType.Binary, contentTypes) &&
    //   isRequestBody &&
    //   isBytesType(result)
    // );
  }

  function isFormDataBytesInRequestBody() {
    return false;
    // ToDo: by xiaogang
    // return (
    //   hasMediaType(KnownMediaType.MultipartFormData, contentTypes) &&
    //   isParentRequestBody &&
    //   isBytesType(result)
    // );
  }
}