From 93c0cf05a125a15efd244c8d200443a642b8c8dd Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Mon, 29 Jul 2024 15:53:36 -0400 Subject: [PATCH 1/6] add check for encoding type --- packages/typespec-client-generator-core/src/types.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/typespec-client-generator-core/src/types.ts b/packages/typespec-client-generator-core/src/types.ts index ee2ae7ea04..6947854f96 100644 --- a/packages/typespec-client-generator-core/src/types.ts +++ b/packages/typespec-client-generator-core/src/types.ts @@ -136,7 +136,15 @@ function getAnyType(context: TCGCContext, type: Type): [SdkBuiltInType, readonly function getEncodeHelper(context: TCGCContext, type: Type, kind: string): string { if (type.kind === "ModelProperty" || type.kind === "Scalar") { - return getEncode(context.program, type)?.encoding || kind; + const encode = getEncode(context.program, type); + if (encode?.encoding) { + return encode.encoding; + } + if (encode?.type) { + // if we specify the encoding type in the decorator, we set the `.encode` string + // to the kind of the encoding type + return getSdkBuiltInType(context, encode.type).kind; + } } return kind; } From 54a1e3b1f7822f2f1d787b3217600778316efe7a Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Mon, 29 Jul 2024 16:13:21 -0400 Subject: [PATCH 2/6] add docs --- .../DataPlane Generation - DPG/06types.mdx | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/docs/howtos/DataPlane Generation - DPG/06types.mdx b/docs/howtos/DataPlane Generation - DPG/06types.mdx index 9fdced5ecd..297b9e45da 100644 --- a/docs/howtos/DataPlane Generation - DPG/06types.mdx +++ b/docs/howtos/DataPlane Generation - DPG/06types.mdx @@ -2251,3 +2251,124 @@ public enum Orientation { + +## Scalars + +### Encoding + +We will take the `@encode` decorator into account, determining how we serialize inputted scalars to send over the wire. + + + + +model Test { + @encode(DateTimeKnownEncoding.rfc3339) + prop: utcDateTime; +} + + + + +```json +{ + "kind": "property", + "name": "prop", + "type": { + "kind": "utcDateTime", + "encode": "rfc3339", + "wireType": { + "kind": "string" + }, + } +} +``` + + + + +```python +serialized_prop = json.dumps(prop, cls=SdkJSONEncoder, format="rfc3339") +``` + + + + +```csharp +TODO +``` + + + + +```typescript +TODO +``` + + + + +```java +TODO +``` + + + + +When you specify an encoding type, say that you want to encode an integer as a string, that will also be represented in our generated SDKs. + + + + +model Test { + @encode(string) + prop: int64; +} + + + + +```json +{ + "kind": "property", + "name": "prop", + "type": { + "kind": "int64", + "encode": "string", + "wireType": { + "kind": "string", + } + } +} +``` + + + + +```python +serialized_prop = json.dumps(prop, cls=SdkJSONEncoder, format="string") +``` + + + + +```csharp +TODO +``` + + + + +```typescript +TODO +``` + + + + +```java +TODO +``` + + + + From 78bacd49f86d9fe6a57e4d4601a87f9f9e1efaa2 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Mon, 29 Jul 2024 16:18:15 -0400 Subject: [PATCH 3/6] format --- .../DataPlane Generation - DPG/06types.mdx | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/docs/howtos/DataPlane Generation - DPG/06types.mdx b/docs/howtos/DataPlane Generation - DPG/06types.mdx index 297b9e45da..a1aa641da1 100644 --- a/docs/howtos/DataPlane Generation - DPG/06types.mdx +++ b/docs/howtos/DataPlane Generation - DPG/06types.mdx @@ -2262,8 +2262,8 @@ We will take the `@encode` decorator into account, determining how we serialize model Test { - @encode(DateTimeKnownEncoding.rfc3339) - prop: utcDateTime; +@encode(DateTimeKnownEncoding.rfc3339) +prop: utcDateTime; } @@ -2278,7 +2278,7 @@ model Test { "encode": "rfc3339", "wireType": { "kind": "string" - }, + } } } ``` @@ -2301,7 +2301,7 @@ TODO ```typescript -TODO +TODO; ``` @@ -2320,8 +2320,8 @@ When you specify an encoding type, say that you want to encode an integer as a s model Test { - @encode(string) - prop: int64; +@encode(string) +prop: int64; } @@ -2335,7 +2335,7 @@ model Test { "kind": "int64", "encode": "string", "wireType": { - "kind": "string", + "kind": "string" } } } @@ -2359,7 +2359,7 @@ TODO ```typescript -TODO +TODO; ``` @@ -2371,4 +2371,3 @@ TODO - From 854b6b21c598c78d9ff4cbb99a9352ca01080bdb Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Mon, 29 Jul 2024 16:19:25 -0400 Subject: [PATCH 4/6] add changeset --- .chronus/changes/allow_int_string-2024-6-29-16-19-19.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .chronus/changes/allow_int_string-2024-6-29-16-19-19.md diff --git a/.chronus/changes/allow_int_string-2024-6-29-16-19-19.md b/.chronus/changes/allow_int_string-2024-6-29-16-19-19.md new file mode 100644 index 0000000000..cb1c21e2af --- /dev/null +++ b/.chronus/changes/allow_int_string-2024-6-29-16-19-19.md @@ -0,0 +1,7 @@ +--- +changeKind: feature +packages: + - "@azure-tools/typespec-client-generator-core" +--- + +add support for encoding ints as strings \ No newline at end of file From d42ecef1873f1d6e155f0f76904f02402a50463a Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Tue, 30 Jul 2024 09:29:34 +0800 Subject: [PATCH 5/6] Update 06types.mdx --- docs/howtos/DataPlane Generation - DPG/06types.mdx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/howtos/DataPlane Generation - DPG/06types.mdx b/docs/howtos/DataPlane Generation - DPG/06types.mdx index a1aa641da1..b9fe65644c 100644 --- a/docs/howtos/DataPlane Generation - DPG/06types.mdx +++ b/docs/howtos/DataPlane Generation - DPG/06types.mdx @@ -2308,7 +2308,9 @@ TODO; ```java -TODO +// Internal implementation +jsonWriter.writeStringField("prop", + this.value == null ? null : DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(this.value)); ``` From a21862c84b136d8a6951b41121881b1ea7267a67 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Tue, 30 Jul 2024 11:59:02 -0400 Subject: [PATCH 6/6] fix cspell --- .chronus/changes/allow_int_string-2024-6-29-16-19-19.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chronus/changes/allow_int_string-2024-6-29-16-19-19.md b/.chronus/changes/allow_int_string-2024-6-29-16-19-19.md index cb1c21e2af..487dbf5eee 100644 --- a/.chronus/changes/allow_int_string-2024-6-29-16-19-19.md +++ b/.chronus/changes/allow_int_string-2024-6-29-16-19-19.md @@ -4,4 +4,4 @@ packages: - "@azure-tools/typespec-client-generator-core" --- -add support for encoding ints as strings \ No newline at end of file +add support for encoding an int as a string