function getSchemaForStdScalar()

in packages/typespec-powershell/src/utils/modelUtils.ts [1350:1522]


function getSchemaForStdScalar(
  dpgContext: SdkContext,
  type: Scalar,
  options?: GetSchemaOptions
) {
  const { relevantProperty } = options ?? {};
  if (!dpgContext.program.checker.isStdType(type)) {
    return undefined;
  }

  /**
   * lookup for @encode decorator
   *  if absent use typespec type (or default way of serializing that type)
   *  if present respect type provided in @encode
   */
  let format = undefined;
  if (relevantProperty) {
    const encodeData = getEncode(dpgContext.program, relevantProperty);
    if (encodeData && isEncodeTypeEffective(type, encodeData)) {
      type = encodeData.type;
      format = encodeData.encoding;
    }
  }
  const name = type.name;
  const description = getSummary(dpgContext.program, type);
  switch (name) {
    case "bytes":
      return { type: "string", format: "bytes", description };
    case "integer":
      return applyIntrinsicDecorators(dpgContext, type, {
        type: "integer"
      });
    case "int8":
      return applyIntrinsicDecorators(dpgContext, type, {
        type: "integer",
        precision: 8
      });
    case "int16":
      return applyIntrinsicDecorators(dpgContext, type, {
        type: "integer",
        precision: 16
      });
    case "int32":
      return applyIntrinsicDecorators(dpgContext, type, {
        type: "integer",
        precision: 32
      });
    case "int64":
      return applyIntrinsicDecorators(dpgContext, type, {
        type: "integer",
        precision: 64
      });
    case "safeint":
      return applyIntrinsicDecorators(dpgContext, type, {
        type: "integer",
        format: "safeint"
      });
    // ToDo: by xiaogang, need handle the following number types
    case "uint8":
      return applyIntrinsicDecorators(dpgContext, type, {
        type: "number",
        format: "uint8"
      });
    case "uint16":
      return applyIntrinsicDecorators(dpgContext, type, {
        type: "number",
        format: "uint16"
      });
    case "uint32":
      return applyIntrinsicDecorators(dpgContext, type, {
        type: "number",
        format: "uint32"
      });
    case "uint64":
      return applyIntrinsicDecorators(dpgContext, type, {
        type: "number",
        format: "uint64"
      });
    case "float64":
      return applyIntrinsicDecorators(dpgContext, type, {
        type: "number",
        precision: 64
      });
    case "float32":
      return applyIntrinsicDecorators(dpgContext, type, {
        type: "number",
        precision: 32
      });
    case "float":
      return applyIntrinsicDecorators(dpgContext, type, {
        type: "number",
        precision: 32
      });
    case "decimal":
      reportDiagnostic(dpgContext.program, {
        code: "decimal-to-number",
        format: {
          propertyName: relevantProperty?.name ?? ""
        },
        target: relevantProperty ?? type
      });
      return applyIntrinsicDecorators(dpgContext, type, {
        type: "number",
        format: "decimal",
        description: "decimal"
      });
    case "decimal128":
      reportDiagnostic(dpgContext.program, {
        code: "decimal-to-number",
        format: {
          propertyName: relevantProperty?.name ?? ""
        },
        target: relevantProperty ?? type
      });
      return applyIntrinsicDecorators(dpgContext, type, {
        type: "number",
        format: "decimal128",
        description: "decimal128"
      });
    case "string":
      if (format === "binary") {
        return {
          type: "string",
          format: "binary",
          description,
          typeName: BINARY_TYPE_UNION,
          outputTypeName: "Uint8Array"
        };
      }
      return applyIntrinsicDecorators(dpgContext, type, {
        type: "string",
        format
      });
    case "boolean":
      return { type: "boolean", description };
    case "plainDate":
      return {
        type: "string",
        format: "date-time",
        description,
        typeName: "Date | string",
        outputTypeName: "string"
      };
    case "utcDateTime":
      return {
        type: "string",
        format: "date-time",
        description,
        typeName: "Date | string",
        outputTypeName: "string"
      };
    case "offsetDateTime":
      return {
        type: "string",
        format: "date-time",
        description,
        typeName: "string",
        outputTypeName: "string"
      };
    case "plainTime":
      return {
        type: "string",
        format: "time",
        description,
        typeName: "Date | string",
        outputTypeName: "string"
      };
    case "duration":
      return { type: "string", format, description };
    case "url":
      return { type: "string", format: "uri" };
  }
}