Граф коммитов

7 Коммитов

Автор SHA1 Сообщение Дата
Michelle dda9db6f08
Update emitter docs to include configuration examples (#4112)
For a TypeSpec beginner, it may be difficult to understand how to
configure or format tspconfig.yaml to tweak the different options
available. Adding a basic example should help folks get started.
2024-08-08 13:48:26 +00:00
Christopher Radek 72d4351207
Updates extension decorator to support value kinds and adds setExtension API to json-schema emitter (#3558)
Related to #3336

## Summary

This PR introduces 2 changes:

1. adds a new `setExtension` API on the `@typespec/json-schema` emitter
to make it easier to author custom extension decorators.
2. updates the `@extension` decorator to support taking values to
obviate the need for the `Json<Data>` model.

Example usage of writing a custom extension `@added` decorator:

```js
export function $added(context, target, value) {
  setExtension(context.program, target, "x-added", value);
}
```

Example usage of passing in value literals to `@extension` decorator:

```tsp
@extension("x-current", Json<["foo"]>)
@extension("x-new", #["foo"])
model Foo {}
```

## Background

The `@typespec/json-schema` package exports an `@extension` decorator
that allows customers to specify additional properties to be present as
part of their emitted JSON schema.

For example, we can add a property - converted to a schema - that
indicates when a field was added to the model:

```tsp
@jsonSchema
model Pet {
  @extension("x-added", "2024-01-01")
  name: string;
}
```

The decorator also supports emitting the property's value as raw code by
wrapping the parameter in the `Json<Data>` template:

```tsp
@jsonSchema
model Pet {
  @extension("x-added", Json<"2024-01-01">)
  name: string;
}
```

### Comparison

Below is a diff between the two ways of calling the decorator for
clarity:

```diff
$schema: https://json-schema.org/draft/2020-12/schema
$id: Pet.yaml
type: object
properties:
  name:
    type: string
-   x-added:
-     type: string
-     const: 2024-01-01
+   x-added: 2024-01-01
required:
  - name
```

## Pain Points

Currently, `@extension` only supports raw values by wrapping them with
the `TypeSpec.JsonSchema.Json` template. Since this type isn't available
in JS code, this forces authors to manually create the wrapped type
themselves, e.g.:

```js
/**
 * @param {import("@typespec/compiler").DecoratorContext} context
 * @param {import("@typespec/compiler").Type} target
 * @param {import("@typespec/compiler").StringLiteral} value
 */
export function $added(context, target, value) {
  $extension(context, target, "x-added", {
    kind: "Model",
    name: "Json",
    namespace: { name: "JsonSchema" },
    properties: createRekeyableMap([["value", { type: value }]]),
  });
}
```

## Update `@extension` to accept values directly and expose
`setExtension` API

This PR exposes a new `setExtension` API that accepts types and values
as input, instead of just `Type`. This also means
`ExtensionRecord.value` has been loosened:

```diff
export interface ExtensionRecord {
  key: string;
- value: Type;
+ value: Type | unknown;
}
```

In addition, the `@extension` decorator is updated to support accepting
values directly:

```diff
- extern dec extension(target: unknown, key: valueof string, value: unknown);
+ extern dec extension(target: unknown, key: valueof string, value: unknown | (valueof unknown));
```

While this change introduces breaking changes to the `@extensions`
decorator and `getExtensions` function, it also allows the `@extension`
decorator to support [value
kinds](https://typespec.io/docs/language-basics/values) as input - a
feature language feature introduced in `0.57.0`.

This would not cause additional breaking changes (beyond
`getExtensions`) for users that already use the
`TypeSpec.JsonSchema.Json` template to pass in raw values. However,
there is 1 scenario that would now fail:

1. Scalar literals/null would now be treated as values instead of types.
(e.g. `@extension("x-bool-literal", true)` would need to be updated to
`@extension("x-bool-literal", typeof true)` to preserve current
behavior)

## Alternatives

### Solution - only expose `setExtension` API

This PR exposes a new `setExtension` API that accepts types and values
as input, instead of just `Type`. This also means
`ExtensionRecord.value` has been loosened:

```diff
export interface ExtensionRecord {
  key: string;
- value: Type;
+ value: Type | any;
}
```

This does introduce a **breaking change** in the **getExtensions** API
since `ExtensionRecord.value` is no longer guaranteed to be `Type`.
However, it is likely this API is not used much if at all outside of
this emitter.

### Create new `setExtensionValue`/`getExtensionValues` functions and
alternative `@extension` decorator

This change has the benefit of not requiring any breaking changes.
However, we would now have 2 different but very similar APIs for
settings/getting extensions that may lead to confusion, as well as 2
very similar extensions to do essentially the same thing.

---------

Co-authored-by: Christopher Radek <Christopher.Radek@microsoft.com>
2024-08-05 20:15:57 +00:00
Brian Terlson 295e68a698
Add oneOf to JSON Schema (#3557)
Fix #3544.
2024-06-11 17:11:53 +00:00
Timothee Guerin 02ed01e79b
Improved ref docs for model (#2951)
fix https://github.com/Azure/typespec-azure-pr/issues/3861
fix #2232
Generate docs for model properties and reference models within the same
package
<img width="1213" alt="image"
src="https://github.com/microsoft/typespec/assets/1031227/ae7eccd0-97d8-401e-b07f-3375c7b41446">
2024-02-29 07:21:13 -08:00
Will Temple aae7166f0a
Rename template parameters for clarity and consistency (#2726)
This is in preparation for merging named template argument
instantiation.

---------

Co-authored-by: Will Temple <will@wtemple.net>
2023-12-18 13:44:37 -05:00
Timothee Guerin 7cce379d3e
Render README from refdoc (#2242)
fix #2130 

Generate the README.md for typespec libraries automatically using the
subset of the content as what gets included in the ref doc

To achieve this I reoganized the markdown rendering to be able to
provide a reusable system that can be cusomized for different markdown
engines.

- The markdown renderer will render a markdown compatible with Github
Flavored Markdown.
- The docusuaurs rendererer extends the markdown renderer and include a
few extra metadata.
2023-08-07 22:24:56 +00:00
Brian Terlson 615a77d4ba
Add a readme for Json Schema (#2129) 2023-06-28 20:32:10 +00:00