Skip to content

Commit

Permalink
Add new behaviors (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
delvedor authored Jan 27, 2021
1 parent 8c200cf commit 495cb45
Show file tree
Hide file tree
Showing 40 changed files with 707 additions and 337 deletions.
610 changes: 491 additions & 119 deletions output/schema/schema.json

Large diffs are not rendered by default.

309 changes: 153 additions & 156 deletions output/typescript/types.ts

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions specification/specs/behaviors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,19 @@
* @behavior Defines a trait that any unknown property for the class should be typed to TValue
*/
interface AdditionalProperties<TKey, TValue> {}

/**
* A response formatted as an array of records.
* Some languages can't represent this easily and need to wrap the
* array inside an object.
* @behavior Defines a trait that the response should be an array of records typed to TRecord
*/
interface ArrayResponse<TRecord> {}

/**
* HEAD APIs can have a different behavior based on the language,
* the response body is always empty to it's up to language generators
* to define how those should be represented.
* @behavior Defines a trait that the response is empty
*/
interface EmptyResponseBase {}
4 changes: 0 additions & 4 deletions specification/specs/cat/CatBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,3 @@ class CatRequestBase extends RequestBase {
s?: string[];
v?: boolean;
}

class CatResponseBase<TCatRecord> extends ResponseBase {
records: TCatRecord[];
}
2 changes: 1 addition & 1 deletion specification/specs/cat/cat_aliases/CatAliasesResponse.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatAliasesResponse extends CatResponseBase<CatAliasesRecord> {}
class CatAliasesResponse extends ResponseBase implements ArrayResponse<CatAliasesRecord> {}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatAllocationResponse extends CatResponseBase<CatAllocationRecord> {}
class CatAllocationResponse extends ResponseBase implements ArrayResponse<CatAllocationRecord> {}
2 changes: 1 addition & 1 deletion specification/specs/cat/cat_count/CatCountResponse.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatCountResponse extends CatResponseBase<CatCountRecord> {}
class CatCountResponse extends ResponseBase implements ArrayResponse<CatCountRecord> {}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatDataFrameAnalyticsResponse extends CatResponseBase<CatDataFrameAnalyticsRecord> {}
class CatDataFrameAnalyticsResponse extends ResponseBase implements ArrayResponse<CatDataFrameAnalyticsRecord> {}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatDatafeedsResponse extends CatResponseBase<CatDatafeedsRecord> {}
class CatDatafeedsResponse extends ResponseBase implements ArrayResponse<CatDatafeedsRecord> {}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatFielddataResponse extends CatResponseBase<CatFielddataRecord> {}
class CatFielddataResponse extends ResponseBase implements ArrayResponse<CatFielddataRecord> {}
2 changes: 1 addition & 1 deletion specification/specs/cat/cat_health/CatHealthResponse.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatHealthResponse extends CatResponseBase<CatHealthRecord> {}
class CatHealthResponse extends ResponseBase implements ArrayResponse<CatHealthRecord> {}
2 changes: 1 addition & 1 deletion specification/specs/cat/cat_help/CatHelpResponse.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatHelpResponse extends CatResponseBase<CatHelpRecord> {}
class CatHelpResponse extends ResponseBase implements ArrayResponse<CatHelpRecord> {}
2 changes: 1 addition & 1 deletion specification/specs/cat/cat_indices/CatIndicesResponse.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatIndicesResponse extends CatResponseBase<CatIndicesRecord> {}
class CatIndicesResponse extends ResponseBase implements ArrayResponse<CatIndicesRecord> {}
2 changes: 1 addition & 1 deletion specification/specs/cat/cat_jobs/CatJobsResponse.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatJobsResponse extends CatResponseBase<CatJobsRecord> {}
class CatJobsResponse extends ResponseBase implements ArrayResponse<CatJobsRecord> {}
2 changes: 1 addition & 1 deletion specification/specs/cat/cat_master/CatMasterResponse.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatMasterResponse extends CatResponseBase<CatMasterRecord> {}
class CatMasterResponse extends ResponseBase implements ArrayResponse<CatMasterRecord> {}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatNodeAttributesResponse extends CatResponseBase<CatNodeAttributesRecord> {}
class CatNodeAttributesResponse extends ResponseBase implements ArrayResponse<CatNodeAttributesRecord> {}
2 changes: 1 addition & 1 deletion specification/specs/cat/cat_nodes/CatNodesResponse.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatNodesResponse extends CatResponseBase<CatNodesRecord> {}
class CatNodesResponse extends ResponseBase implements ArrayResponse<CatNodesRecord> {}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatPendingTasksResponse extends CatResponseBase<CatPendingTasksRecord> {}
class CatPendingTasksResponse extends ResponseBase implements ArrayResponse<CatPendingTasksRecord> {}
2 changes: 1 addition & 1 deletion specification/specs/cat/cat_plugins/CatPluginsResponse.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatPluginsResponse extends CatResponseBase<CatPluginsRecord> {}
class CatPluginsResponse extends ResponseBase implements ArrayResponse<CatPluginsRecord> {}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatRecoveryResponse extends CatResponseBase<CatRecoveryRecord> {}
class CatRecoveryResponse extends ResponseBase implements ArrayResponse<CatRecoveryRecord> {}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatRepositoriesResponse extends CatResponseBase<CatRepositoriesRecord> {}
class CatRepositoriesResponse extends ResponseBase implements ArrayResponse<CatRepositoriesRecord> {}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatSegmentsResponse extends CatResponseBase<CatSegmentsRecord> {}
class CatSegmentsResponse extends ResponseBase implements ArrayResponse<CatSegmentsRecord> {}
2 changes: 1 addition & 1 deletion specification/specs/cat/cat_shards/CatShardsResponse.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatShardsResponse extends CatResponseBase<CatShardsRecord> {}
class CatShardsResponse extends ResponseBase implements ArrayResponse<CatShardsRecord> {}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatSnapshotsResponse extends CatResponseBase<CatSnapshotsRecord> {}
class CatSnapshotsResponse extends ResponseBase implements ArrayResponse<CatSnapshotsRecord> {}
2 changes: 1 addition & 1 deletion specification/specs/cat/cat_tasks/CatTasksResponse.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatTasksResponse extends CatResponseBase<CatTasksRecord> {}
class CatTasksResponse extends ResponseBase implements ArrayResponse<CatTasksRecord> {}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatTemplatesResponse extends CatResponseBase<CatTemplatesRecord> {}
class CatTemplatesResponse extends ResponseBase implements ArrayResponse<CatTemplatesRecord> {}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatThreadPoolResponse extends CatResponseBase<CatThreadPoolRecord> {}
class CatThreadPoolResponse extends ResponseBase implements ArrayResponse<CatThreadPoolRecord> {}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatTrainedModelsResponse extends CatResponseBase<CatTrainedModelsRecord> {}
class CatTrainedModelsResponse extends ResponseBase implements ArrayResponse<CatTrainedModelsRecord> {}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class CatTransformsResponse extends CatResponseBase<CatTransformsRecord> {}
class CatTransformsResponse extends ResponseBase implements ArrayResponse<CatTransformsRecord> {}
2 changes: 1 addition & 1 deletion specification/specs/cluster/ping/PingResponse.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class PingResponse extends EmptyResponseBase {}
class PingResponse extends ResponseBase implements EmptyResponseBase {}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
class ResponseBase {
// error: MainError;
// status: integer;
// Do not add properties to this base class.
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class DocumentExistsResponse extends EmptyResponseBase {}
class DocumentExistsResponse extends ResponseBase implements EmptyResponseBase {}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class SourceExistsResponse extends EmptyResponseBase {}
class SourceExistsResponse extends ResponseBase implements EmptyResponseBase {}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class AliasExistsResponse extends EmptyResponseBase {}
class AliasExistsResponse extends ResponseBase implements EmptyResponseBase {}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class IndexExistsResponse extends EmptyResponseBase {}
class IndexExistsResponse extends ResponseBase implements EmptyResponseBase {}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class TypeExistsResponse extends EmptyResponseBase {}
class TypeExistsResponse extends ResponseBase implements EmptyResponseBase {}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class IndexTemplateExistsResponse extends EmptyResponseBase {}
class IndexTemplateExistsResponse extends ResponseBase implements EmptyResponseBase {}
2 changes: 1 addition & 1 deletion specification/src/api-specification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Specification {
}
}

const knownBehaviors = ["AdditionalProperties"]
const knownBehaviors = ["AdditionalProperties", 'ArrayResponse', 'EmptyResponseBase']
const specVisitor = new TypeReader(this.program)
const types = [].concat(specVisitor.interfaces).concat(specVisitor.enums)
// resolve inherits by creating the proper pointers to instances, pretty hairy but it works
Expand Down
33 changes: 13 additions & 20 deletions typescript-generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ let definitions = `/*
*/\n\n`

const skip = [
'CatResponseBase',
'ResponseBase',
'ArrayResponse',
'EmptyResponseBase',
'AdditionalProperties'
]
Expand Down Expand Up @@ -159,6 +160,8 @@ function createAlias (type) {

function buildInherits (type) {
if (!Array.isArray(type.inherits)) return ''
// ResponseBase will always be empty
if (type.inherits[0].type.name === 'ResponseBase') return ''
return ` extends ${type.inherits.map(buildInheritType).join(', ')}`

function buildInheritType (type) {
Expand All @@ -185,15 +188,9 @@ function cleanPropertyName (name) {
}

function isSpecialInterface (type) {
if (/^Cat.*Response$/g.test(type.name.name)) {
return true
}
if (Array.isArray(type.attachedBehaviors)) {
return type.attachedBehaviors.length > 0
}
if (Array.isArray(type.inherits) && type.inherits[0].type.name === 'EmptyResponseBase') {
return true
}
switch (type.name.name) {
case 'DictionaryResponseBase':
return true
Expand Down Expand Up @@ -230,16 +227,19 @@ function serializeAdditionalPropertiesType (type) {
}

function serializeSpecialInterface (type) {
if (/^Cat.*Response$/g.test(type.name.name)) {
return buildCatResponse(type)
}
if (Array.isArray(type.attachedBehaviors)) {
if (type.attachedBehaviors.includes('AdditionalProperties')) {
return serializeAdditionalPropertiesType(type)
}
}
if (Array.isArray(type.inherits) && type.inherits[0].type.name === 'EmptyResponseBase') {
return `export type ${type.name.name} = boolean\n`
if (type.attachedBehaviors.includes('ArrayResponse')) {
// In the input spec the Cat* responses are represented as an object
// that contains a `records` key, which is an array of the inherited generic.
// What ES actually sends back, is an array of the inherited generic.
return `export type ${type.name.name} = ${type.behaviors[0].generics[0].type.name}[]\n`
}
if (type.attachedBehaviors.includes('EmptyResponseBase')) {
return `export type ${type.name.name} = boolean\n`
}
}

switch (type.name.name) {
Expand All @@ -263,10 +263,3 @@ function buildIndexer (type) {
return t === 'AggregateName' ? 'string' : t
}
}

// In the input spec the Cat* responses are represented as an object
// that contains a `records` key, which is an array of the inherited generic.
// What ES actually sends back, is an array of the inherited generic.
function buildCatResponse (type) {
return `export type ${type.name.name} = ${type.inherits[0].generics[0].type.name}[]\n`
}

0 comments on commit 495cb45

Please sign in to comment.