Skip to content

Commit 9d30b3f

Browse files
hayeskevin-greene-ck
authored andcommitted
Add explicit type to temporary object when encoding structs and unions (#164)
1 parent a60bcb1 commit 9d30b3f

File tree

59 files changed

+276
-158
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+276
-158
lines changed

src/main/render/thrift-server/struct/encode.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ export function createTempVariables(
5454
return [
5555
createConstStatement(
5656
COMMON_IDENTIFIERS.obj,
57-
undefined,
57+
ts.createTypeReferenceNode(
58+
ts.createIdentifier(looseNameForStruct(node, state)),
59+
undefined,
60+
),
5861
ts.createObjectLiteral(
5962
node.fields.map(
6063
(

src/tests/unit/fixtures/apache/generated/SharedStruct.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import * as Code from "./Code";
88
export interface ISharedStructArgs {
99
code: Code.Code;
1010
value: string;
11+
mapWithDefault?: Map<string, string>;
1112
}
1213
export class SharedStruct {
1314
public code: Code.Code;
1415
public value: string;
16+
public mapWithDefault?: Map<string, string> = new Map([]);
1517
constructor(args: ISharedStructArgs) {
1618
if (args != null && args.code != null) {
1719
this.code = args.code;
@@ -25,6 +27,9 @@ export class SharedStruct {
2527
else {
2628
throw new thrift.Thrift.TProtocolException(thrift.Thrift.TProtocolExceptionType.UNKNOWN, "Required field[value] is unset!");
2729
}
30+
if (args != null && args.mapWithDefault != null) {
31+
this.mapWithDefault = args.mapWithDefault;
32+
}
2833
}
2934
public write(output: thrift.TProtocol): void {
3035
output.writeStructBegin("SharedStruct");
@@ -38,6 +43,16 @@ export class SharedStruct {
3843
output.writeString(this.value);
3944
output.writeFieldEnd();
4045
}
46+
if (this.mapWithDefault != null) {
47+
output.writeFieldBegin("mapWithDefault", thrift.Thrift.Type.MAP, 3);
48+
output.writeMapBegin(thrift.Thrift.Type.STRING, thrift.Thrift.Type.STRING, this.mapWithDefault.size);
49+
this.mapWithDefault.forEach((value_1: string, key_1: string): void => {
50+
output.writeString(key_1);
51+
output.writeString(value_1);
52+
});
53+
output.writeMapEnd();
54+
output.writeFieldEnd();
55+
}
4156
output.writeFieldStop();
4257
output.writeStructEnd();
4358
return;
@@ -55,17 +70,34 @@ export class SharedStruct {
5570
switch (fieldId) {
5671
case 1:
5772
if (fieldType === thrift.Thrift.Type.STRUCT) {
58-
const value_1: Code.Code = Code.Code.read(input);
59-
_args.code = value_1;
73+
const value_2: Code.Code = Code.Code.read(input);
74+
_args.code = value_2;
6075
}
6176
else {
6277
input.skip(fieldType);
6378
}
6479
break;
6580
case 2:
6681
if (fieldType === thrift.Thrift.Type.STRING) {
67-
const value_2: string = input.readString();
68-
_args.value = value_2;
82+
const value_3: string = input.readString();
83+
_args.value = value_3;
84+
}
85+
else {
86+
input.skip(fieldType);
87+
}
88+
break;
89+
case 3:
90+
if (fieldType === thrift.Thrift.Type.MAP) {
91+
const value_4: Map<string, string> = new Map<string, string>();
92+
const metadata_1: thrift.TMap = input.readMapBegin();
93+
const size_1: number = metadata_1.size;
94+
for (let i_1: number = 0; i_1 < size_1; i_1++) {
95+
const key_2: string = input.readString();
96+
const value_5: string = input.readString();
97+
value_4.set(key_2, value_5);
98+
}
99+
input.readMapEnd();
100+
_args.mapWithDefault = value_4;
69101
}
70102
else {
71103
input.skip(fieldType);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface IMyExceptionArgs {
99
}
1010
export const MyExceptionCodec: thrift.IStructCodec<IMyExceptionArgs, IMyException> = {
1111
encode(args: IMyExceptionArgs, output: thrift.TProtocol): void {
12-
const obj = {
12+
const obj: IMyExceptionArgs = {
1313
message: args.message,
1414
code: (args.code != null ? args.code : 200)
1515
};

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface IUserArgs {
99
}
1010
export const UserCodec: thrift.IStructCodec<IUserArgs, IUser> = {
1111
encode(args: IUserArgs, output: thrift.TProtocol): void {
12-
const obj = {
12+
const obj: IUserArgs = {
1313
name: args.name,
1414
id: args.id
1515
};
@@ -160,7 +160,7 @@ export interface IGetUser__ArgsArgs {
160160
}
161161
export const GetUser__ArgsCodec: thrift.IStructCodec<IGetUser__ArgsArgs, IGetUser__Args> = {
162162
encode(args: IGetUser__ArgsArgs, output: thrift.TProtocol): void {
163-
const obj = {
163+
const obj: IGetUser__ArgsArgs = {
164164
id: args.id
165165
};
166166
output.writeStructBegin("GetUser__Args");
@@ -248,7 +248,7 @@ export interface ISaveUser__ArgsArgs {
248248
}
249249
export const SaveUser__ArgsCodec: thrift.IStructCodec<ISaveUser__ArgsArgs, ISaveUser__Args> = {
250250
encode(args: ISaveUser__ArgsArgs, output: thrift.TProtocol): void {
251-
const obj = {
251+
const obj: ISaveUser__ArgsArgs = {
252252
user: args.user
253253
};
254254
output.writeStructBegin("SaveUser__Args");
@@ -387,7 +387,7 @@ export interface IGetUser__ResultArgs {
387387
}
388388
export const GetUser__ResultCodec: thrift.IStructCodec<IGetUser__ResultArgs, IGetUser__Result> = {
389389
encode(args: IGetUser__ResultArgs, output: thrift.TProtocol): void {
390-
const obj = {
390+
const obj: IGetUser__ResultArgs = {
391391
success: args.success
392392
};
393393
output.writeStructBegin("GetUser__Result");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface IMyStructArgs {
99
}
1010
export const MyStructCodec: thrift.IStructCodec<IMyStructArgs, IMyStruct> = {
1111
encode(args: IMyStructArgs, output: thrift.TProtocol): void {
12-
const obj = {
12+
const obj: IMyStructArgs = {
1313
id: (args.id != null ? args.id : 45),
1414
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"))
1515
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface IMyUnionArgs {
1010
export const MyUnionCodec: thrift.IStructCodec<IMyUnionArgs, IMyUnion> = {
1111
encode(args: IMyUnionArgs, output: thrift.TProtocol): void {
1212
let _fieldsSet: number = 0;
13-
const obj = {
13+
const obj: IMyUnionArgs = {
1414
field1: args.field1,
1515
field2: (typeof args.field2 === "number" ? new thrift.Int64(args.field2) : typeof args.field2 === "string" ? thrift.Int64.fromDecimalString(args.field2) : args.field2)
1616
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface IMyExceptionArgs {
99
}
1010
export const MyExceptionCodec: thrift.IStructCodec<IMyExceptionArgs, IMyException> = {
1111
encode(args: IMyExceptionArgs, output: thrift.TProtocol): void {
12-
const obj = {
12+
const obj: IMyExceptionArgs = {
1313
message: args.message,
1414
code: (args.code != null ? args.code : 200)
1515
};

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface IUserArgs {
99
}
1010
export const UserCodec: thrift.IStructCodec<IUserArgs, IUser> = {
1111
encode(args: IUserArgs, output: thrift.TProtocol): void {
12-
const obj = {
12+
const obj: IUserArgs = {
1313
name: args.name,
1414
id: args.id
1515
};
@@ -148,7 +148,7 @@ export interface IGetUser__ArgsArgs {
148148
}
149149
export const GetUser__ArgsCodec: thrift.IStructCodec<IGetUser__ArgsArgs, IGetUser__Args> = {
150150
encode(args: IGetUser__ArgsArgs, output: thrift.TProtocol): void {
151-
const obj = {
151+
const obj: IGetUser__ArgsArgs = {
152152
id: args.id
153153
};
154154
output.writeStructBegin("GetUser__Args");
@@ -236,7 +236,7 @@ export interface ISaveUser__ArgsArgs {
236236
}
237237
export const SaveUser__ArgsCodec: thrift.IStructCodec<ISaveUser__ArgsArgs, ISaveUser__Args> = {
238238
encode(args: ISaveUser__ArgsArgs, output: thrift.TProtocol): void {
239-
const obj = {
239+
const obj: ISaveUser__ArgsArgs = {
240240
user: args.user
241241
};
242242
output.writeStructBegin("SaveUser__Args");
@@ -375,7 +375,7 @@ export interface IGetUser__ResultArgs {
375375
}
376376
export const GetUser__ResultCodec: thrift.IStructCodec<IGetUser__ResultArgs, IGetUser__Result> = {
377377
encode(args: IGetUser__ResultArgs, output: thrift.TProtocol): void {
378-
const obj = {
378+
const obj: IGetUser__ResultArgs = {
379379
success: args.success
380380
};
381381
output.writeStructBegin("GetUser__Result");

src/tests/unit/fixtures/thrift-server/basic_service.strict_union.solution.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const MyUnionCodec: thrift.IStructToolkit<MyUnionArgs, MyUnion> = {
6666
},
6767
encode(args: MyUnionArgs, output: thrift.TProtocol): void {
6868
let _fieldsSet: number = 0;
69-
const obj = {
69+
const obj: MyUnionArgs = {
7070
field1: args.field1,
7171
field2: (typeof args.field2 === "number" ? new thrift.Int64(args.field2) : typeof args.field2 === "string" ? thrift.Int64.fromDecimalString(args.field2) : args.field2)
7272
};
@@ -187,7 +187,7 @@ export interface IGetUser__ArgsArgs {
187187
}
188188
export const GetUser__ArgsCodec: thrift.IStructCodec<IGetUser__ArgsArgs, IGetUser__Args> = {
189189
encode(args: IGetUser__ArgsArgs, output: thrift.TProtocol): void {
190-
const obj = {
190+
const obj: IGetUser__ArgsArgs = {
191191
arg1: args.arg1
192192
};
193193
output.writeStructBegin("GetUser__Args");
@@ -326,7 +326,7 @@ export interface IGetUser__ResultArgs {
326326
}
327327
export const GetUser__ResultCodec: thrift.IStructCodec<IGetUser__ResultArgs, IGetUser__Result> = {
328328
encode(args: IGetUser__ResultArgs, output: thrift.TProtocol): void {
329-
const obj = {
329+
const obj: IGetUser__ResultArgs = {
330330
success: args.success
331331
};
332332
output.writeStructBegin("GetUser__Result");

src/tests/unit/fixtures/thrift-server/basic_struct.no_name.solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export interface IMyStructArgs {
66
}
77
export const MyStructCodec: thrift.IStructCodec<IMyStructArgs, IMyStruct> = {
88
encode(args: IMyStructArgs, output: thrift.TProtocol): void {
9-
const obj = {
9+
const obj: IMyStructArgs = {
1010
id: args.id
1111
};
1212
output.writeStructBegin("MyStruct");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface IMyStructArgs {
1515
}
1616
export const MyStructCodec: thrift.IStructCodec<IMyStructArgs, IMyStruct> = {
1717
encode(args: IMyStructArgs, output: thrift.TProtocol): void {
18-
const obj = {
18+
const obj: IMyStructArgs = {
1919
id: (args.id != null ? args.id : 45),
2020
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")),
2121
word: args.word,

src/tests/unit/fixtures/thrift-server/basic_union.no_name.solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface IMyUnionArgs {
99
export const MyUnionCodec: thrift.IStructCodec<IMyUnionArgs, IMyUnion> = {
1010
encode(args: IMyUnionArgs, output: thrift.TProtocol): void {
1111
let _fieldsSet: number = 0;
12-
const obj = {
12+
const obj: IMyUnionArgs = {
1313
option1: args.option1,
1414
option2: (typeof args.option2 === "number" ? new thrift.Int64(args.option2) : typeof args.option2 === "string" ? thrift.Int64.fromDecimalString(args.option2) : args.option2)
1515
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface IMyUnionArgs {
1010
export const MyUnionCodec: thrift.IStructCodec<IMyUnionArgs, IMyUnion> = {
1111
encode(args: IMyUnionArgs, output: thrift.TProtocol): void {
1212
let _fieldsSet: number = 0;
13-
const obj = {
13+
const obj: IMyUnionArgs = {
1414
option1: args.option1,
1515
option2: (typeof args.option2 === "number" ? new thrift.Int64(args.option2) : typeof args.option2 === "string" ? thrift.Int64.fromDecimalString(args.option2) : args.option2)
1616
};

src/tests/unit/fixtures/thrift-server/basic_union.strict_union.solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const MyUnionCodec: thrift.IStructToolkit<MyUnionArgs, MyUnion> = {
6666
},
6767
encode(args: MyUnionArgs, output: thrift.TProtocol): void {
6868
let _fieldsSet: number = 0;
69-
const obj = {
69+
const obj: MyUnionArgs = {
7070
field1: args.field1,
7171
field2: (typeof args.field2 === "number" ? new thrift.Int64(args.field2) : typeof args.field2 === "string" ? thrift.Int64.fromDecimalString(args.field2) : args.field2)
7272
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface IOtherStructArgs {
99
}
1010
export const OtherStructCodec: thrift.IStructCodec<IOtherStructArgs, IOtherStruct> = {
1111
encode(args: IOtherStructArgs, output: thrift.TProtocol): void {
12-
const obj = {
12+
const obj: IOtherStructArgs = {
1313
id: (typeof args.id === "number" ? new thrift.Int64(args.id) : typeof args.id === "string" ? thrift.Int64.fromDecimalString(args.id) : args.id),
1414
name: (args.name != null ? (typeof args.name === "string" ? Buffer.from(args.name) : args.name) : Buffer.from("John"))
1515
};
@@ -132,7 +132,7 @@ export interface IMyStructArgs {
132132
}
133133
export const MyStructCodec: thrift.IStructCodec<IMyStructArgs, IMyStruct> = {
134134
encode(args: IMyStructArgs, output: thrift.TProtocol): void {
135-
const obj = {
135+
const obj: IMyStructArgs = {
136136
idList: args.idList,
137137
idMap: args.idMap,
138138
idMapList: args.idMapList,

src/tests/unit/fixtures/thrift-server/complex_typedef.strict_union.solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export const MyUnionCodec: thrift.IStructToolkit<MyUnionArgs, MyUnion> = {
7070
},
7171
encode(args: MyUnionArgs, output: thrift.TProtocol): void {
7272
let _fieldsSet: number = 0;
73-
const obj = {
73+
const obj: MyUnionArgs = {
7474
option1: args.option1,
7575
option2: args.option2
7676
};

src/tests/unit/fixtures/thrift-server/generated-strict/Code.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface ICodeArgs {
1313
}
1414
export const CodeCodec: thrift.IStructCodec<ICodeArgs, ICode> = {
1515
encode(args: ICodeArgs, output: thrift.TProtocol): void {
16-
const obj = {
16+
const obj: ICodeArgs = {
1717
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");

src/tests/unit/fixtures/thrift-server/generated-strict/SharedService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export interface IGetUnion__ArgsArgs {
4040
}
4141
export const GetUnion__ArgsCodec: thrift.IStructCodec<IGetUnion__ArgsArgs, IGetUnion__Args> = {
4242
encode(args: IGetUnion__ArgsArgs, output: thrift.TProtocol): void {
43-
const obj = {
43+
const obj: IGetUnion__ArgsArgs = {
4444
index: args.index
4545
};
4646
output.writeStructBegin("GetUnion__Args");
@@ -179,7 +179,7 @@ export interface IGetUnion__ResultArgs {
179179
}
180180
export const GetUnion__ResultCodec: thrift.IStructCodec<IGetUnion__ResultArgs, IGetUnion__Result> = {
181181
encode(args: IGetUnion__ResultArgs, output: thrift.TProtocol): void {
182-
const obj = {
182+
const obj: IGetUnion__ResultArgs = {
183183
success: args.success
184184
};
185185
output.writeStructBegin("GetUnion__Result");
@@ -256,7 +256,7 @@ export interface IGetEnum__ResultArgs {
256256
}
257257
export const GetEnum__ResultCodec: thrift.IStructCodec<IGetEnum__ResultArgs, IGetEnum__Result> = {
258258
encode(args: IGetEnum__ResultArgs, output: thrift.TProtocol): void {
259-
const obj = {
259+
const obj: IGetEnum__ResultArgs = {
260260
success: args.success
261261
};
262262
output.writeStructBegin("GetEnum__Result");

src/tests/unit/fixtures/thrift-server/generated-strict/SharedServiceBase.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface IGetStruct__ArgsArgs {
2828
}
2929
export const GetStruct__ArgsCodec: thrift.IStructCodec<IGetStruct__ArgsArgs, IGetStruct__Args> = {
3030
encode(args: IGetStruct__ArgsArgs, output: thrift.TProtocol): void {
31-
const obj = {
31+
const obj: IGetStruct__ArgsArgs = {
3232
key: args.key
3333
};
3434
output.writeStructBegin("GetStruct__Args");
@@ -116,7 +116,7 @@ export interface IGetStruct__ResultArgs {
116116
}
117117
export const GetStruct__ResultCodec: thrift.IStructCodec<IGetStruct__ResultArgs, IGetStruct__Result> = {
118118
encode(args: IGetStruct__ResultArgs, output: thrift.TProtocol): void {
119-
const obj = {
119+
const obj: IGetStruct__ResultArgs = {
120120
success: args.success
121121
};
122122
output.writeStructBegin("GetStruct__Result");

0 commit comments

Comments
 (0)