Skip to content

Commit

Permalink
have separate datetime types (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
iscai-msft authored Feb 23, 2024
1 parent a61b351 commit 315ab0c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/utc_info-2024-1-22-17-11-48.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: breaking
packages:
- "@azure-tools/typespec-client-generator-core"
---

Split datetime type into utcDateTime and offsetDateTime to remain in sync with tsp
13 changes: 11 additions & 2 deletions packages/typespec-client-generator-core/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,21 @@ export function isSdkDatetimeEncodings(encoding: string): encoding is DateTimeKn
return SdkDatetimeEncodingsConst.includes(encoding as DateTimeKnownEncoding);
}

export interface SdkDatetimeType extends SdkTypeBase {
kind: "datetime";
interface SdkDatetimeTypeBase extends SdkTypeBase {
encode: DateTimeKnownEncoding;
wireType: SdkBuiltInType;
}

interface SdkUtcDatetimeType extends SdkDatetimeTypeBase {
kind: "utcDateTime";
}

interface SdkOffsetDatetimeType extends SdkDatetimeTypeBase {
kind: "offsetDateTime";
}

export type SdkDatetimeType = SdkUtcDatetimeType | SdkOffsetDatetimeType;

export interface SdkDurationType extends SdkTypeBase {
kind: "duration";
encode: DurationKnownEncoding;
Expand Down
19 changes: 6 additions & 13 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ function addEncodeInfo(
getClientType(context, encodeData.type)
) as SdkBuiltInType;
}
if (propertyType.kind === "datetime") {
if (propertyType.kind === "utcDateTime" || propertyType.kind === "offsetDateTime") {
if (encodeData) {
propertyType.encode = encodeData.encoding as DateTimeKnownEncoding;
propertyType.wireType = diagnostics.pipe(
Expand Down Expand Up @@ -254,17 +254,6 @@ export function getSdkBuiltInType(
throw Error(`Unknown kind ${type.kind}`);
}

export function getSdkDatetimeType(context: SdkContext, type: Scalar): SdkDatetimeType {
// we don't get encode info until we get to the property / parameter level
// so we insert the default. Later in properties, we will check
// for encoding info and override accordingly
return {
...getSdkTypeBaseHelper(context, type, "datetime"),
encode: "rfc3339",
wireType: { ...getSdkTypeBaseHelper(context, type, "string"), encode: "string" },
};
}

export function getSdkDurationType(context: SdkContext, type: Scalar): SdkDurationType {
// we don't get encode info until we get to the property / parameter level
// so we insert the default. Later in properties, we will check
Expand Down Expand Up @@ -706,7 +695,11 @@ export function getClientType(
break;
}
if (type.name === "utcDateTime" || type.name === "offsetDateTime") {
retval = getSdkDatetimeType(context, type);
retval = {
...getSdkTypeBaseHelper(context, type, type.name),
encode: "rfc3339",
wireType: { ...getSdkTypeBaseHelper(context, type, "string"), encode: "string" },
} as SdkDatetimeType;
break;
}
if (type.name === "duration") {
Expand Down
10 changes: 5 additions & 5 deletions packages/typespec-client-generator-core/test/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ describe("typespec-client-generator-core: types", () => {
`
);
const sdkType = getSdkTypeHelper(runner);
strictEqual(sdkType.kind, "datetime");
strictEqual(sdkType.kind, "utcDateTime");
strictEqual(sdkType.wireType.kind, "string");
strictEqual(sdkType.encode, "rfc3339");
});
Expand All @@ -377,7 +377,7 @@ describe("typespec-client-generator-core: types", () => {
`
);
const sdkType = getSdkTypeHelper(runner);
strictEqual(sdkType.kind, "datetime");
strictEqual(sdkType.kind, "utcDateTime");
strictEqual(sdkType.wireType.kind, "string");
strictEqual(sdkType.encode, "rfc3339");
});
Expand All @@ -393,7 +393,7 @@ describe("typespec-client-generator-core: types", () => {
`
);
const sdkType = getSdkTypeHelper(runner);
strictEqual(sdkType.kind, "datetime");
strictEqual(sdkType.kind, "utcDateTime");
strictEqual(sdkType.wireType.kind, "string");
strictEqual(sdkType.encode, "rfc7231");
});
Expand All @@ -410,7 +410,7 @@ describe("typespec-client-generator-core: types", () => {
`
);
const sdkType = getSdkTypeHelper(runner);
strictEqual(sdkType.kind, "datetime");
strictEqual(sdkType.kind, "utcDateTime");
strictEqual(sdkType.wireType.kind, "int64");
strictEqual(sdkType.encode, "unixTimestamp");
});
Expand All @@ -430,7 +430,7 @@ describe("typespec-client-generator-core: types", () => {
);
const sdkType = getSdkTypeHelper(runner);
strictEqual(sdkType.kind, "array");
strictEqual(sdkType.valueType.kind, "datetime");
strictEqual(sdkType.valueType.kind, "utcDateTime");
strictEqual(sdkType.valueType.wireType.kind, "int64");
strictEqual(sdkType.valueType.encode, "unixTimestamp");
});
Expand Down

0 comments on commit 315ab0c

Please sign in to comment.