Skip to content

Commit e61cc72

Browse files
committed
Implement OrderedDictionary<K, V> type
1 parent 1ec9346 commit e61cc72

File tree

8 files changed

+509
-7
lines changed

8 files changed

+509
-7
lines changed

compiler/src/model/metamodel.ts

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export class DictionaryOf {
8888
key: ValueOf
8989
value: ValueOf
9090
singleKey: boolean
91+
ordered: boolean
9192
}
9293

9394
/**

compiler/src/model/utils.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,8 @@ export function modelType (node: Node): model.ValueOf {
281281
kind: 'dictionary_of',
282282
key,
283283
value,
284-
singleKey: false
284+
singleKey: false,
285+
ordered: false
285286
}
286287
return type
287288
}
@@ -294,7 +295,21 @@ export function modelType (node: Node): model.ValueOf {
294295
kind: 'dictionary_of',
295296
key,
296297
value,
297-
singleKey: true
298+
singleKey: true,
299+
ordered: false
300+
}
301+
return type
302+
}
303+
304+
case 'OrderedDictionary': {
305+
assert(node, node.getTypeArguments().length === 2, 'A OrderedDictionary must have two arguments')
306+
const [key, value] = node.getTypeArguments().map(node => modelType(node))
307+
const type: model.DictionaryOf = {
308+
kind: 'dictionary_of',
309+
key,
310+
value,
311+
singleKey: false,
312+
ordered: true
298313
}
299314
return type
300315
}
@@ -500,7 +515,7 @@ export function modelEnumDeclaration (declaration: EnumDeclaration): model.Enum
500515
}
501516

502517
if (typeof tags.es_quirk === 'string') {
503-
type.esQuirk = tags.es_quirk
518+
type.esQuirk = tags.es_quirk.replace(/\r/g, '')
504519
}
505520

506521
return type
@@ -892,7 +907,7 @@ function hoistPropertyAnnotations (property: model.Property, jsDocs: JSDoc[]): v
892907
assert(jsDocs, value === 'container_property', `Unknown 'variant' value '${value}' on property ${property.name}`)
893908
property.containerProperty = true
894909
} else if (tag === 'es_quirk') {
895-
property.esQuirk = value
910+
property.esQuirk = value.replace(/\r/g, '')
896911
} else {
897912
assert(jsDocs, false, `Unhandled tag: '${tag}' with value: '${value}' on property ${property.name}`)
898913
}

compiler/src/transform/expand-generics.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ export function expandGenerics (inputModel: Model, config?: ExpansionConfig): Mo
359359
kind: 'dictionary_of',
360360
key: expandValueOf(value.key, mappings),
361361
value: expandValueOf(value.value, mappings),
362-
singleKey: value.singleKey
362+
singleKey: value.singleKey,
363+
ordered: value.ordered
363364
}
364365

365366
case 'instance_of': {

docs/modeling-guide.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,21 @@ For example:
2424
```json
2525
{
2626
"property1": "type",
27-
"property2": "other-type",
27+
"property2": "other-type"
2828
}
2929
```
3030

31+
### OrderedDictionary
32+
33+
Represents a dynamic key value map that preserves the order of items:
34+
35+
```ts
36+
property: OrderedDictionary<string, TypeDefinition>
37+
```
38+
39+
The JSON specification and most dictionary implementations do not make guarantees about item ordering.
40+
`OrderedDictionary` can be used to express this fact in the Elasticsearch specification.
41+
3142
### SingleKeyDictionary
3243

3344
Represents a dynamic key value map with a single top level key:

0 commit comments

Comments
 (0)