Skip to content

Added variants handling #234

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

Merged
merged 14 commits into from
Mar 29, 2021
141 changes: 141 additions & 0 deletions docs/modeling-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,144 @@ type Timestamp = string
type TimeSpan = string
type DateString = string
```

### Literal values

The compiler supports literal values as well. This can be useful if a
definition changes based on a specific field.

```ts
class Foo {
type: 'foo',
prop: string
}

class Bar {
type: 'bar',
prop: boolean
}

type FooOrBar = Foo | Bar
```

The example shown above is the correct way to solve this cases, but to make it
easy to use in every language you need to add a *variant* definition as well.
You can find how it works in the next section.

### Variants

Variants is a special syntax that can be used by language generators to understand
which type they will need to build based on the variant configuration.
There are three type of variants:

#### Internal

The key used as variant is present inside the definition, for example:

```ts
class Foo {
type: 'foo', // 'type' is the variant
prop: string
}
```

If the variant type is internal you should configure the parent type with
the `@variants` js doc tag. teh syntax is:

```ts
/** @variants internal tag='<field-name>' */
```

For example:

```ts
class Foo {
type: 'foo',
prop: string
}

class Bar {
type: 'bar',
prop: boolean
}

/** @variants internal tag='type' */
type FooOrBar = Foo | Bar
```

An example of internal variants are the type mapping properties.

#### External

The key that defines the variant is external to the definition, like in the
case of aggregations in responses or suggesters.

The variant type should be configured in the parent type, while the variant
name in the definition itself.

The syntax is:

```ts
/** @variants external */

/** @variant name='<field-name>' */
```

For example:

```ts
/** @variants external */
type FooAlias = Faz | Bar

/** @variant name='faz' */
class Faz {
prop: string
}

/** @variant name='bar' */
class Bar {
prop: boolean
}
```

In the example above, `FooAlias` will look like this:

```json
{
"faz": {
"prop": "hello world"
}
}
```

or:

```json
{
"bar": {
"prop": true
}
}
```

#### Container

The container variant is used for all the types that contains all the
variants inside the defintion. An example is `QueryContainer`.

The syntax is:

```ts
/** @variants container */
```

For example:

```ts
/** @variants container */
class FooContainer {
bar: BarDefinition
baz: BazDefinition
faz: FazDefinition
}
```
11 changes: 1 addition & 10 deletions output/dangling-types/dangling.csv
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ Condition,x_pack/watcher/condition/Condition.ts
Input,x_pack/watcher/input/Input.ts
Schedule,x_pack/watcher/schedule/Schedule.ts
TriggerEvent,x_pack/watcher/trigger/TriggerEvent.ts
NestedProperty,mapping/types/complex/nested/NestedProperty.ts
NumberType,mapping/types/core/number/NumberType.ts
RangeType,mapping/types/core/range/RangeType.ts
GeoTree,mapping/types/geo/geo_shape/GeoTree.ts
GeoPointFielddataFormat,modules/indices/fielddata/geo_point/GeoPointFielddataFormat.ts
WeightScoreFunction,query_dsl/compound/function_score/functions/ScoreFunction.ts
Expand All @@ -106,10 +103,4 @@ RareFunction,x_pack/machine_learning/job/detectors/RareFunction.ts
SumFunction,x_pack/machine_learning/job/detectors/SumFunction.ts
TimeFunction,x_pack/machine_learning/job/detectors/TimeFunction.ts
DataAttachmentFormat,x_pack/watcher/action/email/DataAttachmentFormat.ts
SlackActionMessageResult,x_pack/watcher/execution/slack/SlackActionMessageResult.ts
DateRangeProperty,mapping/types/core/range/date_range/DateRangeProperty.ts
DoubleRangeProperty,mapping/types/core/range/double_range/DoubleRangeProperty.ts
FloatRangeProperty,mapping/types/core/range/float_range/FloatRangeProperty.ts
IntegerRangeProperty,mapping/types/core/range/integer_range/IntegerRangeProperty.ts
IpRangeProperty,mapping/types/core/range/ip_range/IpRangeProperty.ts
LongRangeProperty,mapping/types/core/range/long_range/LongRangeProperty.ts
SlackActionMessageResult,x_pack/watcher/execution/slack/SlackActionMessageResult.ts
Loading