Skip to content

Commit

Permalink
feat: Allow i64 to be represented as string in user code (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-greene-ck authored Mar 11, 2019
1 parent 446d804 commit 0831ec6
Show file tree
Hide file tree
Showing 18 changed files with 103 additions and 87 deletions.
6 changes: 3 additions & 3 deletions src/main/render/apache/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,18 @@ export function typeNodeForFieldType(
return ts.createTypeReferenceNode(fieldType.value, undefined)

case SyntaxType.SetType:
return ts.createTypeReferenceNode('Set', [
return ts.createTypeReferenceNode(COMMON_IDENTIFIERS.Set, [
typeNodeForFieldType(fieldType.valueType),
])

case SyntaxType.MapType:
return ts.createTypeReferenceNode('Map', [
return ts.createTypeReferenceNode(COMMON_IDENTIFIERS.Map, [
typeNodeForFieldType(fieldType.keyType),
typeNodeForFieldType(fieldType.valueType),
])

case SyntaxType.ListType:
return ts.createTypeReferenceNode('Array', [
return ts.createTypeReferenceNode(COMMON_IDENTIFIERS.Array, [
typeNodeForFieldType(fieldType.valueType),
])

Expand Down
7 changes: 4 additions & 3 deletions src/main/render/thrift-server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,18 +275,18 @@ export function typeNodeForFieldType(
)

case SyntaxType.SetType:
return ts.createTypeReferenceNode('Set', [
return ts.createTypeReferenceNode(COMMON_IDENTIFIERS.Set, [
typeNodeForFieldType(fieldType.valueType, state, loose),
])

case SyntaxType.MapType:
return ts.createTypeReferenceNode('Map', [
return ts.createTypeReferenceNode(COMMON_IDENTIFIERS.Map, [
typeNodeForFieldType(fieldType.keyType, state, loose),
typeNodeForFieldType(fieldType.valueType, state, loose),
])

case SyntaxType.ListType:
return ts.createTypeReferenceNode('Array', [
return ts.createTypeReferenceNode(COMMON_IDENTIFIERS.Array, [
typeNodeForFieldType(fieldType.valueType, state, loose),
])

Expand All @@ -300,6 +300,7 @@ export function typeNodeForFieldType(
if (loose === true) {
return ts.createUnionTypeNode([
createNumberType(),
createStringType(),
ts.createTypeReferenceNode(
COMMON_IDENTIFIERS.Int64,
undefined,
Expand Down
17 changes: 16 additions & 1 deletion src/main/render/thrift-server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,22 @@ export function coerceType(
ts.createNew(COMMON_IDENTIFIERS.Int64, undefined, [
valueName,
]),
valueName,
ts.createConditional(
ts.createBinary(
ts.createTypeOf(valueName),
ts.SyntaxKind.EqualsEqualsEqualsToken,
ts.createLiteral('string'),
),
ts.createCall(
ts.createPropertyAccess(
COMMON_IDENTIFIERS.Int64,
ts.createIdentifier('fromDecimalString'),
),
undefined,
[valueName],
),
valueName,
),
),
)

Expand Down
26 changes: 13 additions & 13 deletions src/tests/unit/fixtures/generated/calculator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,14 +685,14 @@ export namespace Calculator {
num2: thrift.Int64;
}
export interface IAddInt64__ArgsArgs {
num1: number | thrift.Int64;
num2: number | thrift.Int64;
num1: number | string | thrift.Int64;
num2: number | string | thrift.Int64;
}
export const AddInt64__ArgsCodec: thrift.IStructCodec<IAddInt64__ArgsArgs, IAddInt64__Args> = {
encode(args: IAddInt64__ArgsArgs, output: thrift.TProtocol): void {
const obj = {
num1: (typeof args.num1 === "number" ? new thrift.Int64(args.num1) : args.num1),
num2: (typeof args.num2 === "number" ? new thrift.Int64(args.num2) : args.num2)
num1: (typeof args.num1 === "number" ? new thrift.Int64(args.num1) : typeof args.num1 === "string" ? thrift.Int64.fromDecimalString(args.num1) : args.num1),
num2: (typeof args.num2 === "number" ? new thrift.Int64(args.num2) : typeof args.num2 === "string" ? thrift.Int64.fromDecimalString(args.num2) : args.num2)
};
output.writeStructBegin("AddInt64__Args");
if (obj.num1 != null) {
Expand Down Expand Up @@ -770,14 +770,14 @@ export namespace Calculator {
constructor(args: IAddInt64__ArgsArgs) {
super();
if (args.num1 != null) {
const value_23: thrift.Int64 = (typeof args.num1 === "number" ? new thrift.Int64(args.num1) : args.num1);
const value_23: thrift.Int64 = (typeof args.num1 === "number" ? new thrift.Int64(args.num1) : typeof args.num1 === "string" ? thrift.Int64.fromDecimalString(args.num1) : args.num1);
this.num1 = value_23;
}
else {
throw new thrift.TProtocolException(thrift.TProtocolExceptionType.UNKNOWN, "Required field[num1] is unset!");
}
if (args.num2 != null) {
const value_24: thrift.Int64 = (typeof args.num2 === "number" ? new thrift.Int64(args.num2) : args.num2);
const value_24: thrift.Int64 = (typeof args.num2 === "number" ? new thrift.Int64(args.num2) : typeof args.num2 === "string" ? thrift.Int64.fromDecimalString(args.num2) : args.num2);
this.num2 = value_24;
}
else {
Expand Down Expand Up @@ -1929,12 +1929,12 @@ export namespace Calculator {
success?: thrift.Int64;
}
export interface IAddInt64__ResultArgs {
success?: number | thrift.Int64;
success?: number | string | thrift.Int64;
}
export const AddInt64__ResultCodec: thrift.IStructCodec<IAddInt64__ResultArgs, IAddInt64__Result> = {
encode(args: IAddInt64__ResultArgs, output: thrift.TProtocol): void {
const obj = {
success: (typeof args.success === "number" ? new thrift.Int64(args.success) : args.success)
success: (typeof args.success === "number" ? new thrift.Int64(args.success) : typeof args.success === "string" ? thrift.Int64.fromDecimalString(args.success) : args.success)
};
output.writeStructBegin("AddInt64__Result");
if (obj.success != null) {
Expand Down Expand Up @@ -1985,7 +1985,7 @@ export namespace Calculator {
constructor(args: IAddInt64__ResultArgs = {}) {
super();
if (args.success != null) {
const value_69: thrift.Int64 = (typeof args.success === "number" ? new thrift.Int64(args.success) : args.success);
const value_69: thrift.Int64 = (typeof args.success === "number" ? new thrift.Int64(args.success) : typeof args.success === "string" ? thrift.Int64.fromDecimalString(args.success) : args.success);
this.success = value_69;
}
}
Expand Down Expand Up @@ -2961,7 +2961,7 @@ export namespace Calculator {
}
});
}
public addInt64(num1: number | thrift.Int64, num2: number | thrift.Int64, context?: Context): Promise<thrift.Int64> {
public addInt64(num1: number | string | thrift.Int64, num2: number | string | thrift.Int64, context?: Context): Promise<thrift.Int64> {
const writer: thrift.TTransport = new this.transport();
const output: thrift.TProtocol = new this.protocol(writer);
output.writeMessageBegin("addInt64", thrift.MessageType.CALL, this.incrementRequestId());
Expand Down 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): (number | thrift.Int64) | Promise<number | thrift.Int64>;
addInt64(num1: thrift.Int64, num2: thrift.Int64, context?: Context): (number | string | thrift.Int64) | Promise<number | string | 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 Down Expand Up @@ -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<number | thrift.Int64>((resolve, reject): void => {
return new Promise<number | string | thrift.Int64>((resolve, reject): void => {
try {
const args: IAddInt64__Args = AddInt64__ArgsCodec.decode(input);
input.readMessageEnd();
Expand All @@ -3601,7 +3601,7 @@ export namespace Calculator {
catch (err) {
reject(err);
}
}).then((data: number | thrift.Int64): Buffer => {
}).then((data: number | string | thrift.Int64): Buffer => {
const result: IAddInt64__ResultArgs = { success: data };
output.writeMessageBegin("addInt64", thrift.MessageType.REPLY, requestId);
AddInt64__ResultCodec.encode(result, output);
Expand Down
6 changes: 3 additions & 3 deletions src/tests/unit/fixtures/generated/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export interface ICode {
status?: thrift.Int64;
}
export interface ICodeArgs {
status?: number | thrift.Int64;
status?: number | string | thrift.Int64;
}
export const CodeCodec: thrift.IStructCodec<ICodeArgs, ICode> = {
encode(args: ICodeArgs, output: thrift.TProtocol): void {
const obj = {
status: (typeof args.status === "number" ? new thrift.Int64(args.status) : args.status)
status: (typeof args.status === "number" ? new thrift.Int64(args.status) : typeof args.status === "string" ? thrift.Int64.fromDecimalString(args.status) : args.status)
};
output.writeStructBegin("Code");
if (obj.status != null) {
Expand Down Expand Up @@ -65,7 +65,7 @@ export class Code extends thrift.StructLike implements ICode {
constructor(args: ICodeArgs = {}) {
super();
if (args.status != null) {
const value_2: thrift.Int64 = (typeof args.status === "number" ? new thrift.Int64(args.status) : args.status);
const value_2: thrift.Int64 = (typeof args.status === "number" ? new thrift.Int64(args.status) : typeof args.status === "string" ? thrift.Int64.fromDecimalString(args.status) : args.status);
this.status = value_2;
}
}
Expand Down
26 changes: 13 additions & 13 deletions src/tests/unit/fixtures/generated/strict-unions/calculator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,14 +714,14 @@ export namespace Calculator {
num2: thrift.Int64;
}
export interface IAddInt64__ArgsArgs {
num1: number | thrift.Int64;
num2: number | thrift.Int64;
num1: number | string | thrift.Int64;
num2: number | string | thrift.Int64;
}
export const AddInt64__ArgsCodec: thrift.IStructCodec<IAddInt64__ArgsArgs, IAddInt64__Args> = {
encode(args: IAddInt64__ArgsArgs, output: thrift.TProtocol): void {
const obj = {
num1: (typeof args.num1 === "number" ? new thrift.Int64(args.num1) : args.num1),
num2: (typeof args.num2 === "number" ? new thrift.Int64(args.num2) : args.num2)
num1: (typeof args.num1 === "number" ? new thrift.Int64(args.num1) : typeof args.num1 === "string" ? thrift.Int64.fromDecimalString(args.num1) : args.num1),
num2: (typeof args.num2 === "number" ? new thrift.Int64(args.num2) : typeof args.num2 === "string" ? thrift.Int64.fromDecimalString(args.num2) : args.num2)
};
output.writeStructBegin("AddInt64__Args");
if (obj.num1 != null) {
Expand Down Expand Up @@ -799,14 +799,14 @@ export namespace Calculator {
constructor(args: IAddInt64__ArgsArgs) {
super();
if (args.num1 != null) {
const value_23: thrift.Int64 = (typeof args.num1 === "number" ? new thrift.Int64(args.num1) : args.num1);
const value_23: thrift.Int64 = (typeof args.num1 === "number" ? new thrift.Int64(args.num1) : typeof args.num1 === "string" ? thrift.Int64.fromDecimalString(args.num1) : args.num1);
this.num1 = value_23;
}
else {
throw new thrift.TProtocolException(thrift.TProtocolExceptionType.UNKNOWN, "Required field[num1] is unset!");
}
if (args.num2 != null) {
const value_24: thrift.Int64 = (typeof args.num2 === "number" ? new thrift.Int64(args.num2) : args.num2);
const value_24: thrift.Int64 = (typeof args.num2 === "number" ? new thrift.Int64(args.num2) : typeof args.num2 === "string" ? thrift.Int64.fromDecimalString(args.num2) : args.num2);
this.num2 = value_24;
}
else {
Expand Down Expand Up @@ -1958,12 +1958,12 @@ export namespace Calculator {
success?: thrift.Int64;
}
export interface IAddInt64__ResultArgs {
success?: number | thrift.Int64;
success?: number | string | thrift.Int64;
}
export const AddInt64__ResultCodec: thrift.IStructCodec<IAddInt64__ResultArgs, IAddInt64__Result> = {
encode(args: IAddInt64__ResultArgs, output: thrift.TProtocol): void {
const obj = {
success: (typeof args.success === "number" ? new thrift.Int64(args.success) : args.success)
success: (typeof args.success === "number" ? new thrift.Int64(args.success) : typeof args.success === "string" ? thrift.Int64.fromDecimalString(args.success) : args.success)
};
output.writeStructBegin("AddInt64__Result");
if (obj.success != null) {
Expand Down Expand Up @@ -2014,7 +2014,7 @@ export namespace Calculator {
constructor(args: IAddInt64__ResultArgs = {}) {
super();
if (args.success != null) {
const value_69: thrift.Int64 = (typeof args.success === "number" ? new thrift.Int64(args.success) : args.success);
const value_69: thrift.Int64 = (typeof args.success === "number" ? new thrift.Int64(args.success) : typeof args.success === "string" ? thrift.Int64.fromDecimalString(args.success) : args.success);
this.success = value_69;
}
}
Expand Down Expand Up @@ -2990,7 +2990,7 @@ export namespace Calculator {
}
});
}
public addInt64(num1: number | thrift.Int64, num2: number | thrift.Int64, context?: Context): Promise<thrift.Int64> {
public addInt64(num1: number | string | thrift.Int64, num2: number | string | thrift.Int64, context?: Context): Promise<thrift.Int64> {
const writer: thrift.TTransport = new this.transport();
const output: thrift.TProtocol = new this.protocol(writer);
output.writeMessageBegin("addInt64", thrift.MessageType.CALL, this.incrementRequestId());
Expand Down Expand Up @@ -3448,7 +3448,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): (number | thrift.Int64) | Promise<number | thrift.Int64>;
addInt64(num1: thrift.Int64, num2: thrift.Int64, context?: Context): (number | string | thrift.Int64) | Promise<number | string | 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 Down Expand Up @@ -3621,7 +3621,7 @@ export namespace Calculator {
});
}
public process_addInt64(requestId: number, input: thrift.TProtocol, output: thrift.TProtocol, context: Context): Promise<Buffer> {
return new Promise<number | thrift.Int64>((resolve, reject): void => {
return new Promise<number | string | thrift.Int64>((resolve, reject): void => {
try {
const args: IAddInt64__Args = AddInt64__ArgsCodec.decode(input);
input.readMessageEnd();
Expand All @@ -3630,7 +3630,7 @@ export namespace Calculator {
catch (err) {
reject(err);
}
}).then((data: number | thrift.Int64): Buffer => {
}).then((data: number | string | thrift.Int64): Buffer => {
const result: IAddInt64__ResultArgs = { success: data };
output.writeMessageBegin("addInt64", thrift.MessageType.REPLY, requestId);
AddInt64__ResultCodec.encode(result, output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export interface ICode {
status?: thrift.Int64;
}
export interface ICodeArgs {
status?: number | thrift.Int64;
status?: number | string | thrift.Int64;
}
export const CodeCodec: thrift.IStructCodec<ICodeArgs, ICode> = {
encode(args: ICodeArgs, output: thrift.TProtocol): void {
const obj = {
status: (typeof args.status === "number" ? new thrift.Int64(args.status) : args.status)
status: (typeof args.status === "number" ? new thrift.Int64(args.status) : typeof args.status === "string" ? thrift.Int64.fromDecimalString(args.status) : args.status)
};
output.writeStructBegin("Code");
if (obj.status != null) {
Expand Down Expand Up @@ -65,7 +65,7 @@ export class Code extends thrift.StructLike implements ICode {
constructor(args: ICodeArgs = {}) {
super();
if (args.status != null) {
const value_2: thrift.Int64 = (typeof args.status === "number" ? new thrift.Int64(args.status) : args.status);
const value_2: thrift.Int64 = (typeof args.status === "number" ? new thrift.Int64(args.status) : typeof args.status === "string" ? thrift.Int64.fromDecimalString(args.status) : args.status);
this.status = value_2;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ export interface IMyStruct {
}
export interface IMyStructArgs {
id?: number;
bigID?: number | thrift.Int64;
bigID?: number | string | thrift.Int64;
}
export const MyStructCodec: thrift.IStructCodec<IMyStructArgs, IMyStruct> = {
encode(args: IMyStructArgs, output: thrift.TProtocol): void {
const obj = {
id: (args.id != null ? args.id : 45),
bigID: (args.bigID != null ? (typeof args.bigID === "number" ? new thrift.Int64(args.bigID) : args.bigID) : thrift.Int64.fromDecimalString("23948234"))
bigID: (args.bigID != null ? (typeof args.bigID === "number" ? new thrift.Int64(args.bigID) : typeof args.bigID === "string" ? thrift.Int64.fromDecimalString(args.bigID) : args.bigID) : thrift.Int64.fromDecimalString("23948234"))
};
output.writeStructBegin("MyStruct");
if (obj.id != null) {
Expand Down Expand Up @@ -100,7 +100,7 @@ export class MyStruct extends thrift.StructLike implements IMyStruct {
this.id = value_3;
}
if (args.bigID != null) {
const value_4: thrift.Int64 = (typeof args.bigID === "number" ? new thrift.Int64(args.bigID) : args.bigID);
const value_4: thrift.Int64 = (typeof args.bigID === "number" ? new thrift.Int64(args.bigID) : typeof args.bigID === "string" ? thrift.Int64.fromDecimalString(args.bigID) : args.bigID);
this.bigID = value_4;
}
}
Expand Down
Loading

0 comments on commit 0831ec6

Please sign in to comment.