Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support decimal type in RLC #2170

Merged
merged 7 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/typespec-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"ts-node": "~10.9.1",
"typescript": "~5.2.0",
"prettier": "~2.7.1",
"@azure-tools/cadl-ranch-specs": "^0.26.1",
"@azure-tools/cadl-ranch-specs": "^0.26.2",
"@azure-tools/cadl-ranch-expect": "^0.9.0",
"@azure-tools/cadl-ranch": "^0.10.0",
"chalk": "^4.0.0",
Expand All @@ -71,7 +71,8 @@
"@azure/logger": "^1.0.4",
"@azure/core-util": "^1.4.0",
"eslint-plugin-require-extensions": "0.1.3",
"@typespec/ts-http-runtime": "1.0.0-alpha.20231129.4"
"@typespec/ts-http-runtime": "1.0.0-alpha.20231129.4",
"decimal.js": "10.4.3"
},
"peerDependencies": {
"@azure-tools/typespec-azure-core": ">=0.36.0 <1.0.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/typespec-ts/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ const libDef = {
messages: {
default: paramMessage`Please specify @items property for the paging operation - ${"operationName"}.`
}
},
"decimal-to-number": {
severity: "warning",
messages: {
default: paramMessage`Please note the decimal type will be converted to number. If you strongly care about precision you can use @encode to encode it as a string for the property - ${"propertyName"}.`
}
}
},
emitter: {
Expand Down
45 changes: 41 additions & 4 deletions packages/typespec-ts/src/utils/modelUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ function getSchemaForStdScalar(
case "safeint":
return applyIntrinsicDecorators(program, type, {
type: "number",
format: "int64"
format: "safeint"
});
case "uint8":
return applyIntrinsicDecorators(program, type, {
Expand All @@ -1077,18 +1077,44 @@ function getSchemaForStdScalar(
case "float64":
return applyIntrinsicDecorators(program, type, {
type: "number",
format: "double"
format: "float64"
});
case "float32":
return applyIntrinsicDecorators(program, type, {
type: "number",
format: "float"
format: "float32"
});
case "float":
return applyIntrinsicDecorators(program, type, {
type: "number",
format: "float"
});
case "decimal":
reportDiagnostic(program, {
code: "decimal-to-number",
format: {
propertyName: relevantProperty?.name ?? ""
},
target: relevantProperty ?? type
});
return applyIntrinsicDecorators(program, type, {
type: "number",
format: "decimal",
description: "decimal"
});
case "decimal128":
reportDiagnostic(program, {
code: "decimal-to-number",
format: {
propertyName: relevantProperty?.name ?? ""
},
target: relevantProperty ?? type
});
return applyIntrinsicDecorators(program, type, {
type: "number",
format: "decimal128",
description: "decimal128"
});
case "string":
if (format === "binary") {
return {
Expand Down Expand Up @@ -1243,14 +1269,25 @@ function getEnumStringDescription(type: any) {
return undefined;
}

function getDecimalDescription(type: any) {
if (
(type.format === "decimal" || type.format === "decimal128") &&
type.type === "number"
) {
return `Please note the field was supposed to be a ${type.format} but JavaScript does not have a native 'BigDecimal' data type.\nSo it was converted to a number instead. It is recommended to use a third-party library like 'decimal.js' to handle\nany calculations.`;
MaryGao marked this conversation as resolved.
Show resolved Hide resolved
}
return undefined;
}

export function getFormattedPropertyDoc(
program: Program,
type: ModelProperty | Type,
schemaType: any,
sperator: string = "\n\n"
) {
const propertyDoc = getDoc(program, type);
const enhancedDocFromType = getEnumStringDescription(schemaType);
const enhancedDocFromType =
getEnumStringDescription(schemaType) ?? getDecimalDescription(schemaType);
if (propertyDoc && enhancedDocFromType) {
return `${propertyDoc}${sperator}${enhancedDocFromType}`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import {
IntPutParameters,
FloatGetParameters,
FloatPutParameters,
DecimalGetParameters,
DecimalPutParameters,
Decimal128GetParameters,
Decimal128PutParameters,
DatetimeGetParameters,
DatetimePutParameters,
DurationGetParameters,
Expand Down Expand Up @@ -66,6 +70,10 @@ import {
IntPut204Response,
FloatGet200Response,
FloatPut204Response,
DecimalGet200Response,
DecimalPut204Response,
Decimal128Get200Response,
Decimal128Put204Response,
DatetimeGet200Response,
DatetimePut204Response,
DurationGet200Response,
Expand Down Expand Up @@ -154,6 +162,24 @@ export interface FloatGet {
put(options: FloatPutParameters): StreamableMethod<FloatPut204Response>;
}

export interface DecimalGet {
/** Get call */
get(options?: DecimalGetParameters): StreamableMethod<DecimalGet200Response>;
/** Put operation */
put(options: DecimalPutParameters): StreamableMethod<DecimalPut204Response>;
}

export interface Decimal128Get {
/** Get call */
get(
options?: Decimal128GetParameters
): StreamableMethod<Decimal128Get200Response>;
/** Put operation */
put(
options: Decimal128PutParameters
): StreamableMethod<Decimal128Put204Response>;
}

export interface DatetimeGet {
/** Get call */
get(
Expand Down Expand Up @@ -380,6 +406,10 @@ export interface Routes {
(path: "/type/property/value-types/int"): IntGet;
/** Resource for '/type/property/value-types/float' has methods for the following verbs: get, put */
(path: "/type/property/value-types/float"): FloatGet;
/** Resource for '/type/property/value-types/decimal' has methods for the following verbs: get, put */
(path: "/type/property/value-types/decimal"): DecimalGet;
/** Resource for '/type/property/value-types/decimal128' has methods for the following verbs: get, put */
(path: "/type/property/value-types/decimal128"): Decimal128Get;
/** Resource for '/type/property/value-types/datetime' has methods for the following verbs: get, put */
(path: "/type/property/value-types/datetime"): DatetimeGet;
/** Resource for '/type/property/value-types/duration' has methods for the following verbs: get, put */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@ export interface FloatProperty {
property: number;
}

/** Model with a decimal property */
export interface DecimalProperty {
/**
* Property
*
* Please note the field was supposed to be a decimal but JavaScript does not have a native 'BigDecimal' data type.
* So it was converted to a number instead. It is recommended to use a third-party library like 'decimal.js' to handle
* any calculations.
*/
property: number;
}

/** Model with a decimal128 property */
export interface Decimal128Property {
/**
* Property
*
* Please note the field was supposed to be a decimal128 but JavaScript does not have a native 'BigDecimal' data type.
* So it was converted to a number instead. It is recommended to use a third-party library like 'decimal.js' to handle
* any calculations.
*/
property: number;
}

/** Model with a datetime property */
export interface DatetimeProperty {
/** Property */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@ export interface FloatPropertyOutput {
property: number;
}

/** Model with a decimal property */
export interface DecimalPropertyOutput {
/**
* Property
*
* Please note the field was supposed to be a decimal but JavaScript does not have a native 'BigDecimal' data type.
* So it was converted to a number instead. It is recommended to use a third-party library like 'decimal.js' to handle
* any calculations.
*/
property: number;
}

/** Model with a decimal128 property */
export interface Decimal128PropertyOutput {
/**
* Property
*
* Please note the field was supposed to be a decimal128 but JavaScript does not have a native 'BigDecimal' data type.
* So it was converted to a number instead. It is recommended to use a third-party library like 'decimal.js' to handle
* any calculations.
*/
property: number;
}

/** Model with a datetime property */
export interface DatetimePropertyOutput {
/** Property */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
BytesProperty,
IntProperty,
FloatProperty,
DecimalProperty,
Decimal128Property,
DatetimeProperty,
DurationProperty,
EnumProperty,
Expand Down Expand Up @@ -73,6 +75,23 @@ export interface FloatPutBodyParam {
}

export type FloatPutParameters = FloatPutBodyParam & RequestParameters;
export type DecimalGetParameters = RequestParameters;

export interface DecimalPutBodyParam {
/** body */
body: DecimalProperty;
}

export type DecimalPutParameters = DecimalPutBodyParam & RequestParameters;
export type Decimal128GetParameters = RequestParameters;

export interface Decimal128PutBodyParam {
/** body */
body: Decimal128Property;
}

export type Decimal128PutParameters = Decimal128PutBodyParam &
RequestParameters;
export type DatetimeGetParameters = RequestParameters;

export interface DatetimePutBodyParam {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
BytesPropertyOutput,
IntPropertyOutput,
FloatPropertyOutput,
DecimalPropertyOutput,
Decimal128PropertyOutput,
DatetimePropertyOutput,
DurationPropertyOutput,
EnumPropertyOutput,
Expand Down Expand Up @@ -86,6 +88,28 @@ export interface FloatPut204Response extends HttpResponse {
status: "204";
}

/** The request has succeeded. */
export interface DecimalGet200Response extends HttpResponse {
status: "200";
body: DecimalPropertyOutput;
}

/** There is no content to send for this request, but the headers may be useful. */
export interface DecimalPut204Response extends HttpResponse {
status: "204";
}

/** The request has succeeded. */
export interface Decimal128Get200Response extends HttpResponse {
status: "200";
body: Decimal128PropertyOutput;
}

/** There is no content to send for this request, but the headers may be useful. */
export interface Decimal128Put204Response extends HttpResponse {
status: "204";
}

/** The request has succeeded. */
export interface DatetimeGet200Response extends HttpResponse {
status: "200";
Expand Down
Loading
Loading