Skip to content

Commit 7e7ee83

Browse files
committed
Add Stringified
1 parent a3ffdd0 commit 7e7ee83

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
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: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ The `Date` type in TypeScript refers to the JavaScript `Date` object,
160160
since Elasticsearch needs a string or a numeric value, there are aliases also for date types:
161161
162162
```ts
163-
type Timestamp = string
163+
type DateTime = string
164164
type TimeSpan = string
165-
type DateString = string
165+
type DateTime = string
166166
```
167167
168168
### Binary
@@ -230,6 +230,31 @@ class Response {
230230
}
231231
```
232232

233+
### Stringified values
234+
235+
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.
236+
237+
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).
238+
239+
Instead of:
240+
```ts
241+
export class IndexSettings {
242+
// DO NOT DO THAT
243+
number_of_shards?: integer | string
244+
number_of_replicas?: integer | string
245+
hidden?: boolean | string
246+
}
247+
```
248+
249+
Use the `Stringified` behavior:
250+
```
251+
export class IndexSettings {
252+
number_of_shards?: Stringified<integer>
253+
number_of_replicas?: Stringified<integer>
254+
hidden?: Stringified<boolean>
255+
}
256+
```
257+
233258
### Variants
234259

235260
Variants is a special syntax that can be used by language generators to understand
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
/**
21+
* Some APIs will return values such as numbers also as a string (notably epoch timestamps). This behavior
22+
* is used to capture this behavior while keeping the semantics of the field type.
23+
*
24+
* Depending on the target language, code generators can keep the union or remove it and leniently parse
25+
* strings to the target type.
26+
*/
27+
export type Stringified<T> = T | string

0 commit comments

Comments
 (0)