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

Value world, object, tuple literals, const #3022

Merged
merged 210 commits into from
May 8, 2024

Conversation

timotheeguerin
Copy link
Member

@timotheeguerin timotheeguerin commented Mar 14, 2024

resolves #2046 Playround

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
image
  • provide a codefix
image

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

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

export const $flags = defineModuleFlags({
  decoratorArgMarshalling: "value",
});

@azure-sdk
Copy link
Collaborator

azure-sdk commented Mar 14, 2024

All changed packages have been documented.

  • @typespec/compiler
  • @typespec/html-program-viewer
  • @typespec/http
  • @typespec/json-schema
  • @typespec/openapi3
  • @typespec/protobuf
  • @typespec/rest
  • @typespec/versioning
Show changes

@typespec/compiler - deprecation ✏️

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"];,>

@typespec/compiler - deprecation ✏️

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"};,>

@typespec/compiler - feature ✏️

Add syntax for declaring values. See docs.,> ,> 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");,> },>

@typespec/json-schema - fix ✏️

Update to support new value types

@typespec/protobuf - fix ✏️

Update to support new value types

@typespec/versioning - fix ✏️

Update to support new value types

@typespec/compiler - deprecation ✏️

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",,> });,>

@typespec/openapi3 - feature ✏️

Add support for new object and array values as default values (e.g. decimals: decimal[] = #[123, 456.7];)

@typespec/rest - fix ✏️

Update types to support new values in TypeSpec

@typespec/html-program-viewer - feature ✏️

Add support for values

@typespec/http - fix ✏️

Update Flow Template to make use of the new array values

@azure-sdk
Copy link
Collaborator

You can try these changes at https://cadlplayground.z22.web.core.windows.net/prs/3022/

Check the website changes at https://tspwebsitepr.z22.web.core.windows.net/prs/3022/

@timotheeguerin timotheeguerin marked this pull request as ready for review March 15, 2024 21:08
timotheeguerin and others added 2 commits May 6, 2024 13:56
Co-authored-by: Brian Terlson <brian.terlson@microsoft.com>
Co-authored-by: Brian Terlson <brian.terlson@microsoft.com>
Copy link
Member

@bterlson bterlson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still need to review checker.

packages/compiler/lib/intrinsics.tsp Outdated Show resolved Hide resolved
packages/compiler/lib/intrinsics.tsp Outdated Show resolved Hide resolved
packages/compiler/lib/intrinsics.tsp Show resolved Hide resolved
packages/compiler/src/core/messages.ts Outdated Show resolved Hide resolved
packages/compiler/src/core/types.ts Outdated Show resolved Hide resolved
packages/compiler/src/core/types.ts Show resolved Hide resolved
packages/compiler/src/core/types.ts Outdated Show resolved Hide resolved
packages/compiler/src/core/types.ts Outdated Show resolved Hide resolved
packages/json-schema/lib/main.tsp Outdated Show resolved Hide resolved
packages/compiler/src/core/checker.ts Outdated Show resolved Hide resolved
packages/compiler/src/core/checker.ts Outdated Show resolved Hide resolved
packages/compiler/src/core/types.ts Outdated Show resolved Hide resolved
timotheeguerin and others added 2 commits May 7, 2024 12:03
Co-authored-by: Brian Terlson <brian.terlson@microsoft.com>
Co-authored-by: Brian Terlson <brian.terlson@microsoft.com>
@timotheeguerin timotheeguerin mentioned this pull request May 8, 2024
4 tasks
@timotheeguerin timotheeguerin added this pull request to the merge queue May 8, 2024
Merged via the queue into microsoft:main with commit 7ec1716 May 8, 2024
21 checks passed
@timotheeguerin timotheeguerin deleted the feature/object-literals branch May 8, 2024 20:24
args: readonly Type[];
/** @internal */ map: Map<TemplateParameter, Type>;
getMappedType(type: TemplateParameter): Type | Value | IndeterminateEntity;
args: readonly (Type | Value | IndeterminateEntity)[];
Copy link
Member

@MaryGao MaryGao May 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

widen the union in output is a breaking, in this case that union type is being used by us, it's an output, therefore it's a breaking. as inspired by the point 6 of @xirzec 's summary of what should be considered as breaking Azure/azure-sdk-for-js#22983 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[TypeSpec Language] object, tuple values and the value world
6 participants