-
Notifications
You must be signed in to change notification settings - Fork 51
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
Update the client generation document to provide valid typespec samples #2237
Open
lirenhe
wants to merge
5
commits into
main
Choose a base branch
from
doc-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dcbd9c0
Update the client generation document to provide a validate typespec
lirenhe 297c51b
Merge branch 'main' into doc-fix
lirenhe 512afe5
Merge branch 'main' into doc-fix
lirenhe 5ea4802
Merge branch 'main' into doc-fix
lirenhe 5dcc4b5
Merge branch 'main' into doc-fix
lirenhe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -703,14 +703,6 @@ public final class Animal implements JsonSerializable<Animal> { | |
<ClientTabs> | ||
<ClientTabItem lang="typespec" > | ||
|
||
```typespec | ||
model Animal { | ||
name: string; | ||
kind: string; | ||
...Record<string | int32>; | ||
} | ||
``` | ||
|
||
```typespec | ||
model Animal { | ||
name: string; | ||
|
@@ -963,7 +955,7 @@ TypeSpec uses `@discriminator` decorator to add a discriminator to a model. | |
<ClientTabs> | ||
<ClientTabItem lang="typespec" > | ||
|
||
TypeSpec now has two ways to represent a discriminated set. | ||
Client emitters now only support a single way to represent a discriminated set in TypeSpec. | ||
|
||
1. Use model | ||
|
||
|
@@ -1005,20 +997,6 @@ model Ragdoll extends Cat { | |
} | ||
``` | ||
|
||
2. Use union | ||
|
||
```typespec | ||
@discriminator("kind") | ||
union Cat { | ||
Siamese, | ||
Ragdoll, | ||
} | ||
|
||
model Siamese {} | ||
|
||
model Ragdoll {} | ||
``` | ||
|
||
</ClientTabItem> | ||
<ClientTabItem lang="tcgc"> | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the next line could be removed. |
||
|
@@ -1250,18 +1228,13 @@ about nullability by inspecting the type of a property. | |
model Foo { | ||
basicNullableProperty: string | null; | ||
modelNullableProperty: Bar | null; | ||
unionNullableProperty: Bar | Baz | null; | ||
enumNullableProperty: LR | null; | ||
} | ||
|
||
model Bar { | ||
prop: string; | ||
} | ||
|
||
model Baz { | ||
prop: int32; | ||
} | ||
|
||
enum LR { | ||
left, | ||
right, | ||
|
@@ -1316,52 +1289,6 @@ A nullable type has kind `nullable` and property `valueType`. The kind of the ty | |
} | ||
} | ||
}, | ||
{ | ||
"kind": "property", | ||
"name": "unionNullableProperty", | ||
"serializedName": "unionNullableProperty", | ||
"optional": false, | ||
"type": { | ||
"kind": "nullable", | ||
"valueType": { | ||
"kind": "union", | ||
"values": [ | ||
{ | ||
"kind": "model", | ||
"name": "Bar", | ||
"properties": [ | ||
{ | ||
"kind": "property", | ||
"name": "prop", | ||
"serializedName": "prop", | ||
"optional": false, | ||
"type": { | ||
"kind": "string", | ||
"encode": "string" | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
"kind": "model", | ||
"name": "Baz", | ||
"properties": [ | ||
{ | ||
"kind": "property", | ||
"name": "prop", | ||
"serializedName": "prop", | ||
"optional": false, | ||
"type": { | ||
"kind": "int32", | ||
"encode": "int32" | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
{ | ||
"kind": "property", | ||
"name": "enumNullableProperty", | ||
|
@@ -1409,17 +1336,13 @@ from corehttp.utils import CaseInsensitiveEnumMeta | |
class Bar(_model_base.Model): | ||
prop: Optional[str] = rest_field() | ||
|
||
class Baz(_model_base.Model): | ||
prop: Optional[str] = rest_field() | ||
|
||
class LR(str, Enum, metaclass=CaseInsensitiveEnumMeta): | ||
LEFT = "left" | ||
RIGHT = "right" | ||
|
||
class Foo(_model_base.Model): | ||
basicNullableProperty: Optional[str] = rest_field() | ||
modelNullableProperty: Optional["_models.Bar"] = rest_field() | ||
unionNullableProperty: Optional[Union["_models.Bar", "_models.Baz"]] = rest_field() | ||
enumNullableProperty: Optional["LR"] = rest_field() | ||
|
||
``` | ||
|
@@ -2314,108 +2237,6 @@ public enum WidgetOrientation { | |
</ClientTabItem> | ||
</ClientTabs> | ||
|
||
### Union with multiple types | ||
|
||
These are unions where the values don't share same type. | ||
|
||
<ClientTabs> | ||
<ClientTabItem lang="typespec" > | ||
|
||
```typespec | ||
model Shirt { | ||
sizing: 32 | 34 | int32 | "small" | "medium" | string; | ||
} | ||
``` | ||
|
||
</ClientTabItem> | ||
<ClientTabItem lang="tcgc"> | ||
|
||
```json | ||
{ | ||
"kind": "union", | ||
"name": "ShirtSizings", | ||
"generatedName": true, | ||
"values": [ | ||
{ | ||
"kind": "constant", | ||
"value": 32, | ||
"valueType": { | ||
"kind": "int32" | ||
} | ||
}, | ||
{ | ||
"kind": "constant", | ||
"value": 34, | ||
"valueType": { | ||
"kind": "int32" | ||
} | ||
}, | ||
{ | ||
"kind": "constant", | ||
"value": "small", | ||
"valueType": { | ||
"kind": "string" | ||
} | ||
}, | ||
{ | ||
"kind": "constant", | ||
"value": "medium", | ||
"valueType": { | ||
"kind": "string" | ||
} | ||
}, | ||
{ | ||
"kind": "string" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
</ClientTabItem> | ||
<ClientTabItem lang="python"> | ||
|
||
Python will generate this as a union since these entries don't share the same type | ||
|
||
```python | ||
from typing import Literal | ||
|
||
type ShirtSizing = Literal[32] | Literal[34] | int | Literal["small"] | Literal["medium"] | str | ||
|
||
model Shirt: | ||
sizing: ShirtSizing | ||
``` | ||
|
||
</ClientTabItem> | ||
<ClientTabItem lang="csharp" > | ||
|
||
```csharp | ||
public partial class Shirt | ||
{ | ||
public BinaryData Shirt; | ||
} | ||
``` | ||
|
||
</ClientTabItem> | ||
<ClientTabItem lang="typescript" > | ||
|
||
```typescript | ||
export interface Shirt { | ||
sizing: 32 | 34 | number | "small" | "medium" | string; | ||
} | ||
``` | ||
|
||
</ClientTabItem> | ||
<ClientTabItem lang="java" > | ||
|
||
```java | ||
public final class Shirt { | ||
private BinaryData sizing; | ||
} | ||
``` | ||
|
||
</ClientTabItem> | ||
</ClientTabs> | ||
|
||
## Enums | ||
|
||
### Standard | ||
|
@@ -2706,7 +2527,7 @@ We will take the `@encode` decorator into account, determining how we serialize | |
<ClientTabs> | ||
<ClientTabItem lang="typespec" > | ||
|
||
```tsp | ||
```typespec | ||
model Test { | ||
@encode(DateTimeKnownEncoding.rfc3339) | ||
prop: utcDateTime; | ||
|
@@ -2769,7 +2590,7 @@ When you specify an encoding type, say that you want to encode an integer as a s | |
<ClientTabs> | ||
<ClientTabItem lang="typespec" > | ||
|
||
```tsp | ||
```typespec | ||
model Test { | ||
@encode(string) | ||
prop: int64; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we do not support union type, we could also remove this section.