Skip to content

Commit

Permalink
fix: update return types to be loose
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-greene-ck committed Mar 5, 2019
1 parent 76c375f commit 44e4746
Show file tree
Hide file tree
Showing 16 changed files with 274 additions and 404 deletions.
469 changes: 166 additions & 303 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
"clean-all": "npm run clean && rimraf ./node_modules package-lock.json",
"codegen": "node ./dist/main/bin/index.js --sourceDir ./src/tests/integration/thrift --outDir ./src/tests/integration/apache/codegen",
"prebuild": "npm run clean && npm run lint && npm run format",
"build": "tsc",
"build": "npm run build:only",
"build:only": "tsc",
"prebuild:test": "npm run build && npm run codegen",
"build:test": "tsc -p tsconfig.test.json",
"build:test": "npm run build:test:only",
"build:test:only": "tsc -p tsconfig.test.json",
"build:watch": "tsc --watch",
"lint": "tslint --fix 'src/**/*.ts'",
"format": "prettier --write 'src/**/*.ts'",
Expand All @@ -41,7 +43,7 @@
"url": "https://github.com/creditkarma/thrift-typescript"
},
"dependencies": {
"@creditkarma/thrift-parser": "~1.1.4",
"@creditkarma/thrift-parser": "^1.1.5",
"glob": "^7.1.2",
"typescript": "3.2.x"
},
Expand Down
4 changes: 2 additions & 2 deletions src/main/render/shared/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ function funcToMethodReducer(
),
],
ts.createUnionTypeNode([
typeMapping(func.returnType),
typeMapping(func.returnType, true),
ts.createTypeReferenceNode(COMMON_IDENTIFIERS.Promise, [
typeMapping(func.returnType),
typeMapping(func.returnType, true),
]),
]),
func.name.value,
Expand Down
13 changes: 9 additions & 4 deletions src/main/render/thrift-server/service/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import {
renderServiceAnnotationsStaticProperty,
} from '../annotations'

import { strictName, toolkitName } from '../struct/utils'
import { looseName, strictName, toolkitName } from '../struct/utils'

