Skip to content

Commit

Permalink
Add untagged variant to the modelling guide
Browse files Browse the repository at this point in the history
  • Loading branch information
flobernd committed Jun 13, 2024
1 parent 701f55d commit 93ea8c6
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion docs/modeling-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ An annotation allows distinguishing these properties from container variants:

For example:

```
```ts
/**
* @variants container
*/
Expand All @@ -435,6 +435,38 @@ class AggregationContainer {
...
```
#### Untagged
The untagged variant is used for unions that can only be distinguished by the type of one or more fields.
> [!WARNING]
> This variant should only be used for legacy types and should otherwise be avoided as far as possible, as it leads to less optimal code generation in the client libraries.
The syntax is:
```ts
/** @variants untagged */
```
Untagged variants must exactly follow a defined pattern.
For example:
```ts
export class MyTypeBase<T1, T2, ...> { ... }

export class MyTypeSpecialized1 extends MyTypeBase<int> {}
export class MyTypeSpecialized2 extends MyTypeBase<string> {}
export class MyTypeSpecialized3 extends MyTypeBase<bool> {}

/**
* @codegen_names mytype1, mytypet2, mytype3
* @variant untagged
*/
// Note: deserialization depends on value types
export type MyType = MyTypeSpecialized1 | MyTypeSpecialized2 | MyTypeSpecialized3
```
### Shortcut properties
In many places Elasticsearch accepts a property value to be either a complete data structure or a single value, that value being a shortcut for a property in the data structure.
Expand Down

0 comments on commit 93ea8c6

Please sign in to comment.