Skip to content

Commit aed39f4

Browse files
authored
Fix time-related types and properties (#1713)
1 parent a3ffdd0 commit aed39f4

File tree

263 files changed

+5402
-3005
lines changed

Some content is hidden

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

263 files changed

+5402
-3005
lines changed

compiler/src/model/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ export function modelType (node: Node): model.ValueOf {
215215
return type
216216
}
217217

218+
case ts.SyntaxKind.PrefixUnaryExpression: {
219+
// Negative number
220+
const type: model.LiteralValue = {
221+
kind: 'literal_value',
222+
value: Number(node.getText())
223+
}
224+
return type
225+
}
226+
218227
case ts.SyntaxKind.TypeParameter: {
219228
assert(node, Node.isTypeParameterDeclaration(node), `The node is not of type ${ts.SyntaxKind[ts.SyntaxKind.TypeReference]} but ${ts.SyntaxKind[node.getKind()]} instead`)
220229
const name = node.compilerNode.getText()

docs/modeling-guide.md

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,16 @@ feel free to add more if it feels appropriate!
156156
157157
### Dates
158158
159-
The `Date` type in TypeScript refers to the JavaScript `Date` object,
160-
since Elasticsearch needs a string or a numeric value, there are aliases also for date types:
159+
Elasticsearch uses a lot of dates, times and durations. There are various types available to capture the variety of types and representations in the specification:
160+
161+
* for date and time: `DateTime` for formatted dates, `EpochTime<UnitMillis>`, `EpochTime<UnitSeconds>`, etc. for number values
162+
* for intervals: `Duration` for formatted values, `DurationValue<UnitMillis>`, `DurationValue<UnitSeconds>`, etc. for number values
163+
* for time of day: `TimeOfDay
164+
165+
See [`specification/_types/Time.ts`](../specification/_types/Time.ts) for additional details.
166+
167+
Since code generators may choose to use a platform builtin type to represent time-related data, make sure to choose the appropriate representation and **never** use a primitive value such as `string` or `long`.
161168

162-
```ts
163-
type Timestamp = string
164-
type TimeSpan = string
165-
type DateString = string
166-
```
167169

168170
### Binary
169171

@@ -230,6 +232,31 @@ class Response {
230232
}
231233
```
232234

235+
### Stringified values
236+
237+
Elasticsearch sometimes uses string-only representations in the JSON it outputs, even for numbers and booleans. This is notably seen in `cat` request and index and cluster settings.
238+
239+
To keep the semantic soundness of the specification and avoid adding ` | string` to handle these cases, the `Stringified` behaviour should be used for these cases. Also, this problem only affects output: data should be sent in its original format (i.e. number, boolean, etc).
240+
241+
Instead of:
242+
```ts
243+
export class IndexSettings {
244+
// DO NOT DO THAT
245+
number_of_shards?: integer | string
246+
number_of_replicas?: integer | string
247+
hidden?: boolean | string
248+
}
249+
```
250+
251+
Use the `Stringified` behavior:
252+
```
253+
export class IndexSettings {
254+
number_of_shards?: Stringified<integer>
255+
number_of_replicas?: Stringified<integer>
256+
hidden?: Stringified<boolean>
257+
}
258+
```
259+
233260
### Variants
234261
235262
Variants is a special syntax that can be used by language generators to understand

docs/validation-example.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ open it with your favourite editor and perform the fix
6969
pipeline?: string;
7070
refresh?: Refresh;
7171
routing?: Routing;
72-
timeout?: Time;
72+
timeout?: Duration;
7373
version?: long;
7474
version_type?: VersionType;
7575
wait_for_active_shards?: string;

0 commit comments

Comments
 (0)