Skip to content

Commit 657f383

Browse files
authored
Added variants handling (#234)
1 parent a011861 commit 657f383

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1561
-444
lines changed

docs/modeling-guide.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,144 @@ type Timestamp = string
138138
type TimeSpan = string
139139
type DateString = string
140140
```
141+
142+
### Literal values
143+
144+
The compiler supports literal values as well. This can be useful if a
145+
definition changes based on a specific field.
146+
147+
```ts
148+
class Foo {
149+
type: 'foo',
150+
prop: string
151+
}
152+
153+
class Bar {
154+
type: 'bar',
155+
prop: boolean
156+
}
157+
158+
type FooOrBar = Foo | Bar
159+
```
160+
161+
The example shown above is the correct way to solve this cases, but to make it
162+
easy to use in every language you need to add a *variant* definition as well.
163+
You can find how it works in the next section.
164+
165+
### Variants
166+
167+
Variants is a special syntax that can be used by language generators to understand
168+
which type they will need to build based on the variant configuration.
169+
There are three type of variants:
170+
171+
#### Internal
172+
173+
The key used as variant is present inside the definition, for example:
174+
175+
```ts
176+
class Foo {
177+
type: 'foo', // 'type' is the variant
178+
prop: string
179+
}
180+
```
181+
182+
If the variant type is internal you should configure the parent type with
183+
the `@variants` js doc tag. teh syntax is:
184+
185+
```ts
186+
/** @variants internal tag='<field-name>' */
187+
```
188+
189+
For example:
190+
191+
```ts
192+
class Foo {
193+
type: 'foo',
194+
prop: string
195+
}
196+
197+
class Bar {
198+
type: 'bar',
199+
prop: boolean
200+
}
201+
202+
/** @variants internal tag='type' */
203+
type FooOrBar = Foo | Bar
204+
```
205+
206+
An example of internal variants are the type mapping properties.
207+
208+
#### External
209+
210+
The key that defines the variant is external to the definition, like in the
211+
case of aggregations in responses or suggesters.
212+
213+
The variant type should be configured in the parent type, while the variant
214+
name in the definition itself.
215+
216+
The syntax is:
217+
218+
```ts
219+
/** @variants external */
220+
221+
/** @variant name='<field-name>' */
222+
```
223+
224+
For example:
225+
226+
```ts
227+
/** @variants external */
228+
type FooAlias = Faz | Bar
229+
230+
/** @variant name='faz' */
231+
class Faz {
232+
prop: string
233+
}
234+
235+
/** @variant name='bar' */
236+
class Bar {
237+
prop: boolean
238+
}
239+
```
240+
241+
In the example above, `FooAlias` will look like this:
242+
243+
```json
244+
{
245+
"faz": {
246+
"prop": "hello world"
247+
}
248+
}
249+
```
250+
251+
or:
252+
253+
```json
254+
{
255+
"bar": {
256+
"prop": true
257+
}
258+
}
259+
```
260+
261+
#### Container
262+
263+
The container variant is used for all the types that contains all the
264+
variants inside the defintion. An example is `QueryContainer`.
265+
266+
The syntax is:
267+
268+
```ts
269+
/** @variants container */
270+
```
271+
272+
For example:
273+
274+
```ts
275+
/** @variants container */
276+
class FooContainer {
277+
bar: BarDefinition
278+
baz: BazDefinition
279+
faz: FazDefinition
280+
}
281+
```

output/dangling-types/dangling.csv

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ Condition,x_pack/watcher/condition/Condition.ts
8888
Input,x_pack/watcher/input/Input.ts
8989
Schedule,x_pack/watcher/schedule/Schedule.ts
9090
TriggerEvent,x_pack/watcher/trigger/TriggerEvent.ts
91-
NestedProperty,mapping/types/complex/nested/NestedProperty.ts
92-
NumberType,mapping/types/core/number/NumberType.ts
93-
RangeType,mapping/types/core/range/RangeType.ts
9491
GeoTree,mapping/types/geo/geo_shape/GeoTree.ts
9592
GeoPointFielddataFormat,modules/indices/fielddata/geo_point/GeoPointFielddataFormat.ts
9693
WeightScoreFunction,query_dsl/compound/function_score/functions/ScoreFunction.ts
@@ -106,10 +103,4 @@ RareFunction,x_pack/machine_learning/job/detectors/RareFunction.ts
106103
SumFunction,x_pack/machine_learning/job/detectors/SumFunction.ts
107104
TimeFunction,x_pack/machine_learning/job/detectors/TimeFunction.ts
108105
DataAttachmentFormat,x_pack/watcher/action/email/DataAttachmentFormat.ts
109-
SlackActionMessageResult,x_pack/watcher/execution/slack/SlackActionMessageResult.ts
110-
DateRangeProperty,mapping/types/core/range/date_range/DateRangeProperty.ts
111-
DoubleRangeProperty,mapping/types/core/range/double_range/DoubleRangeProperty.ts
112-
FloatRangeProperty,mapping/types/core/range/float_range/FloatRangeProperty.ts
113-
IntegerRangeProperty,mapping/types/core/range/integer_range/IntegerRangeProperty.ts
114-
IpRangeProperty,mapping/types/core/range/ip_range/IpRangeProperty.ts
115-
LongRangeProperty,mapping/types/core/range/long_range/LongRangeProperty.ts
106+
SlackActionMessageResult,x_pack/watcher/execution/slack/SlackActionMessageResult.ts

0 commit comments

Comments
 (0)