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

[tcgc] support int as string encoding #1258

Merged
merged 9 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions .chronus/changes/allow_int_string-2024-6-29-16-19-19.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

add support for encoding an int as a string
122 changes: 122 additions & 0 deletions docs/howtos/DataPlane Generation - DPG/06types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2251,3 +2251,125 @@ public enum Orientation {

</TabItem>
</Tabs>

## Scalars

### Encoding

We will take the `@encode` decorator into account, determining how we serialize inputted scalars to send over the wire.

<Tabs>
<TabItem value="typespec" label="TypeSpec" default>

model Test {
@encode(DateTimeKnownEncoding.rfc3339)
prop: utcDateTime;
}

</TabItem>
<TabItem value="tcgc" label="TCGC">

```json
{
"kind": "property",
"name": "prop",
"type": {
"kind": "utcDateTime",
"encode": "rfc3339",
"wireType": {
"kind": "string"
}
}
}
```

</TabItem>
<TabItem value="python" label="Python">

```python
serialized_prop = json.dumps(prop, cls=SdkJSONEncoder, format="rfc3339")
```

</TabItem>
<TabItem value="csharp" label="CSharp" >

```csharp
TODO
```

</TabItem>
<TabItem value="typescript" label="Typescript" >

```typescript
TODO;
```

</TabItem>
<TabItem value="java" label="Java" >

```java
// Internal implementation
jsonWriter.writeStringField("prop",
this.value == null ? null : DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(this.value));
```

</TabItem>
</Tabs>

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.

<Tabs>
<TabItem value="typespec" label="TypeSpec" default>

model Test {
@encode(string)
prop: int64;
}

</TabItem>
<TabItem value="tcgc" label="TCGC">

```json
{
"kind": "property",
"name": "prop",
"type": {
"kind": "int64",
"encode": "string",
"wireType": {
"kind": "string"
}
}
}
```

</TabItem>
<TabItem value="python" label="Python">

```python
serialized_prop = json.dumps(prop, cls=SdkJSONEncoder, format="string")
```

</TabItem>
<TabItem value="csharp" label="CSharp" >

```csharp
TODO
```

</TabItem>
<TabItem value="typescript" label="Typescript" >

```typescript
TODO;
```

</TabItem>
<TabItem value="java" label="Java" >

```java
TODO
```

</TabItem>
</Tabs>
10 changes: 9 additions & 1 deletion packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
iscai-msft marked this conversation as resolved.
Show resolved Hide resolved
}
return kind;
}
Expand Down
Loading