Skip to content

Commit 0831ec6

Browse files
feat: Allow i64 to be represented as string in user code (#148)
1 parent 446d804 commit 0831ec6

18 files changed

+103
-87
lines changed

src/main/render/apache/types.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,18 @@ export function typeNodeForFieldType(
233233
return ts.createTypeReferenceNode(fieldType.value, undefined)
234234

235235
case SyntaxType.SetType:
236-
return ts.createTypeReferenceNode('Set', [
236+
return ts.createTypeReferenceNode(COMMON_IDENTIFIERS.Set, [
237237
typeNodeForFieldType(fieldType.valueType),
238238
])
239239

240240
case SyntaxType.MapType:
241-
return ts.createTypeReferenceNode('Map', [
241+
return ts.createTypeReferenceNode(COMMON_IDENTIFIERS.Map, [
242242
typeNodeForFieldType(fieldType.keyType),
243243
typeNodeForFieldType(fieldType.valueType),
244244
])
245245

246246
case SyntaxType.ListType:
247-
return ts.createTypeReferenceNode('Array', [
247+
return ts.createTypeReferenceNode(COMMON_IDENTIFIERS.Array, [
248248
typeNodeForFieldType(fieldType.valueType),
249249
])
250250

src/main/render/thrift-server/types.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -275,18 +275,18 @@ export function typeNodeForFieldType(
275275
)
276276

277277
case SyntaxType.SetType:
278-
return ts.createTypeReferenceNode('Set', [
278+
return ts.createTypeReferenceNode(COMMON_IDENTIFIERS.Set, [
279279
typeNodeForFieldType(fieldType.valueType, state, loose),
280280
])
281281

282282
case SyntaxType.MapType:
283-
return ts.createTypeReferenceNode('Map', [
283+
return ts.createTypeReferenceNode(COMMON_IDENTIFIERS.Map, [
284284
typeNodeForFieldType(fieldType.keyType, state, loose),
285285
typeNodeForFieldType(fieldType.valueType, state, loose),
286286
])
287287

288288
case SyntaxType.ListType:
289-
return ts.createTypeReferenceNode('Array', [
289+
return ts.createTypeReferenceNode(COMMON_IDENTIFIERS.Array, [
290290
typeNodeForFieldType(fieldType.valueType, state, loose),
291291
])
292292

@@ -300,6 +300,7 @@ export function typeNodeForFieldType(
300300
if (loose === true) {
301301
return ts.createUnionTypeNode([
302302
createNumberType(),
303+
createStringType(),
303304
ts.createTypeReferenceNode(
304305
COMMON_IDENTIFIERS.Int64,
305306
undefined,

src/main/render/thrift-server/utils.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,22 @@ export function coerceType(
4343
ts.createNew(COMMON_IDENTIFIERS.Int64, undefined, [
4444
valueName,
4545
]),
46-
valueName,
46+
ts.createConditional(
47+
ts.createBinary(
48+
ts.createTypeOf(valueName),
49+
ts.SyntaxKind.EqualsEqualsEqualsToken,
50+
ts.createLiteral('string'),
51+
),
52+
ts.createCall(
53+
ts.createPropertyAccess(
54+
COMMON_IDENTIFIERS.Int64,
55+
ts.createIdentifier('fromDecimalString'),
56+
),
57+
undefined,
58+
[valueName],
59+
),
60+
valueName,
61+
),
4762
),
4863
)
4964

src/tests/unit/fixtures/generated/calculator/index.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -685,14 +685,14 @@ export namespace Calculator {
685685
num2: thrift.Int64;
686686
}
687687
export interface IAddInt64__ArgsArgs {
688-
num1: number | thrift.Int64;
689-
num2: number | thrift.Int64;
688+
num1: number | string | thrift.Int64;
689+
num2: number | string | thrift.Int64;
690690
}
691691
export const AddInt64__ArgsCodec: thrift.IStructCodec<IAddInt64__ArgsArgs, IAddInt64__Args> = {
692692
encode(args: IAddInt64__ArgsArgs, output: thrift.TProtocol): void {
693693
const obj = {
694-
num1: (typeof args.num1 === "number" ? new thrift.Int64(args.num1) : args.num1),
695-
num2: (typeof args.num2 === "number" ? new thrift.Int64(args.num2) : args.num2)
694+
num1: (typeof args.num1 === "number" ? new thrift.Int64(args.num1) : typeof args.num1 === "string" ? thrift.Int64.fromDecimalString(args.num1) : args.num1),
695+
num2: (typeof args.num2 === "number" ? new thrift.Int64(args.num2) : typeof args.num2 === "string" ? thrift.Int64.fromDecimalString(args.num2) : args.num2)
696696
};
697697
output.writeStructBegin("AddInt64__Args");
698698
if (obj.num1 != null) {
@@ -770,14 +770,14 @@ export namespace Calculator {
770770
constructor(args: IAddInt64__ArgsArgs) {
771771
super();
772772
if (args.num1 != null) {
773-
const value_23: thrift.Int64 = (typeof args.num1 === "number" ? new thrift.Int64(args.num1) : args.num1);
773+
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);
774774
this.num1 = value_23;
775775
}
776776
else {
777777
throw new thrift.TProtocolException(thrift.TProtocolExceptionType.UNKNOWN, "Required field[num1] is unset!");
778778
}
779779
if (args.num2 != null) {
780-
const value_24: thrift.Int64 = (typeof args.num2 === "number" ? new thrift.Int64(args.num2) : args.num2);
780+
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);
781781
this.num2 = value_24;
782782
}
783783
else {
@@ -1929,12 +1929,12 @@ export namespace Calculator {
19291929
success?: thrift.Int64;
19301930
}
19311931
export interface IAddInt64__ResultArgs {
1932-
success?: number | thrift.Int64;
1932+
success?: number | string | thrift.Int64;
19331933
}
19341934
export const AddInt64__ResultCodec: thrift.IStructCodec<IAddInt64__ResultArgs, IAddInt64__Result> = {
19351935
encode(args: IAddInt64__ResultArgs, output: thrift.TProtocol): void {
19361936
const obj = {
1937-
success: (typeof args.success === "number" ? new thrift.Int64(args.success) : args.success)
1937+
success: (typeof args.success === "number" ? new thrift.Int64(args.success) : typeof args.success === "string" ? thrift.Int64.fromDecimalString(args.success) : args.success)
19381938
};
19391939
output.writeStructBegin("AddInt64__Result");
19401940
if (obj.success != null) {
@@ -1985,7 +1985,7 @@ export namespace Calculator {
19851985
constructor(args: IAddInt64__ResultArgs = {}) {
19861986
super();
19871987
if (args.success != null) {
1988-
const value_69: thrift.Int64 = (typeof args.success === "number" ? new thrift.Int64(args.success) : args.success);
1988+
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);
19891989
this.success = value_69;
19901990
}
19911991
}
@@ -2961,7 +2961,7 @@ export namespace Calculator {
29612961
}
29622962
});
29632963
}
2964-
public addInt64(num1: number | thrift.Int64, num2: number | thrift.Int64, context?: Context): Promise<thrift.Int64> {
2964+
public addInt64(num1: number | string | thrift.Int64, num2: number | string | thrift.Int64, context?: Context): Promise<thrift.Int64> {
29652965
const writer: thrift.TTransport = new this.transport();
29662966
const output: thrift.TProtocol = new this.protocol(writer);
29672967
output.writeMessageBegin("addInt64", thrift.MessageType.CALL, this.incrementRequestId());
@@ -3419,7 +3419,7 @@ export namespace Calculator {
34193419
export interface ILocalHandler<Context = any> {
34203420
ping(context?: Context): void | Promise<void>;
34213421
add(num1: number, num2: number, context?: Context): number | Promise<number>;
3422-
addInt64(num1: thrift.Int64, num2: thrift.Int64, context?: Context): (number | thrift.Int64) | Promise<number | thrift.Int64>;
3422+
addInt64(num1: thrift.Int64, num2: thrift.Int64, context?: Context): (number | string | thrift.Int64) | Promise<number | string | thrift.Int64>;
34233423
addWithContext(num1: number, num2: number, context?: Context): number | Promise<number>;
34243424
calculate(logid: number, work: IWork, context?: Context): number | Promise<number>;
34253425
echoBinary(word: Buffer, context?: Context): string | Promise<string>;
@@ -3592,7 +3592,7 @@ export namespace Calculator {
35923592
});
35933593
}
35943594
public process_addInt64(requestId: number, input: thrift.TProtocol, output: thrift.TProtocol, context: Context): Promise<Buffer> {
3595-
return new Promise<number | thrift.Int64>((resolve, reject): void => {
3595+
return new Promise<number | string | thrift.Int64>((resolve, reject): void => {
35963596
try {
35973597
const args: IAddInt64__Args = AddInt64__ArgsCodec.decode(input);
35983598
input.readMessageEnd();
@@ -3601,7 +3601,7 @@ export namespace Calculator {
36013601
catch (err) {
36023602
reject(err);
36033603
}
3604-
}).then((data: number | thrift.Int64): Buffer => {
3604+
}).then((data: number | string | thrift.Int64): Buffer => {
36053605
const result: IAddInt64__ResultArgs = { success: data };
36063606
output.writeMessageBegin("addInt64", thrift.MessageType.REPLY, requestId);
36073607
AddInt64__ResultCodec.encode(result, output);

src/tests/unit/fixtures/generated/shared/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ export interface ICode {
99
status?: thrift.Int64;
1010
}
1111
export interface ICodeArgs {
12-
status?: number | thrift.Int64;
12+
status?: number | string | thrift.Int64;
1313
}
1414
export const CodeCodec: thrift.IStructCodec<ICodeArgs, ICode> = {
1515
encode(args: ICodeArgs, output: thrift.TProtocol): void {
1616
const obj = {
17-
status: (typeof args.status === "number" ? new thrift.Int64(args.status) : args.status)
17+
status: (typeof args.status === "number" ? new thrift.Int64(args.status) : typeof args.status === "string" ? thrift.Int64.fromDecimalString(args.status) : args.status)
1818
};
1919
output.writeStructBegin("Code");
2020
if (obj.status != null) {
@@ -65,7 +65,7 @@ export class Code extends thrift.StructLike implements ICode {
6565
constructor(args: ICodeArgs = {}) {
6666
super();
6767
if (args.status != null) {
68-
const value_2: thrift.Int64 = (typeof args.status === "number" ? new thrift.Int64(args.status) : args.status);
68+
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);
6969
this.status = value_2;
7070
}
7171
}

src/tests/unit/fixtures/generated/strict-unions/calculator/index.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -714,14 +714,14 @@ export namespace Calculator {
714714
num2: thrift.Int64;
715715
}
716716
export interface IAddInt64__ArgsArgs {
717-
num1: number | thrift.Int64;
718-
num2: number | thrift.Int64;
717+
num1: number | string | thrift.Int64;
718+
num2: number | string | thrift.Int64;
719719
}
720720
export const AddInt64__ArgsCodec: thrift.IStructCodec<IAddInt64__ArgsArgs, IAddInt64__Args> = {
721721
encode(args: IAddInt64__ArgsArgs, output: thrift.TProtocol): void {
722722
const obj = {
723-
num1: (typeof args.num1 === "number" ? new thrift.Int64(args.num1) : args.num1),
724-
num2: (typeof args.num2 === "number" ? new thrift.Int64(args.num2) : args.num2)
723+
num1: (typeof args.num1 === "number" ? new thrift.Int64(args.num1) : typeof args.num1 === "string" ? thrift.Int64.fromDecimalString(args.num1) : args.num1),
724+
num2: (typeof args.num2 === "number" ? new thrift.Int64(args.num2) : typeof args.num2 === "string" ? thrift.Int64.fromDecimalString(args.num2) : args.num2)
725725
};
726726
output.writeStructBegin("AddInt64__Args");
727727
if (obj.num1 != null) {
@@ -799,14 +799,14 @@ export namespace Calculator {
799799
constructor(args: IAddInt64__ArgsArgs) {
800800
super();
801801
if (args.num1 != null) {
802-
const value_23: thrift.Int64 = (typeof args.num1 === "number" ? new thrift.Int64(args.num1) : args.num1);
802+
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);
803803
this.num1 = value_23;
804804
}
805805
else {
806806
throw new thrift.TProtocolException(thrift.TProtocolExceptionType.UNKNOWN, "Required field[num1] is unset!");
807807
}
808808
if (args.num2 != null) {
809-
const value_24: thrift.Int64 = (typeof args.num2 === "number" ? new thrift.Int64(args.num2) : args.num2);
809+
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);
810810
this.num2 = value_24;
811811
}
812812
else {
@@ -1958,12 +1958,12 @@ export namespace Calculator {
19581958
success?: thrift.Int64;
19591959
}
19601960
export interface IAddInt64__ResultArgs {
1961-
success?: number | thrift.Int64;
1961+
success?: number | string | thrift.Int64;
19621962
}
19631963
export const AddInt64__ResultCodec: thrift.IStructCodec<IAddInt64__ResultArgs, IAddInt64__Result> = {
19641964
encode(args: IAddInt64__ResultArgs, output: thrift.TProtocol): void {
19651965
const obj = {
1966-
success: (typeof args.success === "number" ? new thrift.Int64(args.success) : args.success)
1966+
success: (typeof args.success === "number" ? new thrift.Int64(args.success) : typeof args.success === "string" ? thrift.Int64.fromDecimalString(args.success) : args.success)
19671967
};
19681968
output.writeStructBegin("AddInt64__Result");
19691969
if (obj.success != null) {
@@ -2014,7 +2014,7 @@ export namespace Calculator {
20142014
constructor(args: IAddInt64__ResultArgs = {}) {
20152015
super();
20162016
if (args.success != null) {
2017-
const value_69: thrift.Int64 = (typeof args.success === "number" ? new thrift.Int64(args.success) : args.success);
2017+
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);
20182018
this.success = value_69;
20192019
}
20202020
}
@@ -2990,7 +2990,7 @@ export namespace Calculator {
29902990
}
29912991
});
29922992
}
2993-
public addInt64(num1: number | thrift.Int64, num2: number | thrift.Int64, context?: Context): Promise<thrift.Int64> {
2993+
public addInt64(num1: number | string | thrift.Int64, num2: number | string | thrift.Int64, context?: Context): Promise<thrift.Int64> {
29942994
const writer: thrift.TTransport = new this.transport();
29952995
const output: thrift.TProtocol = new this.protocol(writer);
29962996
output.writeMessageBegin("addInt64", thrift.MessageType.CALL, this.incrementRequestId());
@@ -3448,7 +3448,7 @@ export namespace Calculator {
34483448
export interface ILocalHandler<Context = any> {
34493449
ping(context?: Context): void | Promise<void>;
34503450
add(num1: number, num2: number, context?: Context): number | Promise<number>;
3451-
addInt64(num1: thrift.Int64, num2: thrift.Int64, context?: Context): (number | thrift.Int64) | Promise<number | thrift.Int64>;
3451+
addInt64(num1: thrift.Int64, num2: thrift.Int64, context?: Context): (number | string | thrift.Int64) | Promise<number | string | thrift.Int64>;
34523452
addWithContext(num1: number, num2: number, context?: Context): number | Promise<number>;
34533453
calculate(logid: number, work: IWork, context?: Context): number | Promise<number>;
34543454
echoBinary(word: Buffer, context?: Context): string | Promise<string>;
@@ -3621,7 +3621,7 @@ export namespace Calculator {
36213621
});
36223622
}
36233623
public process_addInt64(requestId: number, input: thrift.TProtocol, output: thrift.TProtocol, context: Context): Promise<Buffer> {
3624-
return new Promise<number | thrift.Int64>((resolve, reject): void => {
3624+
return new Promise<number | string | thrift.Int64>((resolve, reject): void => {
36253625
try {
36263626
const args: IAddInt64__Args = AddInt64__ArgsCodec.decode(input);
36273627
input.readMessageEnd();
@@ -3630,7 +3630,7 @@ export namespace Calculator {
36303630
catch (err) {
36313631
reject(err);
36323632
}
3633-
}).then((data: number | thrift.Int64): Buffer => {
3633+
}).then((data: number | string | thrift.Int64): Buffer => {
36343634
const result: IAddInt64__ResultArgs = { success: data };
36353635
output.writeMessageBegin("addInt64", thrift.MessageType.REPLY, requestId);
36363636
AddInt64__ResultCodec.encode(result, output);

src/tests/unit/fixtures/generated/strict-unions/shared/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ export interface ICode {
99
status?: thrift.Int64;
1010
}
1111
export interface ICodeArgs {
12-
status?: number | thrift.Int64;
12+
status?: number | string | thrift.Int64;
1313
}
1414
export const CodeCodec: thrift.IStructCodec<ICodeArgs, ICode> = {
1515
encode(args: ICodeArgs, output: thrift.TProtocol): void {
1616
const obj = {
17-
status: (typeof args.status === "number" ? new thrift.Int64(args.status) : args.status)
17+
status: (typeof args.status === "number" ? new thrift.Int64(args.status) : typeof args.status === "string" ? thrift.Int64.fromDecimalString(args.status) : args.status)
1818
};
1919
output.writeStructBegin("Code");
2020
if (obj.status != null) {
@@ -65,7 +65,7 @@ export class Code extends thrift.StructLike implements ICode {
6565
constructor(args: ICodeArgs = {}) {
6666
super();
6767
if (args.status != null) {
68-
const value_2: thrift.Int64 = (typeof args.status === "number" ? new thrift.Int64(args.status) : args.status);
68+
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);
6969
this.status = value_2;
7070
}
7171
}

src/tests/unit/fixtures/thrift-server/annotations_struct.solution.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ export interface IMyStruct {
44
}
55
export interface IMyStructArgs {
66
id?: number;
7-
bigID?: number | thrift.Int64;
7+
bigID?: number | string | thrift.Int64;
88
}
99
export const MyStructCodec: thrift.IStructCodec<IMyStructArgs, IMyStruct> = {
1010
encode(args: IMyStructArgs, output: thrift.TProtocol): void {
1111
const obj = {
1212
id: (args.id != null ? args.id : 45),
13-
bigID: (args.bigID != null ? (typeof args.bigID === "number" ? new thrift.Int64(args.bigID) : args.bigID) : thrift.Int64.fromDecimalString("23948234"))
13+
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"))
1414
};
1515
output.writeStructBegin("MyStruct");
1616
if (obj.id != null) {
@@ -100,7 +100,7 @@ export class MyStruct extends thrift.StructLike implements IMyStruct {
100100
this.id = value_3;
101101
}
102102
if (args.bigID != null) {
103-
const value_4: thrift.Int64 = (typeof args.bigID === "number" ? new thrift.Int64(args.bigID) : args.bigID);
103+
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);
104104
this.bigID = value_4;
105105
}
106106
}

0 commit comments

Comments
 (0)