-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Value world, object, tuple literals, const (#3022)
resolves #2046 [Playround](https://cadlplayground.z22.web.core.windows.net/prs/3022/) Add the new syntax for object literals using `#{`. For this first version an object literal can only contain other object literal and other literals(string, number, boolean)) ## Values axioms 1. `alias` always produces a type. If you attempt to alias a value, you get an error. 2. A string template produces a string template type if all substitutions are types, and a value if all substitutions are numeric, boolean, or string values. A mixture of types and values is an error. 3. The string literal syntax always results in a string literal type 4. A string literal type may be passed as a string value when the signature expects a value. When the signature expects either a string literal type or a string value, it is passed as a string value. 5. A string template type can be passed as a string value when all its substitutions are string literal types. ## Breaking change ### Removal of the `ValueType` replacement with `MixedConstraint` This shouldn't affect anyone as you were only exposed to this if you digged into the template parameter and looked at the constraint ## Deprecation ## Using a tuple instead of a tuple literal - ✅ still work - emit a warning <img width="1013" alt="image" src="https://github.com/microsoft/typespec/assets/1031227/ab05359a-5ed9-4a27-a8d1-f40d1e21766f"> - provide a codefix <img width="312" alt="image" src="https://github.com/microsoft/typespec/assets/1031227/5ef93bdf-665f-4445-a6b2-62475efe8c16"> ## Using a model expression instead of an object literal This technically didn't work before(different from above where tuple was used as a value) but allow this will allow us to convert most of our decorators to use `valueof` without being breaking ![Kapture 2024-03-18 at 19 31 32](https://github.com/microsoft/typespec/assets/1031227/f6d69ab4-139e-4b01-95a3-f376b8515d1c) ## Old decorator marshalling If a library had a decorator with `valueof` one of those types `numeric`, `int64`, `uint64`, `integer`, `float`, `decimal`, `decimal128`, `null` it used to marshall those as JS `number` and `NullType` for `null`. With the introduction of values we have a new marshalling logic which will marshall those numeric types as `Numeric` and the others will remain numbers. `null` will also get marshalled as `null`. For now this is an opt-in behavior with a warning on decorators not opt-in having a parameter with a constraint from the list above. Example: ``` extern dec multipleOf(target: numeric | Reflection.ModelProperty, value: valueof numeric); ``` Will now emit a deprecated warning because `value` is of type `valueof string` which would marshall to `Numeric` under the new logic but as `number` previously. To opt-in you can add the following to your library ```ts export const $flags = defineModuleFlags({ decoratorArgMarshalling: "value", }); ``` --------- Co-authored-by: Brian Terlson <brian.terlson@microsoft.com> Co-authored-by: Mark Cowlishaw <markcowl@microsoft.com>
- Loading branch information
1 parent
dec5043
commit 7ec1716
Showing
122 changed files
with
8,055 additions
and
1,237 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
.chronus/changes/feature-object-literals-2024-2-15-18-36-3-1.md
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking | ||
changeKind: deprecation | ||
packages: | ||
- "@typespec/compiler" | ||
--- | ||
|
||
Using a tuple type as a value is deprecated. Tuple types in contexts where values are expected must be updated to be array values instead. A codefix is provided to automatically convert tuple types into array values. | ||
|
||
```tsp | ||
model Test { | ||
// Deprecated | ||
values: string[] = ["a", "b", "c"]; | ||
// Correct | ||
values: string[] = #["a", "b", "c"]; | ||
``` |
17 changes: 17 additions & 0 deletions
17
.chronus/changes/feature-object-literals-2024-2-15-18-36-3-2.md
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking | ||
changeKind: deprecation | ||
packages: | ||
- "@typespec/compiler" | ||
--- | ||
|
||
Using a model type as a value is deprecated. Model types in contexts where values are expected must be updated to be object values instead. A codefix is provided to automatically convert model types into object values. | ||
|
||
```tsp | ||
model Test { | ||
// Deprecated | ||
user: {name: string} = {name: "System"}; | ||
// Correct | ||
user: {name: string} = #{name: "System"}; | ||
``` |
30 changes: 30 additions & 0 deletions
30
.chronus/changes/feature-object-literals-2024-2-15-18-36-3.md
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking | ||
changeKind: feature | ||
packages: | ||
- "@typespec/compiler" | ||
--- | ||
|
||
Add syntax for declaring values. [See docs](https://typespec.io/docs/language-basics/values). | ||
|
||
Object and array values | ||
```tsp | ||
@dummy(#{ | ||
name: "John", | ||
age: 48, | ||
address: #{ city: "London" } | ||
aliases: #["Bob", "Frank"] | ||
}) | ||
``` | ||
|
||
Scalar constructors | ||
|
||
```tsp | ||
scalar utcDateTime { | ||
init fromISO(value: string); | ||
} | ||
model DateRange { | ||
minDate: utcDateTime = utcDateTime.fromISO("2024-02-15T18:36:03Z"); | ||
} | ||
``` |
10 changes: 10 additions & 0 deletions
10
.chronus/changes/feature-object-literals-2024-2-18-22-23-26.md
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking | ||
changeKind: fix | ||
packages: | ||
- "@typespec/json-schema" | ||
- "@typespec/protobuf" | ||
- "@typespec/versioning" | ||
--- | ||
|
||
Update to support new value types |
23 changes: 23 additions & 0 deletions
23
.chronus/changes/feature-object-literals-2024-3-16-10-38-3.md
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
changeKind: deprecation | ||
packages: | ||
- "@typespec/compiler" | ||
--- | ||
|
||
Decorator API: Legacy marshalling logic | ||
|
||
With the introduction of values, the decorator marshalling behavior has changed in some cases. This behavior is opt-in by setting the `valueMarshalling` package flag to `"new"`, but will be the default behavior in future versions. It is strongly recommended to adopt this new behavior as soon as possible. | ||
|
||
|
||
Example: | ||
```tsp | ||
extern dec multipleOf(target: numeric | Reflection.ModelProperty, value: valueof numeric); | ||
``` | ||
Will now emit a deprecated warning because `value` is of type `valueof string` which would marshall to `Numeric` under the new logic but as `number` previously. | ||
|
||
To opt-in you can add the following to your library js/ts files. | ||
```ts | ||
export const $flags = definePackageFlags({ | ||
decoratorArgMarshalling: "new", | ||
}); | ||
``` |
7 changes: 7 additions & 0 deletions
7
.chronus/changes/feature-object-literals-2024-3-16-11-58-32.md
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
changeKind: feature | ||
packages: | ||
- "@typespec/openapi3" | ||
--- | ||
|
||
Add support for new object and array values as default values (e.g. `decimals: decimal[] = #[123, 456.7];`) |
7 changes: 7 additions & 0 deletions
7
.chronus/changes/feature-object-literals-2024-3-16-12-0-15.md
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
changeKind: fix | ||
packages: | ||
- "@typespec/rest" | ||
--- | ||
|
||
Update types to support new values in TypeSpec |
8 changes: 8 additions & 0 deletions
8
.chronus/changes/feature-object-literals-2024-3-16-17-54-23.md
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking | ||
changeKind: feature | ||
packages: | ||
- "@typespec/html-program-viewer" | ||
--- | ||
|
||
Add support for values |
9 changes: 9 additions & 0 deletions
9
.chronus/changes/feature-object-literals-32024-2-15-18-36-3.md
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking | ||
changeKind: fix | ||
packages: | ||
- "@typespec/http" | ||
--- | ||
|
||
Update Flow Template to make use of the new array values | ||
|
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
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
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
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
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.