Skip to content

Commit 8936c86

Browse files
authored
Add support for generics on type aliases (elastic#151)
1 parent b8d632c commit 8936c86

File tree

11 files changed

+398
-286
lines changed

11 files changed

+398
-286
lines changed

Diff for: output/schema/schema.json

+319-223
Large diffs are not rendered by default.

Diff for: output/typescript/types.ts

+31-22
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ export interface AsyncSearch<TDocument = unknown> {
485485
profile: Profile
486486
_scroll_id: string
487487
_shards: ShardStatistics
488-
suggest: SuggestDictionary<TDocument>
488+
suggest?: Record<SuggestionName, Array<Suggest<TDocument>>>
489489
terminated_early: boolean
490490
timed_out: boolean
491491
took: long
@@ -2208,6 +2208,19 @@ export interface CompletionStats {
22082208
fields?: Record<Field, CompletionStats>
22092209
}
22102210

2211+
export interface CompletionSuggestOption<TDocument = unknown> {
2212+
collate_match?: boolean
2213+
contexts: Record<string, Array<Context>>
2214+
fields: Record<string, any>
2215+
_id: string
2216+
_index: IndexName
2217+
_type?: Type
2218+
_routing: Routing
2219+
_score: double
2220+
_source: TDocument
2221+
text: string
2222+
}
2223+
22112224
export interface CompletionSuggester {
22122225
contexts: Record<string, Array<SuggestContextQuery>>
22132226
fuzzy: SuggestFuzziness
@@ -5025,7 +5038,7 @@ export interface Hit<TDocument = unknown> {
50255038
_seq_no?: long
50265039
_primary_term?: long
50275040
_version?: long
5028-
sort?: Array<long | double | string>
5041+
sort?: Array<long | double | string | null>
50295042
}
50305043

50315044
export interface HitMetadata<TDocument = unknown> {
@@ -7112,6 +7125,12 @@ export interface PhraseSuggestHighlight {
71127125
pre_tag: string
71137126
}
71147127

7128+
export interface PhraseSuggestOption {
7129+
text: string
7130+
highlighted: string
7131+
score: double
7132+
}
7133+
71157134
export interface PhraseSuggester {
71167135
collate: PhraseSuggestCollate
71177136
confidence: double
@@ -8793,7 +8812,7 @@ export interface SearchResponse<TDocument = unknown> {
87938812
profile?: Profile
87948813
pit_id?: string
87958814
_scroll_id?: ScrollId
8796-
suggest?: SuggestDictionary<TDocument>
8815+
suggest?: Record<SuggestionName, Array<Suggest<TDocument>>>
87978816
terminated_early?: boolean
87988817
}
87998818

@@ -10078,12 +10097,6 @@ export interface SuggestContextQuery {
1007810097
prefix: boolean
1007910098
}
1008010099

10081-
export interface SuggestDictionary<T = unknown> {
10082-
item: Array<Suggest<T>>
10083-
keys: Array<string>
10084-
values: Array<Array<Suggest<T>>>
10085-
}
10086-
1008710100
export interface SuggestFuzziness {
1008810101
fuzziness: Fuzziness
1008910102
min_length: integer
@@ -10094,19 +10107,7 @@ export interface SuggestFuzziness {
1009410107

1009510108
export type SuggestMode = 'missing' | 'popular' | 'always'
1009610109

10097-
export interface SuggestOption<TDocument = unknown> {
10098-
collate_match: boolean
10099-
contexts: Record<string, Array<Context>>
10100-
fields: Record<string, any>
10101-
freq: long
10102-
highlighted: string
10103-
_id: string
10104-
_index: IndexName
10105-
_score: double
10106-
score: double
10107-
_source: TDocument
10108-
text: string
10109-
}
10110+
export type SuggestOption<TDocument> = CompletionSuggestOption<TDocument> | PhraseSuggestOption | TermSuggestOption
1011010111

1011110112
export type SuggestSort = 'score' | 'frequency'
1011210113

@@ -10116,6 +10117,8 @@ export interface Suggester {
1011610117
size: integer
1011710118
}
1011810119

10120+
export type SuggestionName = string
10121+
1011910122
export interface SumAggregation {
1012010123
}
1012110124

@@ -10236,6 +10239,12 @@ export interface TermQuery extends QueryBase {
1023610239
value?: string | float | boolean
1023710240
}
1023810241

10242+
export interface TermSuggestOption {
10243+
text: string
10244+
freq: long
10245+
score: double
10246+
}
10247+
1023910248
export interface TermSuggester {
1024010249
lowercase_terms: boolean
1024110250
max_edits: integer

Diff for: specification/compiler/model/metamodel.ts

+2
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ export class Enum extends BaseType {
242242
export class TypeAlias extends BaseType {
243243
kind: 'type_alias'
244244
type: ValueOf
245+
/** generic parameters: either concrete types or open parameters from the enclosing type */
246+
generics?: ValueOf[]
245247
}
246248

247249
// ------------------------------------------------------------------------------------------------

Diff for: specification/compiler/model/utils.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,18 @@ export function modelType (node: Node): model.ValueOf {
169169
return modelType(node.getLiteral())
170170
}
171171

172+
case ts.SyntaxKind.TypeParameter: {
173+
assert(Node.isTypeParameterDeclaration(node), `The node is not of type ${ts.SyntaxKind.TypeReference} but ${node.getKind()} instead`)
174+
const name = node.compilerNode.getText()
175+
const type: model.InstanceOf = {
176+
kind: 'instance_of',
177+
type: {
178+
name,
179+
namespace: getNameSpace(node)
180+
}
181+
}
182+
return type
183+
}
172184
case ts.SyntaxKind.TypeReference: {
173185
// TypeReferences are fun types, it's basically how TypeScript defines
174186
// everything that is not a basic type, an interface or a class will
@@ -384,6 +396,7 @@ export function modelTypeAlias (declaration: TypeAliasDeclaration): model.TypeAl
384396
acc[val.name] = val.value
385397
return acc
386398
}, {})
399+
const generics = declaration.getTypeParameters().map(node => modelType(node))
387400

388401
return {
389402
name: {
@@ -392,7 +405,8 @@ export function modelTypeAlias (declaration: TypeAliasDeclaration): model.TypeAl
392405
},
393406
kind: 'type_alias',
394407
type: modelType(type),
395-
...(Object.keys(annotations).length > 0 && { annotations })
408+
...(Object.keys(annotations).length > 0 && { annotations }),
409+
...(generics.length > 0 && { generics })
396410
}
397411
}
398412

Diff for: specification/specs/common.ts

+6
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ type WaitForActiveShards = byte | WaitForActiveShardOptions
141141
*/
142142
type AggregateName = string
143143

144+
/**
145+
* The suggestion name as returned from the server. Depending whether typed_keys is specified this could come back
146+
* in the form of `name#type` instead of simply `name`
147+
*/
148+
type SuggestionName = string
149+
144150
/** A reference to a date field with formatting instructions on how to return the date */
145151
class DateField {
146152
field: Field

Diff for: specification/specs/search/search/SearchResponse.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ class SearchResponse<TDocument> extends ResponseBase {
3232
profile?: Profile
3333
pit_id?: string
3434
_scroll_id?: ScrollId
35-
suggest?: SuggestDictionary<TDocument>
35+
suggest?: Dictionary<SuggestionName, Suggest<TDocument>[]>
3636
terminated_early?: boolean
3737
}

Diff for: specification/specs/search/search/hits/Hit.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ class Hit<TDocument> {
4040
_seq_no?: long
4141
_primary_term?: long
4242
_version?: long
43-
sort?: Array<long | double | string>
43+
sort?: Array<long | double | string | null>
4444
}

Diff for: specification/specs/search/suggesters/SuggestDictionary.ts

-25
This file was deleted.

Diff for: specification/specs/search/suggesters/SuggestOption.ts

+21-11
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,32 @@
1717
* under the License.
1818
*/
1919

20-
class SuggestOption<TDocument> {
21-
collate_match: boolean
20+
type SuggestOption<TDocument> =
21+
| CompletionSuggestOption<TDocument>
22+
| PhraseSuggestOption
23+
| TermSuggestOption
24+
25+
class CompletionSuggestOption<TDocument> {
26+
collate_match?: boolean
2227
contexts: Dictionary<string, Context[]>
2328
fields: Dictionary<string, UserDefinedValue>
24-
freq: long
25-
highlighted: string
2629
_id: string
2730
_index: IndexName
28-
/** This is a comment that will be exposed
29-
* @alternate_name SuggestScore
30-
* @prop_x he
31-
* **/
31+
_type?: Type
32+
_routing: Routing
3233
_score: double
33-
/** @alternate_name DocumentScore **/
34-
score: double
35-
/** @prop_serializer SourceFormatter`1 */
3634
_source: TDocument
3735
text: string
3836
}
37+
38+
class PhraseSuggestOption {
39+
text: string
40+
highlighted: string
41+
score: double
42+
}
43+
44+
class TermSuggestOption {
45+
text: string
46+
freq: long
47+
score: double
48+
}

Diff for: specification/specs/x_pack/async_search/AsyncSearch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class AsyncSearch<TDocument> {
2828
profile: Profile
2929
_scroll_id: string
3030
_shards: ShardStatistics
31-
suggest: SuggestDictionary<TDocument>
31+
suggest?: Dictionary<SuggestionName, Suggest<TDocument>[]>
3232
terminated_early: boolean
3333
timed_out: boolean
3434
took: long

Diff for: typescript-generator/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ function createEnum (type) {
203203
}
204204

205205
function createAlias (type) {
206-
return `export type ${type.name.name} = ${buildType(type.type)}\n`
206+
return `export type ${type.name.name}${buildGenerics(type)} = ${buildType(type.type)}\n`
207207
}
208208

209209
function buildInherits (type) {

0 commit comments

Comments
 (0)