function objectLiteralForServiceFunctions(
node: ThriftStatement,
Expand Down Expand Up @@ -265,7 +265,11 @@ function createProcessFunctionMethod(
createMethodCall(
createMethodCall(
createPromise(
typeNodeForFieldType(funcDef.returnType, state),
typeNodeForFieldType(
funcDef.returnType,
state,
true,
),
createVoidType(),
[
// try {
Expand Down Expand Up @@ -347,6 +351,7 @@ function createProcessFunctionMethod(
typeNodeForFieldType(
funcDef.returnType,
state,
true,
),
),
],
Expand All @@ -362,7 +367,7 @@ function createProcessFunctionMethod(
COMMON_IDENTIFIERS.result,
ts.createTypeReferenceNode(
ts.createIdentifier(
strictName(
looseName(
createStructResultName(
funcDef,
),
Expand Down Expand Up @@ -583,7 +588,7 @@ function createThenForException(
COMMON_IDENTIFIERS.result,
ts.createTypeReferenceNode(
ts.createIdentifier(
strictName(
looseName(
createStructResultName(funcDef),
SyntaxType.StructDefinition,
state,
Expand Down
44 changes: 22 additions & 22 deletions src/tests/unit/fixtures/generated/calculator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3419,7 +3419,7 @@ export namespace Calculator {
export interface ILocalHandler<Context = any> {
ping(context?: Context): void | Promise<void>;
add(num1: number, num2: number, context?: Context): number | Promise<number>;
addInt64(num1: thrift.Int64, num2: thrift.Int64, context?: Context): thrift.Int64 | Promise<thrift.Int64>;
addInt64(num1: thrift.Int64, num2: thrift.Int64, context?: Context): (number | thrift.Int64) | Promise<number | thrift.Int64>;
addWithContext(num1: number, num2: number, context?: Context): number | Promise<number>;
calculate(logid: number, work: IWork, context?: Context): number | Promise<number>;
echoBinary(word: Buffer, context?: Context): string | Promise<string>;
Expand All @@ -3429,7 +3429,7 @@ export namespace Calculator {
mapOneList(arg: Array<number>, context?: Context): Array<number> | Promise<Array<number>>;
mapValues(arg: Map<string, number>, context?: Context): Array<number> | Promise<Array<number>>;
listToMap(arg: Array<Array<string>>, context?: Context): Map<string, string> | Promise<Map<string, string>>;
fetchThing(context?: Context): common.ICommonStruct | Promise<common.ICommonStruct>;
fetchThing(context?: Context): common.ICommonStructArgs | Promise<common.ICommonStructArgs>;
zip(context?: Context): void | Promise<void>;
}
export type IHandler<Context = any> = ILocalHandler<Context> & shared.SharedService.IHandler<Context>;
Expand Down Expand Up @@ -3545,7 +3545,7 @@ export namespace Calculator {
reject(err);
}
}).then((data: void): Buffer => {
const result: IPing__Result = { success: data };
const result: IPing__ResultArgs = { success: data };
output.writeMessageBegin("ping", thrift.MessageType.REPLY, requestId);
Ping__ResultCodec.encode(result, output);
output.writeMessageEnd();
Expand All @@ -3569,14 +3569,14 @@ export namespace Calculator {
reject(err);
}
}).then((data: number): Buffer => {
const result: IAdd__Result = { success: data };
const result: IAdd__ResultArgs = { success: data };
output.writeMessageBegin("add", thrift.MessageType.REPLY, requestId);
Add__ResultCodec.encode(result, output);
output.writeMessageEnd();
return output.flush();
}).catch((err: Error): Buffer => {
if (err instanceof operation.JankyResult) {
const result: IAdd__Result = { exp: err };
const result: IAdd__ResultArgs = { exp: err };
output.writeMessageBegin("add", thrift.MessageType.REPLY, requestId);
Add__ResultCodec.encode(result, output);
output.writeMessageEnd();
Expand All @@ -3592,7 +3592,7 @@ export namespace Calculator {
});
}
public process_addInt64(requestId: number, input: thrift.TProtocol, output: thrift.TProtocol, context: Context): Promise<Buffer> {
return new Promise<thrift.Int64>((resolve, reject): void => {
return new Promise<number | thrift.Int64>((resolve, reject): void => {
try {
const args: IAddInt64__Args = AddInt64__ArgsCodec.decode(input);
input.readMessageEnd();
Expand All @@ -3601,8 +3601,8 @@ export namespace Calculator {
catch (err) {
reject(err);
}
}).then((data: thrift.Int64): Buffer => {
const result: IAddInt64__Result = { success: data };
}).then((data: number | thrift.Int64): Buffer => {
const result: IAddInt64__ResultArgs = { success: data };
output.writeMessageBegin("addInt64", thrift.MessageType.REPLY, requestId);
AddInt64__ResultCodec.encode(result, output);
output.writeMessageEnd();
Expand All @@ -3626,7 +3626,7 @@ export namespace Calculator {
reject(err);
}
}).then((data: number): Buffer => {
const result: IAddWithContext__Result = { success: data };
const result: IAddWithContext__ResultArgs = { success: data };
output.writeMessageBegin("addWithContext", thrift.MessageType.REPLY, requestId);
AddWithContext__ResultCodec.encode(result, output);
output.writeMessageEnd();
Expand All @@ -3650,14 +3650,14 @@ export namespace Calculator {
reject(err);
}
}).then((data: number): Buffer => {
const result: ICalculate__Result = { success: data };
const result: ICalculate__ResultArgs = { success: data };
output.writeMessageBegin("calculate", thrift.MessageType.REPLY, requestId);
Calculate__ResultCodec.encode(result, output);
output.writeMessageEnd();
return output.flush();
}).catch((err: Error): Buffer => {
if (err instanceof operation.JankyOperation) {
const result: ICalculate__Result = { ouch: err };
const result: ICalculate__ResultArgs = { ouch: err };
output.writeMessageBegin("calculate", thrift.MessageType.REPLY, requestId);
Calculate__ResultCodec.encode(result, output);
output.writeMessageEnd();
Expand All @@ -3683,7 +3683,7 @@ export namespace Calculator {
reject(err);
}
}).then((data: string): Buffer => {
const result: IEchoBinary__Result = { success: data };
const result: IEchoBinary__ResultArgs = { success: data };
output.writeMessageBegin("echoBinary", thrift.MessageType.REPLY, requestId);
EchoBinary__ResultCodec.encode(result, output);
output.writeMessageEnd();
Expand All @@ -3707,7 +3707,7 @@ export namespace Calculator {
reject(err);
}
}).then((data: string): Buffer => {
const result: IEchoString__Result = { success: data };
const result: IEchoString__ResultArgs = { success: data };
output.writeMessageBegin("echoString", thrift.MessageType.REPLY, requestId);
EchoString__ResultCodec.encode(result, output);
output.writeMessageEnd();
Expand All @@ -3731,7 +3731,7 @@ export namespace Calculator {
reject(err);
}
}).then((data: string): Buffer => {
const result: ICheckName__Result = { success: data };
const result: ICheckName__ResultArgs = { success: data };
output.writeMessageBegin("checkName", thrift.MessageType.REPLY, requestId);
CheckName__ResultCodec.encode(result, output);
output.writeMessageEnd();
Expand All @@ -3755,7 +3755,7 @@ export namespace Calculator {
reject(err);
}
}).then((data: string): Buffer => {
const result: ICheckOptional__Result = { success: data };
const result: ICheckOptional__ResultArgs = { success: data };
output.writeMessageBegin("checkOptional", thrift.MessageType.REPLY, requestId);
CheckOptional__ResultCodec.encode(result, output);
output.writeMessageEnd();
Expand All @@ -3779,7 +3779,7 @@ export namespace Calculator {
reject(err);
}
}).then((data: Array<number>): Buffer => {
const result: IMapOneList__Result = { success: data };
const result: IMapOneList__ResultArgs = { success: data };
output.writeMessageBegin("mapOneList", thrift.MessageType.REPLY, requestId);
MapOneList__ResultCodec.encode(result, output);
output.writeMessageEnd();
Expand All @@ -3803,7 +3803,7 @@ export namespace Calculator {
reject(err);
}
}).then((data: Array<number>): Buffer => {
const result: IMapValues__Result = { success: data };
const result: IMapValues__ResultArgs = { success: data };
output.writeMessageBegin("mapValues", thrift.MessageType.REPLY, requestId);
MapValues__ResultCodec.encode(result, output);
output.writeMessageEnd();
Expand All @@ -3827,7 +3827,7 @@ export namespace Calculator {
reject(err);
}
}).then((data: Map<string, string>): Buffer => {
const result: IListToMap__Result = { success: data };
const result: IListToMap__ResultArgs = { success: data };
output.writeMessageBegin("listToMap", thrift.MessageType.REPLY, requestId);
ListToMap__ResultCodec.encode(result, output);
output.writeMessageEnd();
Expand All @@ -3841,16 +3841,16 @@ export namespace Calculator {
});
}
public process_fetchThing(requestId: number, input: thrift.TProtocol, output: thrift.TProtocol, context: Context): Promise<Buffer> {
return new Promise<common.ICommonStruct>((resolve, reject): void => {
return new Promise<common.ICommonStructArgs>((resolve, reject): void => {
try {
input.readMessageEnd();
resolve(this._handler.fetchThing(context));
}
catch (err) {
reject(err);
}
}).then((data: common.ICommonStruct): Buffer => {
const result: IFetchThing__Result = { success: data };
}).then((data: common.ICommonStructArgs): Buffer => {
const result: IFetchThing__ResultArgs = { success: data };
output.writeMessageBegin("fetchThing", thrift.MessageType.REPLY, requestId);
FetchThing__ResultCodec.encode(result, output);
output.writeMessageEnd();
Expand All @@ -3873,7 +3873,7 @@ export namespace Calculator {
reject(err);
}
}).then((data: void): Buffer => {
const result: IZip__Result = { success: data };
const result: IZip__ResultArgs = { success: data };
output.writeMessageBegin("zip", thrift.MessageType.REPLY, requestId);
Zip__ResultCodec.encode(result, output);
output.writeMessageEnd();
Expand Down
16 changes: 8 additions & 8 deletions src/tests/unit/fixtures/generated/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -738,8 +738,8 @@ export namespace SharedService {
}
}
export interface IHandler<Context = any> {
getStruct(key: number, context?: Context): ISharedStruct | Promise<ISharedStruct>;
getUnion(index: number, context?: Context): ISharedUnion | Promise<ISharedUnion>;
getStruct(key: number, context?: Context): ISharedStructArgs | Promise<ISharedStructArgs>;
getUnion(index: number, context?: Context): ISharedUnionArgs | Promise<ISharedUnionArgs>;
}
export class Processor<Context = any> extends thrift.ThriftProcessor<Context, IHandler<Context>> {
protected readonly _handler: IHandler<Context>;
Expand Down Expand Up @@ -785,7 +785,7 @@ export namespace SharedService {
});
}
public process_getStruct(requestId: number, input: thrift.TProtocol, output: thrift.TProtocol, context: Context): Promise<Buffer> {
return new Promise<ISharedStruct>((resolve, reject): void => {
return new Promise<ISharedStructArgs>((resolve, reject): void => {
try {
const args: IGetStruct__Args = GetStruct__ArgsCodec.decode(input);
input.readMessageEnd();
Expand All @@ -794,8 +794,8 @@ export namespace SharedService {
catch (err) {
reject(err);
}
}).then((data: ISharedStruct): Buffer => {
const result: IGetStruct__Result = { success: data };
}).then((data: ISharedStructArgs): Buffer => {
const result: IGetStruct__ResultArgs = { success: data };
output.writeMessageBegin("getStruct", thrift.MessageType.REPLY, requestId);
GetStruct__ResultCodec.encode(result, output);
output.writeMessageEnd();
Expand All @@ -809,7 +809,7 @@ export namespace SharedService {
});
}
public process_getUnion(requestId: number, input: thrift.TProtocol, output: thrift.TProtocol, context: Context): Promise<Buffer> {
return new Promise<ISharedUnion>((resolve, reject): void => {
return new Promise<ISharedUnionArgs>((resolve, reject): void => {
try {
const args: IGetUnion__Args = GetUnion__ArgsCodec.decode(input);
input.readMessageEnd();
Expand All @@ -818,8 +818,8 @@ export namespace SharedService {
catch (err) {
reject(err);
}
}).then((data: ISharedUnion): Buffer => {
const result: IGetUnion__Result = { success: data };
}).then((data: ISharedUnionArgs): Buffer => {
const result: IGetUnion__ResultArgs = { success: data };
output.writeMessageBegin("getUnion", thrift.MessageType.REPLY, requestId);
GetUnion__ResultCodec.encode(result, output);
output.writeMessageEnd();
Expand Down
Loading

0 comments on commit 44e4746

Please sign in to comment.