From cc4c0af5d61cd53a676c215ebf34bbfd4087b6ba Mon Sep 17 00:00:00 2001 From: Michael Hayes Date: Fri, 19 Apr 2019 16:07:17 -0700 Subject: [PATCH 1/5] Replace import equals declarations with const and type alias statements --- src/main/render/thrift-server/typedef.ts | 113 ++++++++++-------- .../com/test/calculator/typedefs.ts | 10 +- .../com/test/common/typedefs.ts | 52 ++++---- .../com/test/operation/typedefs.ts | 18 +-- .../generated/com/test/calculator/typedefs.ts | 10 +- .../generated/com/test/common/typedefs.ts | 34 +++--- .../generated/com/test/operation/typedefs.ts | 18 +-- 7 files changed, 136 insertions(+), 119 deletions(-) diff --git a/src/main/render/thrift-server/typedef.ts b/src/main/render/thrift-server/typedef.ts index 01868369..4eef013b 100644 --- a/src/main/render/thrift-server/typedef.ts +++ b/src/main/render/thrift-server/typedef.ts @@ -13,7 +13,13 @@ import { TypeMapping } from './types' import { DefinitionType, IRenderState, IResolvedIdentifier } from '../../types' -import { className, looseName, strictName, toolkitName } from './struct/utils' +import { + className, + looseName, + strictName, + tokens, + toolkitName, +} from './struct/utils' import { fieldInterfaceName, @@ -25,6 +31,7 @@ import { resolveIdentifierDefinition, resolveIdentifierName, } from '../../resolver' +import { createConst } from '../shared/utils' function renderStrictInterfaceReexport( id: IResolvedIdentifier, @@ -33,14 +40,14 @@ function renderStrictInterfaceReexport( state: IRenderState, ): ts.Statement { if (id.pathName !== undefined) { - return ts.createImportEqualsDeclaration( + return ts.createTypeAliasDeclaration( undefined, - [ts.createToken(ts.SyntaxKind.ExportKeyword)], - ts.createIdentifier( - strictName(node.name.value, definition.type, state), - ), - ts.createIdentifier( - `${strictName(id.rawName, definition.type, state)}`, + tokens(true), + strictName(node.name.value, definition.type, state), + undefined, + ts.createTypeReferenceNode( + strictName(id.rawName, definition.type, state), + undefined, ), ) } else { @@ -69,14 +76,14 @@ function renderLooseInterfaceReexport( state: IRenderState, ): ts.Statement { if (id.pathName !== undefined) { - return ts.createImportEqualsDeclaration( + return ts.createTypeAliasDeclaration( undefined, - [ts.createToken(ts.SyntaxKind.ExportKeyword)], - ts.createIdentifier( - looseName(node.name.value, definition.type, state), - ), - ts.createIdentifier( - `${looseName(id.rawName, definition.type, state)}`, + tokens(true), + looseName(node.name.value, definition.type, state), + undefined, + ts.createTypeReferenceNode( + looseName(id.rawName, definition.type, state), + undefined, ), ) } else { @@ -104,11 +111,13 @@ function renderClassReexport( state: IRenderState, ): ts.Statement { if (id.pathName !== undefined) { - return ts.createImportEqualsDeclaration( - undefined, - [ts.createToken(ts.SyntaxKind.ExportKeyword)], - ts.createIdentifier(className(node.name.value, state)), - ts.createIdentifier(`${className(id.rawName, state)}`), + return ts.createVariableStatement( + tokens(true), + createConst( + className(node.name.value, state), + undefined, + ts.createIdentifier(className(id.rawName, state)), + ), ) } else { return ts.createExportDeclaration( @@ -132,11 +141,13 @@ function renderToolkitReexport( state: IRenderState, ): ts.Statement { if (id.pathName !== undefined) { - return ts.createImportEqualsDeclaration( - undefined, - [ts.createToken(ts.SyntaxKind.ExportKeyword)], - ts.createIdentifier(toolkitName(node.name.value, state)), - ts.createIdentifier(`${toolkitName(id.rawName, state)}`), + return ts.createVariableStatement( + tokens(true), + createConst( + toolkitName(node.name.value, state), + undefined, + ts.createIdentifier(toolkitName(id.rawName, state)), + ), ) } else { return ts.createExportDeclaration( @@ -159,11 +170,15 @@ function renderUnionTypeReexport( state: IRenderState, ): ts.Statement { if (id.pathName !== undefined) { - return ts.createImportEqualsDeclaration( + return ts.createTypeAliasDeclaration( + undefined, + tokens(true), + renderUnionTypeName(node.name.value, state), undefined, - [ts.createToken(ts.SyntaxKind.ExportKeyword)], - ts.createIdentifier(renderUnionTypeName(node.name.value, state)), - ts.createIdentifier(`${renderUnionTypeName(id.rawName, state)}`), + ts.createTypeReferenceNode( + renderUnionTypeName(id.rawName, state), + undefined, + ), ) } else { return ts.createExportDeclaration( @@ -192,22 +207,18 @@ function renderUnionInterfaceReexports( ): Array { if (id.pathName !== undefined) { return union.fields.map((next: FieldDefinition) => { - return ts.createImportEqualsDeclaration( + return ts.createTypeAliasDeclaration( undefined, - [ts.createToken(ts.SyntaxKind.ExportKeyword)], - ts.createIdentifier( - fieldInterfaceName( - node.name.value, - next.name.value, - strict, - ), - ), - ts.createIdentifier( + tokens(true), + fieldInterfaceName(node.name.value, next.name.value, strict), + undefined, + ts.createTypeReferenceNode( `${id.pathName}.${fieldInterfaceName( union.name.value, next.name.value, strict, )}`, + undefined, ), ) }) @@ -245,11 +256,15 @@ function renderUnionArgsReexport( state: IRenderState, ): ts.Statement { if (id.pathName !== undefined) { - return ts.createImportEqualsDeclaration( + return ts.createTypeAliasDeclaration( undefined, - [ts.createToken(ts.SyntaxKind.ExportKeyword)], - ts.createIdentifier(unionTypeName(node.name.value, state, false)), - ts.createIdentifier(`${unionTypeName(id.rawName, state, false)}`), + tokens(true), + unionTypeName(node.name.value, state, false), + undefined, + ts.createTypeReferenceNode( + unionTypeName(id.rawName, state, false), + undefined, + ), ) } else { return ts.createExportDeclaration( @@ -333,11 +348,13 @@ function renderTypeDefForIdentifier( case SyntaxType.ConstDefinition: case SyntaxType.EnumDefinition: return [ - ts.createImportEqualsDeclaration( - undefined, - [ts.createToken(ts.SyntaxKind.ExportKeyword)], - ts.createIdentifier(node.name.value), - ts.createIdentifier(resolvedIdentifier.fullName), + ts.createVariableStatement( + tokens(true), + createConst( + node.name.value, + undefined, + ts.createIdentifier(resolvedIdentifier.fullName), + ), ), ] diff --git a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/calculator/typedefs.ts b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/calculator/typedefs.ts index 86551bad..c9fd2582 100644 --- a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/calculator/typedefs.ts +++ b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/calculator/typedefs.ts @@ -6,8 +6,8 @@ import * as com_test_operation from "./../operation"; import * as com_test_common from "./../common"; export type MyInteger = number; -export import Operation = com_test_operation.Operation; -export import ICommonStruct = com_test_common.ICommonStruct; -export import ICommonStructArgs = com_test_common.ICommonStructArgs; -export import CommonStruct = com_test_common.CommonStruct; -export import CommonStructCodec = com_test_common.CommonStructCodec; +export const Operation = com_test_operation.Operation; +export type ICommonStruct = com_test_common.ICommonStruct; +export type ICommonStructArgs = com_test_common.ICommonStructArgs; +export const CommonStruct = com_test_common.CommonStruct; +export const CommonStructCodec = com_test_common.CommonStructCodec; diff --git a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/typedefs.ts b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/typedefs.ts index c90831ef..a5f6b64c 100644 --- a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/typedefs.ts +++ b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/typedefs.ts @@ -1,32 +1,32 @@ /* tslint:disable */ /* - * Autogenerated by @creditkarma/thrift-typescript v{{VERSION}} + * Autogenerated by @creditkarma/thrift-typescript v3.5.0 * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING */ import * as com_test_shared from "./../shared"; import * as __NAMESPACE__ from "./."; -export import ICommonStruct = com_test_shared.ISharedStruct; -export import ICommonStructArgs = com_test_shared.ISharedStructArgs; -export import CommonStruct = com_test_shared.SharedStruct; -export import CommonStructCodec = com_test_shared.SharedStructCodec; -export import CommonUnionType = com_test_shared.SharedUnionType; -export import CommonUnion = com_test_shared.SharedUnion; -export import ICommonUnionWithOption1 = com_test_shared.ISharedUnionWithOption1; -export import ICommonUnionWithOption2 = com_test_shared.ISharedUnionWithOption2; -export import CommonUnionArgs = com_test_shared.SharedUnionArgs; -export import ICommonUnionWithOption1Args = com_test_shared.ISharedUnionWithOption1Args; -export import ICommonUnionWithOption2Args = com_test_shared.ISharedUnionWithOption2Args; -export import CommonUnionCodec = com_test_shared.SharedUnionCodec; -export import COMMON_INT = com_test_shared.SHARED_INT; -export import INotAllowed = __NAMESPACE__.IAuthException; -export import INotAllowedArgs = __NAMESPACE__.IAuthExceptionArgs; -export import NotAllowed = __NAMESPACE__.AuthException; -export import NotAllowedCodec = __NAMESPACE__.AuthExceptionCodec; -export import MoreOptionsType = __NAMESPACE__.OtherCommonUnionType; -export import MoreOptions = __NAMESPACE__.OtherCommonUnion; -export import IMoreOptionsWithOption1 = __NAMESPACE__.IOtherCommonUnionWithOption1; -export import IMoreOptionsWithOption2 = __NAMESPACE__.IOtherCommonUnionWithOption2; -export import MoreOptionsArgs = __NAMESPACE__.OtherCommonUnionArgs; -export import IMoreOptionsWithOption1Args = __NAMESPACE__.IOtherCommonUnionWithOption1Args; -export import IMoreOptionsWithOption2Args = __NAMESPACE__.IOtherCommonUnionWithOption2Args; -export import MoreOptionsCodec = __NAMESPACE__.OtherCommonUnionCodec; +export type ICommonStruct = com_test_shared.ISharedStruct; +export type ICommonStructArgs = com_test_shared.ISharedStructArgs; +export const CommonStruct = com_test_shared.SharedStruct; +export const CommonStructCodec = com_test_shared.SharedStructCodec; +export type CommonUnionType = com_test_shared.SharedUnionType; +export const CommonUnion = com_test_shared.SharedUnion; +export type ICommonUnionWithOption1 = com_test_shared.ISharedUnionWithOption1; +export type ICommonUnionWithOption2 = com_test_shared.ISharedUnionWithOption2; +export type CommonUnionArgs = com_test_shared.SharedUnionArgs; +export type ICommonUnionWithOption1Args = com_test_shared.ISharedUnionWithOption1Args; +export type ICommonUnionWithOption2Args = com_test_shared.ISharedUnionWithOption2Args; +export const CommonUnionCodec = com_test_shared.SharedUnionCodec; +export const COMMON_INT = com_test_shared.SHARED_INT; +export type INotAllowed = __NAMESPACE__.IAuthException; +export type INotAllowedArgs = __NAMESPACE__.IAuthExceptionArgs; +export const NotAllowed = __NAMESPACE__.AuthException; +export const NotAllowedCodec = __NAMESPACE__.AuthExceptionCodec; +export type MoreOptionsType = __NAMESPACE__.OtherCommonUnionType; +export const MoreOptions = __NAMESPACE__.OtherCommonUnion; +export type IMoreOptionsWithOption1 = __NAMESPACE__.IOtherCommonUnionWithOption1; +export type IMoreOptionsWithOption2 = __NAMESPACE__.IOtherCommonUnionWithOption2; +export type MoreOptionsArgs = __NAMESPACE__.OtherCommonUnionArgs; +export type IMoreOptionsWithOption1Args = __NAMESPACE__.IOtherCommonUnionWithOption1Args; +export type IMoreOptionsWithOption2Args = __NAMESPACE__.IOtherCommonUnionWithOption2Args; +export const MoreOptionsCodec = __NAMESPACE__.OtherCommonUnionCodec; diff --git a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/operation/typedefs.ts b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/operation/typedefs.ts index 4769465b..119d0f0f 100644 --- a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/operation/typedefs.ts +++ b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/operation/typedefs.ts @@ -5,12 +5,12 @@ */ import * as com_test_exceptions from "./../exceptions"; import * as __NAMESPACE__ from "./."; -export import IJankyOperation = com_test_exceptions.IInvalidOperation; -export import IJankyOperationArgs = com_test_exceptions.IInvalidOperationArgs; -export import JankyOperation = com_test_exceptions.InvalidOperation; -export import JankyOperationCodec = com_test_exceptions.InvalidOperationCodec; -export import IJankyResult = com_test_exceptions.IInvalidResult; -export import IJankyResultArgs = com_test_exceptions.IInvalidResultArgs; -export import JankyResult = com_test_exceptions.InvalidResult; -export import JankyResultCodec = com_test_exceptions.InvalidResultCodec; -export import SomethingToDo = __NAMESPACE__.Operation; +export type IJankyOperation = com_test_exceptions.IInvalidOperation; +export type IJankyOperationArgs = com_test_exceptions.IInvalidOperationArgs; +export const JankyOperation = com_test_exceptions.InvalidOperation; +export const JankyOperationCodec = com_test_exceptions.InvalidOperationCodec; +export type IJankyResult = com_test_exceptions.IInvalidResult; +export type IJankyResultArgs = com_test_exceptions.IInvalidResultArgs; +export const JankyResult = com_test_exceptions.InvalidResult; +export const JankyResultCodec = com_test_exceptions.InvalidResultCodec; +export const SomethingToDo = __NAMESPACE__.Operation; diff --git a/src/tests/unit/fixtures/thrift-server/generated/com/test/calculator/typedefs.ts b/src/tests/unit/fixtures/thrift-server/generated/com/test/calculator/typedefs.ts index 86551bad..c9fd2582 100644 --- a/src/tests/unit/fixtures/thrift-server/generated/com/test/calculator/typedefs.ts +++ b/src/tests/unit/fixtures/thrift-server/generated/com/test/calculator/typedefs.ts @@ -6,8 +6,8 @@ import * as com_test_operation from "./../operation"; import * as com_test_common from "./../common"; export type MyInteger = number; -export import Operation = com_test_operation.Operation; -export import ICommonStruct = com_test_common.ICommonStruct; -export import ICommonStructArgs = com_test_common.ICommonStructArgs; -export import CommonStruct = com_test_common.CommonStruct; -export import CommonStructCodec = com_test_common.CommonStructCodec; +export const Operation = com_test_operation.Operation; +export type ICommonStruct = com_test_common.ICommonStruct; +export type ICommonStructArgs = com_test_common.ICommonStructArgs; +export const CommonStruct = com_test_common.CommonStruct; +export const CommonStructCodec = com_test_common.CommonStructCodec; diff --git a/src/tests/unit/fixtures/thrift-server/generated/com/test/common/typedefs.ts b/src/tests/unit/fixtures/thrift-server/generated/com/test/common/typedefs.ts index 88e9fdb2..6af3d16e 100644 --- a/src/tests/unit/fixtures/thrift-server/generated/com/test/common/typedefs.ts +++ b/src/tests/unit/fixtures/thrift-server/generated/com/test/common/typedefs.ts @@ -5,20 +5,20 @@ */ import * as com_test_shared from "./../shared"; import * as __NAMESPACE__ from "./."; -export import ICommonStruct = com_test_shared.ISharedStruct; -export import ICommonStructArgs = com_test_shared.ISharedStructArgs; -export import CommonStruct = com_test_shared.SharedStruct; -export import CommonStructCodec = com_test_shared.SharedStructCodec; -export import ICommonUnion = com_test_shared.ISharedUnion; -export import ICommonUnionArgs = com_test_shared.ISharedUnionArgs; -export import CommonUnion = com_test_shared.SharedUnion; -export import CommonUnionCodec = com_test_shared.SharedUnionCodec; -export import COMMON_INT = com_test_shared.SHARED_INT; -export import INotAllowed = __NAMESPACE__.IAuthException; -export import INotAllowedArgs = __NAMESPACE__.IAuthExceptionArgs; -export import NotAllowed = __NAMESPACE__.AuthException; -export import NotAllowedCodec = __NAMESPACE__.AuthExceptionCodec; -export import IMoreOptions = __NAMESPACE__.IOtherCommonUnion; -export import IMoreOptionsArgs = __NAMESPACE__.IOtherCommonUnionArgs; -export import MoreOptions = __NAMESPACE__.OtherCommonUnion; -export import MoreOptionsCodec = __NAMESPACE__.OtherCommonUnionCodec; +export type ICommonStruct = com_test_shared.ISharedStruct; +export type ICommonStructArgs = com_test_shared.ISharedStructArgs; +export const CommonStruct = com_test_shared.SharedStruct; +export const CommonStructCodec = com_test_shared.SharedStructCodec; +export type ICommonUnion = com_test_shared.ISharedUnion; +export type ICommonUnionArgs = com_test_shared.ISharedUnionArgs; +export const CommonUnion = com_test_shared.SharedUnion; +export const CommonUnionCodec = com_test_shared.SharedUnionCodec; +export const COMMON_INT = com_test_shared.SHARED_INT; +export type INotAllowed = __NAMESPACE__.IAuthException; +export type INotAllowedArgs = __NAMESPACE__.IAuthExceptionArgs; +export const NotAllowed = __NAMESPACE__.AuthException; +export const NotAllowedCodec = __NAMESPACE__.AuthExceptionCodec; +export type IMoreOptions = __NAMESPACE__.IOtherCommonUnion; +export type IMoreOptionsArgs = __NAMESPACE__.IOtherCommonUnionArgs; +export const MoreOptions = __NAMESPACE__.OtherCommonUnion; +export const MoreOptionsCodec = __NAMESPACE__.OtherCommonUnionCodec; diff --git a/src/tests/unit/fixtures/thrift-server/generated/com/test/operation/typedefs.ts b/src/tests/unit/fixtures/thrift-server/generated/com/test/operation/typedefs.ts index 4769465b..119d0f0f 100644 --- a/src/tests/unit/fixtures/thrift-server/generated/com/test/operation/typedefs.ts +++ b/src/tests/unit/fixtures/thrift-server/generated/com/test/operation/typedefs.ts @@ -5,12 +5,12 @@ */ import * as com_test_exceptions from "./../exceptions"; import * as __NAMESPACE__ from "./."; -export import IJankyOperation = com_test_exceptions.IInvalidOperation; -export import IJankyOperationArgs = com_test_exceptions.IInvalidOperationArgs; -export import JankyOperation = com_test_exceptions.InvalidOperation; -export import JankyOperationCodec = com_test_exceptions.InvalidOperationCodec; -export import IJankyResult = com_test_exceptions.IInvalidResult; -export import IJankyResultArgs = com_test_exceptions.IInvalidResultArgs; -export import JankyResult = com_test_exceptions.InvalidResult; -export import JankyResultCodec = com_test_exceptions.InvalidResultCodec; -export import SomethingToDo = __NAMESPACE__.Operation; +export type IJankyOperation = com_test_exceptions.IInvalidOperation; +export type IJankyOperationArgs = com_test_exceptions.IInvalidOperationArgs; +export const JankyOperation = com_test_exceptions.InvalidOperation; +export const JankyOperationCodec = com_test_exceptions.InvalidOperationCodec; +export type IJankyResult = com_test_exceptions.IInvalidResult; +export type IJankyResultArgs = com_test_exceptions.IInvalidResultArgs; +export const JankyResult = com_test_exceptions.InvalidResult; +export const JankyResultCodec = com_test_exceptions.InvalidResultCodec; +export const SomethingToDo = __NAMESPACE__.Operation; From a648ba82d879892c95a3eb32a36eba94f68cf44e Mon Sep 17 00:00:00 2001 From: Michael Hayes Date: Fri, 19 Apr 2019 16:26:35 -0700 Subject: [PATCH 2/5] Update union re-export --- src/main/render/thrift-server/typedef.ts | 30 ++++++++++++++++++- .../com/test/common/typedefs.ts | 4 +-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/main/render/thrift-server/typedef.ts b/src/main/render/thrift-server/typedef.ts index 4eef013b..91713800 100644 --- a/src/main/render/thrift-server/typedef.ts +++ b/src/main/render/thrift-server/typedef.ts @@ -134,6 +134,34 @@ function renderClassReexport( } } +function renderUnionReexport( + id: IResolvedIdentifier, + node: TypedefDefinition, + state: IRenderState, +): ts.Statement { + if (id.pathName !== undefined) { + return ts.createTypeAliasDeclaration( + undefined, + tokens(true), + className(node.name.value, state), + undefined, + ts.createTypeReferenceNode(className(id.rawName, state), undefined), + ) + } else { + return ts.createExportDeclaration( + [], + [], + ts.createNamedExports([ + ts.createExportSpecifier( + ts.createIdentifier(`${className(id.rawName, state)}`), + ts.createIdentifier(className(node.name.value, state)), + ), + ]), + undefined, + ) + } +} + function renderToolkitReexport( id: IResolvedIdentifier, definition: StructDefinition | UnionDefinition | ExceptionDefinition, @@ -297,7 +325,7 @@ function renderTypeDefForIdentifier( if (state.options.strictUnions) { return [ renderUnionTypeReexport(resolvedIdentifier, node, state), - renderClassReexport(resolvedIdentifier, node, state), + renderUnionReexport(resolvedIdentifier, node, state), ...renderUnionInterfaceReexports( resolvedIdentifier, definition, diff --git a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/typedefs.ts b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/typedefs.ts index a5f6b64c..998214c4 100644 --- a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/typedefs.ts +++ b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/typedefs.ts @@ -10,7 +10,7 @@ export type ICommonStructArgs = com_test_shared.ISharedStructArgs; export const CommonStruct = com_test_shared.SharedStruct; export const CommonStructCodec = com_test_shared.SharedStructCodec; export type CommonUnionType = com_test_shared.SharedUnionType; -export const CommonUnion = com_test_shared.SharedUnion; +export type CommonUnion = com_test_shared.SharedUnion; export type ICommonUnionWithOption1 = com_test_shared.ISharedUnionWithOption1; export type ICommonUnionWithOption2 = com_test_shared.ISharedUnionWithOption2; export type CommonUnionArgs = com_test_shared.SharedUnionArgs; @@ -23,7 +23,7 @@ export type INotAllowedArgs = __NAMESPACE__.IAuthExceptionArgs; export const NotAllowed = __NAMESPACE__.AuthException; export const NotAllowedCodec = __NAMESPACE__.AuthExceptionCodec; export type MoreOptionsType = __NAMESPACE__.OtherCommonUnionType; -export const MoreOptions = __NAMESPACE__.OtherCommonUnion; +export type MoreOptions = __NAMESPACE__.OtherCommonUnion; export type IMoreOptionsWithOption1 = __NAMESPACE__.IOtherCommonUnionWithOption1; export type IMoreOptionsWithOption2 = __NAMESPACE__.IOtherCommonUnionWithOption2; export type MoreOptionsArgs = __NAMESPACE__.OtherCommonUnionArgs; From d7cea35e63ecae6372b42d4f50234db2dd7d0635 Mon Sep 17 00:00:00 2001 From: Michael Hayes Date: Fri, 19 Apr 2019 16:28:31 -0700 Subject: [PATCH 3/5] Update src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/typedefs.ts --- .../thrift-server/generated-strict/com/test/common/typedefs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/typedefs.ts b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/typedefs.ts index 998214c4..37b9c252 100644 --- a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/typedefs.ts +++ b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/typedefs.ts @@ -1,6 +1,6 @@ /* tslint:disable */ /* - * Autogenerated by @creditkarma/thrift-typescript v3.5.0 + * Autogenerated by @creditkarma/thrift-typescript v{{VERSION}} * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING */ import * as com_test_shared from "./../shared"; From fac86dd91b653af4856ac1135e54d91058aa04a7 Mon Sep 17 00:00:00 2001 From: Michael Hayes Date: Mon, 22 Apr 2019 11:54:56 -0700 Subject: [PATCH 4/5] Fix enum typedefs --- src/main/render/thrift-server/typedef.ts | 20 ++ .../com/test/calculator/Calculator.ts | 7 +- .../generated/com/test/common/typedefs.ts | 1 + .../com/test/shared/SharedService.ts | 156 ++++++++++++++ .../generated/com/test/shared/constants.ts | 4 + .../com/test/calculator/Calculator.ts | 13 +- .../com/test/calculator/typedefs.ts | 1 + .../com/test/common/typedefs.ts | 2 + .../com/test/operation/typedefs.ts | 1 + .../com/test/shared/SharedService.ts | 200 +++++++++++++++++- .../com/test/shared/constants.ts | 4 + .../com/test/calculator/Calculator.ts | 13 +- .../generated/com/test/calculator/typedefs.ts | 1 + .../generated/com/test/common/typedefs.ts | 2 + .../generated/com/test/operation/typedefs.ts | 1 + .../com/test/shared/SharedService.ts | 200 +++++++++++++++++- .../generated/com/test/shared/constants.ts | 4 + src/tests/unit/fixtures/thrift/common.thrift | 1 + src/tests/unit/fixtures/thrift/shared.thrift | 6 + 19 files changed, 630 insertions(+), 7 deletions(-) diff --git a/src/main/render/thrift-server/typedef.ts b/src/main/render/thrift-server/typedef.ts index 91713800..944a6a4d 100644 --- a/src/main/render/thrift-server/typedef.ts +++ b/src/main/render/thrift-server/typedef.ts @@ -374,6 +374,16 @@ function renderTypeDefForIdentifier( ] case SyntaxType.ConstDefinition: + return [ + ts.createVariableStatement( + tokens(true), + createConst( + node.name.value, + undefined, + ts.createIdentifier(resolvedIdentifier.fullName), + ), + ), + ] case SyntaxType.EnumDefinition: return [ ts.createVariableStatement( @@ -384,6 +394,16 @@ function renderTypeDefForIdentifier( ts.createIdentifier(resolvedIdentifier.fullName), ), ), + ts.createTypeAliasDeclaration( + undefined, + tokens(true), + node.name.value, + undefined, + ts.createTypeReferenceNode( + resolvedIdentifier.fullName, + undefined, + ), + ), ] default: diff --git a/src/tests/unit/fixtures/apache/generated/com/test/calculator/Calculator.ts b/src/tests/unit/fixtures/apache/generated/com/test/calculator/Calculator.ts index f8fcecbe..7d8e6aba 100644 --- a/src/tests/unit/fixtures/apache/generated/com/test/calculator/Calculator.ts +++ b/src/tests/unit/fixtures/apache/generated/com/test/calculator/Calculator.ts @@ -2345,7 +2345,8 @@ export class Processor extends com_test_shared.SharedService.Processor { constructor(handler: IHandler) { super({ getStruct: handler.getStruct, - getUnion: handler.getUnion + getUnion: handler.getUnion, + getEnum: handler.getEnum }); this._handler = handler; } @@ -2363,6 +2364,10 @@ export class Processor extends com_test_shared.SharedService.Processor { this.process_getUnion(requestId, input, output); return; } + case "process_getEnum": { + this.process_getEnum(requestId, input, output); + return; + } case "process_ping": { this.process_ping(requestId, input, output); return; diff --git a/src/tests/unit/fixtures/apache/generated/com/test/common/typedefs.ts b/src/tests/unit/fixtures/apache/generated/com/test/common/typedefs.ts index 5db587a5..9c88bc68 100644 --- a/src/tests/unit/fixtures/apache/generated/com/test/common/typedefs.ts +++ b/src/tests/unit/fixtures/apache/generated/com/test/common/typedefs.ts @@ -7,6 +7,7 @@ import * as com_test_shared from "./../shared"; import * as __NAMESPACE__ from "./."; export import CommonStruct = com_test_shared.SharedStruct; export import CommonUnion = com_test_shared.SharedUnion; +export import CommonEnum = com_test_shared.SharedEnum; export import COMMON_INT = com_test_shared.SHARED_INT; export import NotAllowed = __NAMESPACE__.AuthException; export import MoreOptions = __NAMESPACE__.OtherCommonUnion; diff --git a/src/tests/unit/fixtures/apache/generated/com/test/shared/SharedService.ts b/src/tests/unit/fixtures/apache/generated/com/test/shared/SharedService.ts index 9d998fe1..1bae25ee 100644 --- a/src/tests/unit/fixtures/apache/generated/com/test/shared/SharedService.ts +++ b/src/tests/unit/fixtures/apache/generated/com/test/shared/SharedService.ts @@ -123,6 +123,37 @@ export class GetUnionArgs { } } } +export interface IGetEnumArgsArgs { +} +export class GetEnumArgs { + constructor() { + } + public write(output: thrift.TProtocol): void { + output.writeStructBegin("GetEnumArgs"); + output.writeFieldStop(); + output.writeStructEnd(); + return; + } + public static read(input: thrift.TProtocol): GetEnumArgs { + input.readStructBegin(); + while (true) { + const ret: thrift.TField = input.readFieldBegin(); + const fieldType: thrift.Thrift.Type = ret.ftype; + const fieldId: number = ret.fid; + if (fieldType === thrift.Thrift.Type.STOP) { + break; + } + switch (fieldId) { + default: { + input.skip(fieldType); + } + } + input.readFieldEnd(); + } + input.readStructEnd(); + return new GetEnumArgs(); + } +} export interface IGetStructResultArgs { success?: __NAMESPACE__.SharedStruct; } @@ -225,6 +256,57 @@ export class GetUnionResult { return new GetUnionResult(_args); } } +export interface IGetEnumResultArgs { + success?: __NAMESPACE__.SharedEnum; +} +export class GetEnumResult { + public success?: __NAMESPACE__.SharedEnum; + constructor(args?: IGetEnumResultArgs) { + if (args != null && args.success != null) { + this.success = args.success; + } + } + public write(output: thrift.TProtocol): void { + output.writeStructBegin("GetEnumResult"); + if (this.success != null) { + output.writeFieldBegin("success", thrift.Thrift.Type.I32, 0); + output.writeI32(this.success); + output.writeFieldEnd(); + } + output.writeFieldStop(); + output.writeStructEnd(); + return; + } + public static read(input: thrift.TProtocol): GetEnumResult { + input.readStructBegin(); + let _args: any = {}; + while (true) { + const ret: thrift.TField = input.readFieldBegin(); + const fieldType: thrift.Thrift.Type = ret.ftype; + const fieldId: number = ret.fid; + if (fieldType === thrift.Thrift.Type.STOP) { + break; + } + switch (fieldId) { + case 0: + if (fieldType === thrift.Thrift.Type.I32) { + const value_5: __NAMESPACE__.SharedEnum = input.readI32(); + _args.success = value_5; + } + else { + input.skip(fieldType); + } + break; + default: { + input.skip(fieldType); + } + } + input.readFieldEnd(); + } + input.readStructEnd(); + return new GetEnumResult(_args); + } +} export class Client { public _seqid: number; public _reqs: { @@ -271,6 +353,21 @@ export class Client { this.send_getUnion(index, requestId); }); } + public getEnum(): Promise<__NAMESPACE__.SharedEnum> { + const requestId: number = this.incrementSeqId(); + return new Promise<__NAMESPACE__.SharedEnum>((resolve, reject): void => { + this._reqs[requestId] = (error, result) => { + delete this._reqs[requestId]; + if (error != null) { + reject(error); + } + else { + resolve(result); + } + }; + this.send_getEnum(requestId); + }); + } public send_getStruct(key: number, requestId: number): void { const output: thrift.TProtocol = new this.protocol(this.output); output.writeMessageBegin("getStruct", thrift.Thrift.MessageType.CALL, requestId); @@ -289,6 +386,15 @@ export class Client { this.output.flush(); return; } + public send_getEnum(requestId: number): void { + const output: thrift.TProtocol = new this.protocol(this.output); + output.writeMessageBegin("getEnum", thrift.Thrift.MessageType.CALL, requestId); + const args: GetEnumArgs = new GetEnumArgs(); + args.write(output); + output.writeMessageEnd(); + this.output.flush(); + return; + } public recv_getStruct(input: thrift.TProtocol, mtype: thrift.Thrift.MessageType, requestId: number): void { const noop = (): any => null; const callback = this._reqs[requestId] || noop; @@ -329,10 +435,31 @@ export class Client { } } } + public recv_getEnum(input: thrift.TProtocol, mtype: thrift.Thrift.MessageType, requestId: number): void { + const noop = (): any => null; + const callback = this._reqs[requestId] || noop; + if (mtype === thrift.Thrift.MessageType.EXCEPTION) { + const x: thrift.Thrift.TApplicationException = new thrift.Thrift.TApplicationException(); + x.read(input); + input.readMessageEnd(); + return callback(x); + } + else { + const result: GetEnumResult = GetEnumResult.read(input); + input.readMessageEnd(); + if (result.success != null) { + return callback(undefined, result.success); + } + else { + return callback(new thrift.Thrift.TApplicationException(thrift.Thrift.TApplicationExceptionType.UNKNOWN, "getEnum failed: unknown result")); + } + } + } } export interface IHandler { getStruct(key: number): __NAMESPACE__.SharedStruct | Promise<__NAMESPACE__.SharedStruct>; getUnion(index: number): __NAMESPACE__.SharedUnion | Promise<__NAMESPACE__.SharedUnion>; + getEnum(): __NAMESPACE__.SharedEnum | Promise<__NAMESPACE__.SharedEnum>; } export class Processor { public _handler: IHandler; @@ -353,6 +480,10 @@ export class Processor { this.process_getUnion(requestId, input, output); return; } + case "process_getEnum": { + this.process_getEnum(requestId, input, output); + return; + } default: { input.skip(thrift.Thrift.Type.STRUCT); input.readMessageEnd(); @@ -418,4 +549,29 @@ export class Processor { return; }); } + public process_getEnum(requestId: number, input: thrift.TProtocol, output: thrift.TProtocol): void { + new Promise<__NAMESPACE__.SharedEnum>((resolve, reject): void => { + try { + input.readMessageEnd(); + resolve(this._handler.getEnum()); + } + catch (err) { + reject(err); + } + }).then((data: __NAMESPACE__.SharedEnum): void => { + const result: GetEnumResult = new GetEnumResult({ success: data }); + output.writeMessageBegin("getEnum", thrift.Thrift.MessageType.REPLY, requestId); + result.write(output); + output.writeMessageEnd(); + output.flush(); + return; + }).catch((err: Error): void => { + const result: thrift.Thrift.TApplicationException = new thrift.Thrift.TApplicationException(thrift.Thrift.TApplicationExceptionType.UNKNOWN, err.message); + output.writeMessageBegin("getEnum", thrift.Thrift.MessageType.EXCEPTION, requestId); + result.write(output); + output.writeMessageEnd(); + output.flush(); + return; + }); + } } diff --git a/src/tests/unit/fixtures/apache/generated/com/test/shared/constants.ts b/src/tests/unit/fixtures/apache/generated/com/test/shared/constants.ts index 9bdc1290..6aaaa419 100644 --- a/src/tests/unit/fixtures/apache/generated/com/test/shared/constants.ts +++ b/src/tests/unit/fixtures/apache/generated/com/test/shared/constants.ts @@ -3,4 +3,8 @@ * Autogenerated by @creditkarma/thrift-typescript v{{VERSION}} * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING */ +export enum SharedEnum { + value1 = 0, + value2 = 1 +} export const SHARED_INT: number = 45; diff --git a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/calculator/Calculator.ts b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/calculator/Calculator.ts index fdcf917f..b67855ca 100644 --- a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/calculator/Calculator.ts +++ b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/calculator/Calculator.ts @@ -19,6 +19,10 @@ export const methodAnnotations: thrift.IMethodAnnotations = { annotations: {}, fieldAnnotations: {} }, + getEnum: { + annotations: {}, + fieldAnnotations: {} + }, ping: { annotations: {}, fieldAnnotations: {} @@ -76,7 +80,7 @@ export const methodAnnotations: thrift.IMethodAnnotations = { fieldAnnotations: {} } }; -export const methodNames: Array = ["getStruct", "getUnion", "ping", "add", "addInt64", "addWithContext", "calculate", "echoBinary", "echoString", "checkName", "checkOptional", "mapOneList", "mapValues", "listToMap", "fetchThing", "zip"]; +export const methodNames: Array = ["getStruct", "getUnion", "getEnum", "ping", "add", "addInt64", "addWithContext", "calculate", "echoBinary", "echoString", "checkName", "checkOptional", "mapOneList", "mapValues", "listToMap", "fetchThing", "zip"]; export interface IPing__Args { __name: "Ping__Args"; } @@ -3117,7 +3121,8 @@ export class Processor extends com_test_shared.SharedService.Proc constructor(handler: IHandler) { super({ getStruct: handler.getStruct, - getUnion: handler.getUnion + getUnion: handler.getUnion, + getEnum: handler.getEnum }); this._handler = handler; } @@ -3136,6 +3141,10 @@ export class Processor extends com_test_shared.SharedService.Proc resolve(this.process_getUnion(requestId, input, output, context)); break; } + case "process_getEnum": { + resolve(this.process_getEnum(requestId, input, output, context)); + break; + } case "process_ping": { resolve(this.process_ping(requestId, input, output, context)); break; diff --git a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/calculator/typedefs.ts b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/calculator/typedefs.ts index c9fd2582..608c0838 100644 --- a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/calculator/typedefs.ts +++ b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/calculator/typedefs.ts @@ -7,6 +7,7 @@ import * as com_test_operation from "./../operation"; import * as com_test_common from "./../common"; export type MyInteger = number; export const Operation = com_test_operation.Operation; +export type Operation = com_test_operation.Operation; export type ICommonStruct = com_test_common.ICommonStruct; export type ICommonStructArgs = com_test_common.ICommonStructArgs; export const CommonStruct = com_test_common.CommonStruct; diff --git a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/typedefs.ts b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/typedefs.ts index 37b9c252..fb14ae4d 100644 --- a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/typedefs.ts +++ b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/typedefs.ts @@ -17,6 +17,8 @@ export type CommonUnionArgs = com_test_shared.SharedUnionArgs; export type ICommonUnionWithOption1Args = com_test_shared.ISharedUnionWithOption1Args; export type ICommonUnionWithOption2Args = com_test_shared.ISharedUnionWithOption2Args; export const CommonUnionCodec = com_test_shared.SharedUnionCodec; +export const CommonEnum = com_test_shared.SharedEnum; +export type CommonEnum = com_test_shared.SharedEnum; export const COMMON_INT = com_test_shared.SHARED_INT; export type INotAllowed = __NAMESPACE__.IAuthException; export type INotAllowedArgs = __NAMESPACE__.IAuthExceptionArgs; diff --git a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/operation/typedefs.ts b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/operation/typedefs.ts index 119d0f0f..bb5370b5 100644 --- a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/operation/typedefs.ts +++ b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/operation/typedefs.ts @@ -14,3 +14,4 @@ export type IJankyResultArgs = com_test_exceptions.IInvalidResultArgs; export const JankyResult = com_test_exceptions.InvalidResult; export const JankyResultCodec = com_test_exceptions.InvalidResultCodec; export const SomethingToDo = __NAMESPACE__.Operation; +export type SomethingToDo = __NAMESPACE__.Operation; diff --git a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/shared/SharedService.ts b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/shared/SharedService.ts index 2f16ed24..11a8b13e 100644 --- a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/shared/SharedService.ts +++ b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/shared/SharedService.ts @@ -15,9 +15,13 @@ export const methodAnnotations: thrift.IMethodAnnotations = { getUnion: { annotations: {}, fieldAnnotations: {} + }, + getEnum: { + annotations: {}, + fieldAnnotations: {} } }; -export const methodNames: Array = ["getStruct", "getUnion"]; +export const methodNames: Array = ["getStruct", "getUnion", "getEnum"]; export interface IGetStruct__Args { __name: "GetStruct__Args"; key: number; @@ -194,6 +198,57 @@ export class GetUnion__Args extends thrift.StructLike implements IGetUnion__Args return GetUnion__ArgsCodec.encode(this, output); } } +export interface IGetEnum__Args { + __name: "GetEnum__Args"; +} +export interface IGetEnum__ArgsArgs { +} +export const GetEnum__ArgsCodec: thrift.IStructCodec = { + encode(args: IGetEnum__ArgsArgs, output: thrift.TProtocol): void { + output.writeStructBegin("GetEnum__Args"); + output.writeFieldStop(); + output.writeStructEnd(); + return; + }, + decode(input: thrift.TProtocol): IGetEnum__Args { + input.readStructBegin(); + while (true) { + const ret: thrift.IThriftField = input.readFieldBegin(); + const fieldType: thrift.TType = ret.fieldType; + const fieldId: number = ret.fieldId; + if (fieldType === thrift.TType.STOP) { + break; + } + switch (fieldId) { + default: { + input.skip(fieldType); + } + } + input.readFieldEnd(); + } + input.readStructEnd(); + return { + __name: "GetEnum__Args" + }; + } +}; +export class GetEnum__Args extends thrift.StructLike implements IGetEnum__Args { + public readonly __name = "GetEnum__Args"; + public readonly _annotations: thrift.IThriftAnnotations = {}; + public readonly _fieldAnnotations: thrift.IFieldAnnotations = {}; + constructor(args: IGetEnum__ArgsArgs = {}) { + super(); + } + public static read(input: thrift.TProtocol): GetEnum__Args { + return new GetEnum__Args(GetEnum__ArgsCodec.decode(input)); + } + public static write(args: IGetEnum__ArgsArgs, output: thrift.TProtocol): void { + return GetEnum__ArgsCodec.encode(args, output); + } + public write(output: thrift.TProtocol): void { + return GetEnum__ArgsCodec.encode(this, output); + } +} export interface IGetStruct__Result { __name: "GetStruct__Result"; success?: __NAMESPACE__.ISharedStruct; @@ -348,6 +403,83 @@ export class GetUnion__Result extends thrift.StructLike implements IGetUnion__Re return GetUnion__ResultCodec.encode(this, output); } } +export interface IGetEnum__Result { + __name: "GetEnum__Result"; + success?: __NAMESPACE__.SharedEnum; +} +export interface IGetEnum__ResultArgs { + success?: __NAMESPACE__.SharedEnum; +} +export const GetEnum__ResultCodec: thrift.IStructCodec = { + encode(args: IGetEnum__ResultArgs, output: thrift.TProtocol): void { + const obj = { + success: args.success + }; + output.writeStructBegin("GetEnum__Result"); + if (obj.success != null) { + output.writeFieldBegin("success", thrift.TType.I32, 0); + output.writeI32(obj.success); + output.writeFieldEnd(); + } + output.writeFieldStop(); + output.writeStructEnd(); + return; + }, + decode(input: thrift.TProtocol): IGetEnum__Result { + let _args: any = {}; + input.readStructBegin(); + while (true) { + const ret: thrift.IThriftField = input.readFieldBegin(); + const fieldType: thrift.TType = ret.fieldType; + const fieldId: number = ret.fieldId; + if (fieldType === thrift.TType.STOP) { + break; + } + switch (fieldId) { + case 0: + if (fieldType === thrift.TType.I32) { + const value_9: __NAMESPACE__.SharedEnum = input.readI32(); + _args.success = value_9; + } + else { + input.skip(fieldType); + } + break; + default: { + input.skip(fieldType); + } + } + input.readFieldEnd(); + } + input.readStructEnd(); + return { + __name: "GetEnum__Result", + success: _args.success + }; + } +}; +export class GetEnum__Result extends thrift.StructLike implements IGetEnum__Result { + public success?: __NAMESPACE__.SharedEnum; + public readonly __name = "GetEnum__Result"; + public readonly _annotations: thrift.IThriftAnnotations = {}; + public readonly _fieldAnnotations: thrift.IFieldAnnotations = {}; + constructor(args: IGetEnum__ResultArgs = {}) { + super(); + if (args.success != null) { + const value_10: __NAMESPACE__.SharedEnum = args.success; + this.success = value_10; + } + } + public static read(input: thrift.TProtocol): GetEnum__Result { + return new GetEnum__Result(GetEnum__ResultCodec.decode(input)); + } + public static write(args: IGetEnum__ResultArgs, output: thrift.TProtocol): void { + return GetEnum__ResultCodec.encode(args, output); + } + public write(output: thrift.TProtocol): void { + return GetEnum__ResultCodec.encode(this, output); + } +} export class Client extends thrift.ThriftClient { public static readonly serviceName: string = serviceName; public static readonly annotations: thrift.IThriftAnnotations = annotations; @@ -433,10 +565,49 @@ export class Client extends thrift.ThriftClient { } }); } + public getEnum(context?: Context): Promise<__NAMESPACE__.SharedEnum> { + const writer: thrift.TTransport = new this.transport(); + const output: thrift.TProtocol = new this.protocol(writer); + output.writeMessageBegin("getEnum", thrift.MessageType.CALL, this.incrementRequestId()); + const args: IGetEnum__ArgsArgs = {}; + GetEnum__ArgsCodec.encode(args, output); + output.writeMessageEnd(); + return this.connection.send(writer.flush(), context).then((data: Buffer) => { + const reader: thrift.TTransport = this.transport.receiver(data); + const input: thrift.TProtocol = new this.protocol(reader); + try { + const { fieldName: fieldName, messageType: messageType }: thrift.IThriftMessage = input.readMessageBegin(); + if (fieldName === "getEnum") { + if (messageType === thrift.MessageType.EXCEPTION) { + const err: thrift.TApplicationException = thrift.TApplicationExceptionCodec.decode(input); + input.readMessageEnd(); + return Promise.reject(err); + } + else { + const result: IGetEnum__Result = GetEnum__ResultCodec.decode(input); + input.readMessageEnd(); + if (result.success != null) { + return Promise.resolve(result.success); + } + else { + return Promise.reject(new thrift.TApplicationException(thrift.TApplicationExceptionType.UNKNOWN, "getEnum failed: unknown result")); + } + } + } + else { + return Promise.reject(new thrift.TApplicationException(thrift.TApplicationExceptionType.WRONG_METHOD_NAME, "Received a response to an unknown RPC function: " + fieldName)); + } + } + catch (err) { + return Promise.reject(err); + } + }); + } } export interface IHandler { getStruct(key: number, context?: Context): __NAMESPACE__.ISharedStructArgs | Promise<__NAMESPACE__.ISharedStructArgs>; getUnion(index: number, context?: Context): __NAMESPACE__.SharedUnionArgs | Promise<__NAMESPACE__.SharedUnionArgs>; + getEnum(context?: Context): __NAMESPACE__.SharedEnum | Promise<__NAMESPACE__.SharedEnum>; } export class Processor extends thrift.ThriftProcessor> { protected readonly _handler: IHandler; @@ -467,6 +638,10 @@ export class Processor extends thrift.ThriftProcessor extends thrift.ThriftProcessor { + return new Promise<__NAMESPACE__.SharedEnum>((resolve, reject): void => { + try { + input.readMessageEnd(); + resolve(this._handler.getEnum(context)); + } + catch (err) { + reject(err); + } + }).then((data: __NAMESPACE__.SharedEnum): Buffer => { + const result: IGetEnum__ResultArgs = { success: data }; + output.writeMessageBegin("getEnum", thrift.MessageType.REPLY, requestId); + GetEnum__ResultCodec.encode(result, output); + output.writeMessageEnd(); + return output.flush(); + }).catch((err: Error): Buffer => { + const result: thrift.TApplicationException = new thrift.TApplicationException(thrift.TApplicationExceptionType.UNKNOWN, err.message); + output.writeMessageBegin("getEnum", thrift.MessageType.EXCEPTION, requestId); + thrift.TApplicationExceptionCodec.encode(result, output); + output.writeMessageEnd(); + return output.flush(); + }); + } } diff --git a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/shared/constants.ts b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/shared/constants.ts index 9bdc1290..6aaaa419 100644 --- a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/shared/constants.ts +++ b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/shared/constants.ts @@ -3,4 +3,8 @@ * Autogenerated by @creditkarma/thrift-typescript v{{VERSION}} * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING */ +export enum SharedEnum { + value1 = 0, + value2 = 1 +} export const SHARED_INT: number = 45; diff --git a/src/tests/unit/fixtures/thrift-server/generated/com/test/calculator/Calculator.ts b/src/tests/unit/fixtures/thrift-server/generated/com/test/calculator/Calculator.ts index df7bcd00..b394c613 100644 --- a/src/tests/unit/fixtures/thrift-server/generated/com/test/calculator/Calculator.ts +++ b/src/tests/unit/fixtures/thrift-server/generated/com/test/calculator/Calculator.ts @@ -19,6 +19,10 @@ export const methodAnnotations: thrift.IMethodAnnotations = { annotations: {}, fieldAnnotations: {} }, + getEnum: { + annotations: {}, + fieldAnnotations: {} + }, ping: { annotations: {}, fieldAnnotations: {} @@ -76,7 +80,7 @@ export const methodAnnotations: thrift.IMethodAnnotations = { fieldAnnotations: {} } }; -export const methodNames: Array = ["getStruct", "getUnion", "ping", "add", "addInt64", "addWithContext", "calculate", "echoBinary", "echoString", "checkName", "checkOptional", "mapOneList", "mapValues", "listToMap", "fetchThing", "zip"]; +export const methodNames: Array = ["getStruct", "getUnion", "getEnum", "ping", "add", "addInt64", "addWithContext", "calculate", "echoBinary", "echoString", "checkName", "checkOptional", "mapOneList", "mapValues", "listToMap", "fetchThing", "zip"]; export interface IPing__Args { __name: "Ping__Args"; } @@ -3117,7 +3121,8 @@ export class Processor extends com_test_shared.SharedService.Proc constructor(handler: IHandler) { super({ getStruct: handler.getStruct, - getUnion: handler.getUnion + getUnion: handler.getUnion, + getEnum: handler.getEnum }); this._handler = handler; } @@ -3136,6 +3141,10 @@ export class Processor extends com_test_shared.SharedService.Proc resolve(this.process_getUnion(requestId, input, output, context)); break; } + case "process_getEnum": { + resolve(this.process_getEnum(requestId, input, output, context)); + break; + } case "process_ping": { resolve(this.process_ping(requestId, input, output, context)); break; diff --git a/src/tests/unit/fixtures/thrift-server/generated/com/test/calculator/typedefs.ts b/src/tests/unit/fixtures/thrift-server/generated/com/test/calculator/typedefs.ts index c9fd2582..608c0838 100644 --- a/src/tests/unit/fixtures/thrift-server/generated/com/test/calculator/typedefs.ts +++ b/src/tests/unit/fixtures/thrift-server/generated/com/test/calculator/typedefs.ts @@ -7,6 +7,7 @@ import * as com_test_operation from "./../operation"; import * as com_test_common from "./../common"; export type MyInteger = number; export const Operation = com_test_operation.Operation; +export type Operation = com_test_operation.Operation; export type ICommonStruct = com_test_common.ICommonStruct; export type ICommonStructArgs = com_test_common.ICommonStructArgs; export const CommonStruct = com_test_common.CommonStruct; diff --git a/src/tests/unit/fixtures/thrift-server/generated/com/test/common/typedefs.ts b/src/tests/unit/fixtures/thrift-server/generated/com/test/common/typedefs.ts index 6af3d16e..80539a23 100644 --- a/src/tests/unit/fixtures/thrift-server/generated/com/test/common/typedefs.ts +++ b/src/tests/unit/fixtures/thrift-server/generated/com/test/common/typedefs.ts @@ -13,6 +13,8 @@ export type ICommonUnion = com_test_shared.ISharedUnion; export type ICommonUnionArgs = com_test_shared.ISharedUnionArgs; export const CommonUnion = com_test_shared.SharedUnion; export const CommonUnionCodec = com_test_shared.SharedUnionCodec; +export const CommonEnum = com_test_shared.SharedEnum; +export type CommonEnum = com_test_shared.SharedEnum; export const COMMON_INT = com_test_shared.SHARED_INT; export type INotAllowed = __NAMESPACE__.IAuthException; export type INotAllowedArgs = __NAMESPACE__.IAuthExceptionArgs; diff --git a/src/tests/unit/fixtures/thrift-server/generated/com/test/operation/typedefs.ts b/src/tests/unit/fixtures/thrift-server/generated/com/test/operation/typedefs.ts index 119d0f0f..bb5370b5 100644 --- a/src/tests/unit/fixtures/thrift-server/generated/com/test/operation/typedefs.ts +++ b/src/tests/unit/fixtures/thrift-server/generated/com/test/operation/typedefs.ts @@ -14,3 +14,4 @@ export type IJankyResultArgs = com_test_exceptions.IInvalidResultArgs; export const JankyResult = com_test_exceptions.InvalidResult; export const JankyResultCodec = com_test_exceptions.InvalidResultCodec; export const SomethingToDo = __NAMESPACE__.Operation; +export type SomethingToDo = __NAMESPACE__.Operation; diff --git a/src/tests/unit/fixtures/thrift-server/generated/com/test/shared/SharedService.ts b/src/tests/unit/fixtures/thrift-server/generated/com/test/shared/SharedService.ts index 724a3803..a28855bb 100644 --- a/src/tests/unit/fixtures/thrift-server/generated/com/test/shared/SharedService.ts +++ b/src/tests/unit/fixtures/thrift-server/generated/com/test/shared/SharedService.ts @@ -15,9 +15,13 @@ export const methodAnnotations: thrift.IMethodAnnotations = { getUnion: { annotations: {}, fieldAnnotations: {} + }, + getEnum: { + annotations: {}, + fieldAnnotations: {} } }; -export const methodNames: Array = ["getStruct", "getUnion"]; +export const methodNames: Array = ["getStruct", "getUnion", "getEnum"]; export interface IGetStruct__Args { __name: "GetStruct__Args"; key: number; @@ -194,6 +198,57 @@ export class GetUnion__Args extends thrift.StructLike implements IGetUnion__Args return GetUnion__ArgsCodec.encode(this, output); } } +export interface IGetEnum__Args { + __name: "GetEnum__Args"; +} +export interface IGetEnum__ArgsArgs { +} +export const GetEnum__ArgsCodec: thrift.IStructCodec = { + encode(args: IGetEnum__ArgsArgs, output: thrift.TProtocol): void { + output.writeStructBegin("GetEnum__Args"); + output.writeFieldStop(); + output.writeStructEnd(); + return; + }, + decode(input: thrift.TProtocol): IGetEnum__Args { + input.readStructBegin(); + while (true) { + const ret: thrift.IThriftField = input.readFieldBegin(); + const fieldType: thrift.TType = ret.fieldType; + const fieldId: number = ret.fieldId; + if (fieldType === thrift.TType.STOP) { + break; + } + switch (fieldId) { + default: { + input.skip(fieldType); + } + } + input.readFieldEnd(); + } + input.readStructEnd(); + return { + __name: "GetEnum__Args" + }; + } +}; +export class GetEnum__Args extends thrift.StructLike implements IGetEnum__Args { + public readonly __name = "GetEnum__Args"; + public readonly _annotations: thrift.IThriftAnnotations = {}; + public readonly _fieldAnnotations: thrift.IFieldAnnotations = {}; + constructor(args: IGetEnum__ArgsArgs = {}) { + super(); + } + public static read(input: thrift.TProtocol): GetEnum__Args { + return new GetEnum__Args(GetEnum__ArgsCodec.decode(input)); + } + public static write(args: IGetEnum__ArgsArgs, output: thrift.TProtocol): void { + return GetEnum__ArgsCodec.encode(args, output); + } + public write(output: thrift.TProtocol): void { + return GetEnum__ArgsCodec.encode(this, output); + } +} export interface IGetStruct__Result { __name: "GetStruct__Result"; success?: __NAMESPACE__.ISharedStruct; @@ -348,6 +403,83 @@ export class GetUnion__Result extends thrift.StructLike implements IGetUnion__Re return GetUnion__ResultCodec.encode(this, output); } } +export interface IGetEnum__Result { + __name: "GetEnum__Result"; + success?: __NAMESPACE__.SharedEnum; +} +export interface IGetEnum__ResultArgs { + success?: __NAMESPACE__.SharedEnum; +} +export const GetEnum__ResultCodec: thrift.IStructCodec = { + encode(args: IGetEnum__ResultArgs, output: thrift.TProtocol): void { + const obj = { + success: args.success + }; + output.writeStructBegin("GetEnum__Result"); + if (obj.success != null) { + output.writeFieldBegin("success", thrift.TType.I32, 0); + output.writeI32(obj.success); + output.writeFieldEnd(); + } + output.writeFieldStop(); + output.writeStructEnd(); + return; + }, + decode(input: thrift.TProtocol): IGetEnum__Result { + let _args: any = {}; + input.readStructBegin(); + while (true) { + const ret: thrift.IThriftField = input.readFieldBegin(); + const fieldType: thrift.TType = ret.fieldType; + const fieldId: number = ret.fieldId; + if (fieldType === thrift.TType.STOP) { + break; + } + switch (fieldId) { + case 0: + if (fieldType === thrift.TType.I32) { + const value_9: __NAMESPACE__.SharedEnum = input.readI32(); + _args.success = value_9; + } + else { + input.skip(fieldType); + } + break; + default: { + input.skip(fieldType); + } + } + input.readFieldEnd(); + } + input.readStructEnd(); + return { + __name: "GetEnum__Result", + success: _args.success + }; + } +}; +export class GetEnum__Result extends thrift.StructLike implements IGetEnum__Result { + public success?: __NAMESPACE__.SharedEnum; + public readonly __name = "GetEnum__Result"; + public readonly _annotations: thrift.IThriftAnnotations = {}; + public readonly _fieldAnnotations: thrift.IFieldAnnotations = {}; + constructor(args: IGetEnum__ResultArgs = {}) { + super(); + if (args.success != null) { + const value_10: __NAMESPACE__.SharedEnum = args.success; + this.success = value_10; + } + } + public static read(input: thrift.TProtocol): GetEnum__Result { + return new GetEnum__Result(GetEnum__ResultCodec.decode(input)); + } + public static write(args: IGetEnum__ResultArgs, output: thrift.TProtocol): void { + return GetEnum__ResultCodec.encode(args, output); + } + public write(output: thrift.TProtocol): void { + return GetEnum__ResultCodec.encode(this, output); + } +} export class Client extends thrift.ThriftClient { public static readonly serviceName: string = serviceName; public static readonly annotations: thrift.IThriftAnnotations = annotations; @@ -433,10 +565,49 @@ export class Client extends thrift.ThriftClient { } }); } + public getEnum(context?: Context): Promise<__NAMESPACE__.SharedEnum> { + const writer: thrift.TTransport = new this.transport(); + const output: thrift.TProtocol = new this.protocol(writer); + output.writeMessageBegin("getEnum", thrift.MessageType.CALL, this.incrementRequestId()); + const args: IGetEnum__ArgsArgs = {}; + GetEnum__ArgsCodec.encode(args, output); + output.writeMessageEnd(); + return this.connection.send(writer.flush(), context).then((data: Buffer) => { + const reader: thrift.TTransport = this.transport.receiver(data); + const input: thrift.TProtocol = new this.protocol(reader); + try { + const { fieldName: fieldName, messageType: messageType }: thrift.IThriftMessage = input.readMessageBegin(); + if (fieldName === "getEnum") { + if (messageType === thrift.MessageType.EXCEPTION) { + const err: thrift.TApplicationException = thrift.TApplicationExceptionCodec.decode(input); + input.readMessageEnd(); + return Promise.reject(err); + } + else { + const result: IGetEnum__Result = GetEnum__ResultCodec.decode(input); + input.readMessageEnd(); + if (result.success != null) { + return Promise.resolve(result.success); + } + else { + return Promise.reject(new thrift.TApplicationException(thrift.TApplicationExceptionType.UNKNOWN, "getEnum failed: unknown result")); + } + } + } + else { + return Promise.reject(new thrift.TApplicationException(thrift.TApplicationExceptionType.WRONG_METHOD_NAME, "Received a response to an unknown RPC function: " + fieldName)); + } + } + catch (err) { + return Promise.reject(err); + } + }); + } } export interface IHandler { getStruct(key: number, context?: Context): __NAMESPACE__.ISharedStructArgs | Promise<__NAMESPACE__.ISharedStructArgs>; getUnion(index: number, context?: Context): __NAMESPACE__.ISharedUnionArgs | Promise<__NAMESPACE__.ISharedUnionArgs>; + getEnum(context?: Context): __NAMESPACE__.SharedEnum | Promise<__NAMESPACE__.SharedEnum>; } export class Processor extends thrift.ThriftProcessor> { protected readonly _handler: IHandler; @@ -467,6 +638,10 @@ export class Processor extends thrift.ThriftProcessor extends thrift.ThriftProcessor { + return new Promise<__NAMESPACE__.SharedEnum>((resolve, reject): void => { + try { + input.readMessageEnd(); + resolve(this._handler.getEnum(context)); + } + catch (err) { + reject(err); + } + }).then((data: __NAMESPACE__.SharedEnum): Buffer => { + const result: IGetEnum__ResultArgs = { success: data }; + output.writeMessageBegin("getEnum", thrift.MessageType.REPLY, requestId); + GetEnum__ResultCodec.encode(result, output); + output.writeMessageEnd(); + return output.flush(); + }).catch((err: Error): Buffer => { + const result: thrift.TApplicationException = new thrift.TApplicationException(thrift.TApplicationExceptionType.UNKNOWN, err.message); + output.writeMessageBegin("getEnum", thrift.MessageType.EXCEPTION, requestId); + thrift.TApplicationExceptionCodec.encode(result, output); + output.writeMessageEnd(); + return output.flush(); + }); + } } diff --git a/src/tests/unit/fixtures/thrift-server/generated/com/test/shared/constants.ts b/src/tests/unit/fixtures/thrift-server/generated/com/test/shared/constants.ts index 9bdc1290..6aaaa419 100644 --- a/src/tests/unit/fixtures/thrift-server/generated/com/test/shared/constants.ts +++ b/src/tests/unit/fixtures/thrift-server/generated/com/test/shared/constants.ts @@ -3,4 +3,8 @@ * Autogenerated by @creditkarma/thrift-typescript v{{VERSION}} * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING */ +export enum SharedEnum { + value1 = 0, + value2 = 1 +} export const SHARED_INT: number = 45; diff --git a/src/tests/unit/fixtures/thrift/common.thrift b/src/tests/unit/fixtures/thrift/common.thrift index 9102328b..f578f127 100644 --- a/src/tests/unit/fixtures/thrift/common.thrift +++ b/src/tests/unit/fixtures/thrift/common.thrift @@ -15,6 +15,7 @@ include "shared.thrift" typedef shared.SharedStruct CommonStruct typedef shared.SharedUnion CommonUnion +typedef shared.SharedEnum CommonEnum typedef shared.SHARED_INT COMMON_INT exception AuthException { diff --git a/src/tests/unit/fixtures/thrift/shared.thrift b/src/tests/unit/fixtures/thrift/shared.thrift index 9f76b21d..04b1e98b 100644 --- a/src/tests/unit/fixtures/thrift/shared.thrift +++ b/src/tests/unit/fixtures/thrift/shared.thrift @@ -23,7 +23,13 @@ union SharedUnion { 2: string option2 } +enum SharedEnum { + value1 + value2 +} + service SharedService { SharedStruct getStruct(1: i32 key) SharedUnion getUnion(1: i32 index) + SharedEnum getEnum() } From 2546557fc91c54d4cdc9e8e8ddbd53cf5941ad9a Mon Sep 17 00:00:00 2001 From: Michael Hayes Date: Tue, 23 Apr 2019 18:26:20 -0700 Subject: [PATCH 5/5] import typedefs last --- src/main/render/shared/index.ts | 22 +++++++++---------- .../generated/com/test/calculator/index.ts | 2 +- .../apache/generated/com/test/common/index.ts | 2 +- .../com/test/calculator/index.ts | 2 +- .../generated-strict/com/test/common/index.ts | 2 +- .../generated/com/test/calculator/index.ts | 2 +- .../generated/com/test/common/index.ts | 2 +- 7 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/main/render/shared/index.ts b/src/main/render/shared/index.ts index 7e36d095..8e174ae7 100644 --- a/src/main/render/shared/index.ts +++ b/src/main/render/shared/index.ts @@ -21,17 +21,6 @@ export function renderIndex(state: IRenderState): Array { ) } - if (currentNamespace.typedefs.length > 0) { - results.push( - ts.createExportDeclaration( - undefined, - undefined, - undefined, - ts.createLiteral(`./typedefs`), - ), - ) - } - ;[ ...currentNamespace.structs, ...currentNamespace.unions, @@ -73,5 +62,16 @@ export function renderIndex(state: IRenderState): Array { ) }) + if (currentNamespace.typedefs.length > 0) { + results.push( + ts.createExportDeclaration( + undefined, + undefined, + undefined, + ts.createLiteral(`./typedefs`), + ), + ) + } + return results } diff --git a/src/tests/unit/fixtures/apache/generated/com/test/calculator/index.ts b/src/tests/unit/fixtures/apache/generated/com/test/calculator/index.ts index 6c07a209..8749a1b9 100644 --- a/src/tests/unit/fixtures/apache/generated/com/test/calculator/index.ts +++ b/src/tests/unit/fixtures/apache/generated/com/test/calculator/index.ts @@ -4,7 +4,6 @@ * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING */ export * from "./constants"; -export * from "./typedefs"; export * from "./Work"; export * from "./FirstName"; export * from "./LastName"; @@ -12,3 +11,4 @@ export * from "./Choice"; export * from "./NotAGoodIdea"; import * as Calculator from "./Calculator"; export { Calculator as Calculator }; +export * from "./typedefs"; diff --git a/src/tests/unit/fixtures/apache/generated/com/test/common/index.ts b/src/tests/unit/fixtures/apache/generated/com/test/common/index.ts index 230e2538..c2dbe0a0 100644 --- a/src/tests/unit/fixtures/apache/generated/com/test/common/index.ts +++ b/src/tests/unit/fixtures/apache/generated/com/test/common/index.ts @@ -3,6 +3,6 @@ * Autogenerated by @creditkarma/thrift-typescript v{{VERSION}} * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING */ -export * from "./typedefs"; export * from "./OtherCommonUnion"; export * from "./AuthException"; +export * from "./typedefs"; diff --git a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/calculator/index.ts b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/calculator/index.ts index 6c07a209..8749a1b9 100644 --- a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/calculator/index.ts +++ b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/calculator/index.ts @@ -4,7 +4,6 @@ * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING */ export * from "./constants"; -export * from "./typedefs"; export * from "./Work"; export * from "./FirstName"; export * from "./LastName"; @@ -12,3 +11,4 @@ export * from "./Choice"; export * from "./NotAGoodIdea"; import * as Calculator from "./Calculator"; export { Calculator as Calculator }; +export * from "./typedefs"; diff --git a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/index.ts b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/index.ts index 230e2538..c2dbe0a0 100644 --- a/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/index.ts +++ b/src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/index.ts @@ -3,6 +3,6 @@ * Autogenerated by @creditkarma/thrift-typescript v{{VERSION}} * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING */ -export * from "./typedefs"; export * from "./OtherCommonUnion"; export * from "./AuthException"; +export * from "./typedefs"; diff --git a/src/tests/unit/fixtures/thrift-server/generated/com/test/calculator/index.ts b/src/tests/unit/fixtures/thrift-server/generated/com/test/calculator/index.ts index 6c07a209..8749a1b9 100644 --- a/src/tests/unit/fixtures/thrift-server/generated/com/test/calculator/index.ts +++ b/src/tests/unit/fixtures/thrift-server/generated/com/test/calculator/index.ts @@ -4,7 +4,6 @@ * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING */ export * from "./constants"; -export * from "./typedefs"; export * from "./Work"; export * from "./FirstName"; export * from "./LastName"; @@ -12,3 +11,4 @@ export * from "./Choice"; export * from "./NotAGoodIdea"; import * as Calculator from "./Calculator"; export { Calculator as Calculator }; +export * from "./typedefs"; diff --git a/src/tests/unit/fixtures/thrift-server/generated/com/test/common/index.ts b/src/tests/unit/fixtures/thrift-server/generated/com/test/common/index.ts index 230e2538..c2dbe0a0 100644 --- a/src/tests/unit/fixtures/thrift-server/generated/com/test/common/index.ts +++ b/src/tests/unit/fixtures/thrift-server/generated/com/test/common/index.ts @@ -3,6 +3,6 @@ * Autogenerated by @creditkarma/thrift-typescript v{{VERSION}} * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING */ -export * from "./typedefs"; export * from "./OtherCommonUnion"; export * from "./AuthException"; +export * from "./typedefs";