Skip to content

Commit

Permalink
Add explicit type to temporary object when encoding structs and unions (
Browse files Browse the repository at this point in the history
  • Loading branch information
hayes authored and kevin-greene-ck committed Jul 2, 2019
1 parent a60bcb1 commit 9d30b3f
Show file tree
Hide file tree
Showing 59 changed files with 276 additions and 158 deletions.
5 changes: 4 additions & 1 deletion src/main/render/thrift-server/struct/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ export function createTempVariables(
return [
createConstStatement(
COMMON_IDENTIFIERS.obj,
undefined,
ts.createTypeReferenceNode(
ts.createIdentifier(looseNameForStruct(node, state)),
undefined,
),
ts.createObjectLiteral(
node.fields.map(
(
Expand Down
40 changes: 36 additions & 4 deletions src/tests/unit/fixtures/apache/generated/SharedStruct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import * as Code from "./Code";
export interface ISharedStructArgs {
code: Code.Code;
value: string;
mapWithDefault?: Map<string, string>;
}
export class SharedStruct {
public code: Code.Code;
public value: string;
public mapWithDefault?: Map<string, string> = new Map([]);
constructor(args: ISharedStructArgs) {
if (args != null && args.code != null) {
this.code = args.code;
Expand All @@ -25,6 +27,9 @@ export class SharedStruct {
else {
throw new thrift.Thrift.TProtocolException(thrift.Thrift.TProtocolExceptionType.UNKNOWN, "Required field[value] is unset!");
}
if (args != null && args.mapWithDefault != null) {
this.mapWithDefault = args.mapWithDefault;
}
}
public write(output: thrift.TProtocol): void {
output.writeStructBegin("SharedStruct");
Expand All @@ -38,6 +43,16 @@ export class SharedStruct {
output.writeString(this.value);
output.writeFieldEnd();
}
if (this.mapWithDefault != null) {
output.writeFieldBegin("mapWithDefault", thrift.Thrift.Type.MAP, 3);
output.writeMapBegin(thrift.Thrift.Type.STRING, thrift.Thrift.Type.STRING, this.mapWithDefault.size);
this.mapWithDefault.forEach((value_1: string, key_1: string): void => {
output.writeString(key_1);
output.writeString(value_1);
});
output.writeMapEnd();
output.writeFieldEnd();
}
output.writeFieldStop();
output.writeStructEnd();
return;
Expand All @@ -55,17 +70,34 @@ export class SharedStruct {
switch (fieldId) {
case 1:
if (fieldType === thrift.Thrift.Type.STRUCT) {
const value_1: Code.Code = Code.Code.read(input);
_args.code = value_1;
const value_2: Code.Code = Code.Code.read(input);
_args.code = value_2;
}
else {
input.skip(fieldType);
}
break;
case 2:
if (fieldType === thrift.Thrift.Type.STRING) {
const value_2: string = input.readString();
_args.value = value_2;
const value_3: string = input.readString();
_args.value = value_3;
}
else {
input.skip(fieldType);
}
break;
case 3:
if (fieldType === thrift.Thrift.Type.MAP) {
const value_4: Map<string, string> = new Map<string, string>();
const metadata_1: thrift.TMap = input.readMapBegin();
const size_1: number = metadata_1.size;
for (let i_1: number = 0; i_1 < size_1; i_1++) {
const key_2: string = input.readString();
const value_5: string = input.readString();
value_4.set(key_2, value_5);
}
input.readMapEnd();
_args.mapWithDefault = value_4;
}
else {
input.skip(fieldType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface IMyExceptionArgs {
}
export const MyExceptionCodec: thrift.IStructCodec<IMyExceptionArgs, IMyException> = {
encode(args: IMyExceptionArgs, output: thrift.TProtocol): void {
const obj = {
const obj: IMyExceptionArgs = {
message: args.message,
code: (args.code != null ? args.code : 200)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface IUserArgs {
}
export const UserCodec: thrift.IStructCodec<IUserArgs, IUser> = {
encode(args: IUserArgs, output: thrift.TProtocol): void {
const obj = {
const obj: IUserArgs = {
name: args.name,
id: args.id
};
Expand Down Expand Up @@ -160,7 +160,7 @@ export interface IGetUser__ArgsArgs {
}
export const GetUser__ArgsCodec: thrift.IStructCodec<IGetUser__ArgsArgs, IGetUser__Args> = {
encode(args: IGetUser__ArgsArgs, output: thrift.TProtocol): void {
const obj = {
const obj: IGetUser__ArgsArgs = {
id: args.id
};
output.writeStructBegin("GetUser__Args");
Expand Down Expand Up @@ -248,7 +248,7 @@ export interface ISaveUser__ArgsArgs {
}
export const SaveUser__ArgsCodec: thrift.IStructCodec<ISaveUser__ArgsArgs, ISaveUser__Args> = {
encode(args: ISaveUser__ArgsArgs, output: thrift.TProtocol): void {
const obj = {
const obj: ISaveUser__ArgsArgs = {
user: args.user
};
output.writeStructBegin("SaveUser__Args");
Expand Down Expand Up @@ -387,7 +387,7 @@ export interface IGetUser__ResultArgs {
}
export const GetUser__ResultCodec: thrift.IStructCodec<IGetUser__ResultArgs, IGetUser__Result> = {
encode(args: IGetUser__ResultArgs, output: thrift.TProtocol): void {
const obj = {
const obj: IGetUser__ResultArgs = {
success: args.success
};
output.writeStructBegin("GetUser__Result");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface IMyStructArgs {
}
export const MyStructCodec: thrift.IStructCodec<IMyStructArgs, IMyStruct> = {
encode(args: IMyStructArgs, output: thrift.TProtocol): void {
const obj = {
const obj: IMyStructArgs = {
id: (args.id != null ? args.id : 45),
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"))
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface IMyUnionArgs {
export const MyUnionCodec: thrift.IStructCodec<IMyUnionArgs, IMyUnion> = {
encode(args: IMyUnionArgs, output: thrift.TProtocol): void {
let _fieldsSet: number = 0;
const obj = {
const obj: IMyUnionArgs = {
field1: args.field1,
field2: (typeof args.field2 === "number" ? new thrift.Int64(args.field2) : typeof args.field2 === "string" ? thrift.Int64.fromDecimalString(args.field2) : args.field2)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface IMyExceptionArgs {
}
export const MyExceptionCodec: thrift.IStructCodec<IMyExceptionArgs, IMyException> = {
encode(args: IMyExceptionArgs, output: thrift.TProtocol): void {
const obj = {
const obj: IMyExceptionArgs = {
message: args.message,
code: (args.code != null ? args.code : 200)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface IUserArgs {
}
export const UserCodec: thrift.IStructCodec<IUserArgs, IUser> = {
encode(args: IUserArgs, output: thrift.TProtocol): void {
const obj = {
const obj: IUserArgs = {
name: args.name,
id: args.id
};
Expand Down Expand Up @@ -148,7 +148,7 @@ export interface IGetUser__ArgsArgs {
}
export const GetUser__ArgsCodec: thrift.IStructCodec<IGetUser__ArgsArgs, IGetUser__Args> = {
encode(args: IGetUser__ArgsArgs, output: thrift.TProtocol): void {
const obj = {
const obj: IGetUser__ArgsArgs = {
id: args.id
};
output.writeStructBegin("GetUser__Args");
Expand Down Expand Up @@ -236,7 +236,7 @@ export interface ISaveUser__ArgsArgs {
}
export const SaveUser__ArgsCodec: thrift.IStructCodec<ISaveUser__ArgsArgs, ISaveUser__Args> = {
encode(args: ISaveUser__ArgsArgs, output: thrift.TProtocol): void {
const obj = {
const obj: ISaveUser__ArgsArgs = {
user: args.user
};
output.writeStructBegin("SaveUser__Args");
Expand Down Expand Up @@ -375,7 +375,7 @@ export interface IGetUser__ResultArgs {
}
export const GetUser__ResultCodec: thrift.IStructCodec<IGetUser__ResultArgs, IGetUser__Result> = {
encode(args: IGetUser__ResultArgs, output: thrift.TProtocol): void {
const obj = {
const obj: IGetUser__ResultArgs = {
success: args.success
};
output.writeStructBegin("GetUser__Result");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const MyUnionCodec: thrift.IStructToolkit<MyUnionArgs, MyUnion> = {
},
encode(args: MyUnionArgs, output: thrift.TProtocol): void {
let _fieldsSet: number = 0;
const obj = {
const obj: MyUnionArgs = {
field1: args.field1,
field2: (typeof args.field2 === "number" ? new thrift.Int64(args.field2) : typeof args.field2 === "string" ? thrift.Int64.fromDecimalString(args.field2) : args.field2)
};
Expand Down Expand Up @@ -187,7 +187,7 @@ export interface IGetUser__ArgsArgs {
}
export const GetUser__ArgsCodec: thrift.IStructCodec<IGetUser__ArgsArgs, IGetUser__Args> = {
encode(args: IGetUser__ArgsArgs, output: thrift.TProtocol): void {
const obj = {
const obj: IGetUser__ArgsArgs = {
arg1: args.arg1
};
output.writeStructBegin("GetUser__Args");
Expand Down Expand Up @@ -326,7 +326,7 @@ export interface IGetUser__ResultArgs {
}
export const GetUser__ResultCodec: thrift.IStructCodec<IGetUser__ResultArgs, IGetUser__Result> = {
encode(args: IGetUser__ResultArgs, output: thrift.TProtocol): void {
const obj = {
const obj: IGetUser__ResultArgs = {
success: args.success
};
output.writeStructBegin("GetUser__Result");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface IMyStructArgs {
}
export const MyStructCodec: thrift.IStructCodec<IMyStructArgs, IMyStruct> = {
encode(args: IMyStructArgs, output: thrift.TProtocol): void {
const obj = {
const obj: IMyStructArgs = {
id: args.id
};
output.writeStructBegin("MyStruct");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface IMyStructArgs {
}
export const MyStructCodec: thrift.IStructCodec<IMyStructArgs, IMyStruct> = {
encode(args: IMyStructArgs, output: thrift.TProtocol): void {
const obj = {
const obj: IMyStructArgs = {
id: (args.id != null ? args.id : 45),
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")),
word: args.word,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface IMyUnionArgs {
export const MyUnionCodec: thrift.IStructCodec<IMyUnionArgs, IMyUnion> = {
encode(args: IMyUnionArgs, output: thrift.TProtocol): void {
let _fieldsSet: number = 0;
const obj = {
const obj: IMyUnionArgs = {
option1: args.option1,
option2: (typeof args.option2 === "number" ? new thrift.Int64(args.option2) : typeof args.option2 === "string" ? thrift.Int64.fromDecimalString(args.option2) : args.option2)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface IMyUnionArgs {
export const MyUnionCodec: thrift.IStructCodec<IMyUnionArgs, IMyUnion> = {
encode(args: IMyUnionArgs, output: thrift.TProtocol): void {
let _fieldsSet: number = 0;
const obj = {
const obj: IMyUnionArgs = {
option1: args.option1,
option2: (typeof args.option2 === "number" ? new thrift.Int64(args.option2) : typeof args.option2 === "string" ? thrift.Int64.fromDecimalString(args.option2) : args.option2)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const MyUnionCodec: thrift.IStructToolkit<MyUnionArgs, MyUnion> = {
},
encode(args: MyUnionArgs, output: thrift.TProtocol): void {
let _fieldsSet: number = 0;
const obj = {
const obj: MyUnionArgs = {
field1: args.field1,
field2: (typeof args.field2 === "number" ? new thrift.Int64(args.field2) : typeof args.field2 === "string" ? thrift.Int64.fromDecimalString(args.field2) : args.field2)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface IOtherStructArgs {
}
export const OtherStructCodec: thrift.IStructCodec<IOtherStructArgs, IOtherStruct> = {
encode(args: IOtherStructArgs, output: thrift.TProtocol): void {
const obj = {
const obj: IOtherStructArgs = {
id: (typeof args.id === "number" ? new thrift.Int64(args.id) : typeof args.id === "string" ? thrift.Int64.fromDecimalString(args.id) : args.id),
name: (args.name != null ? (typeof args.name === "string" ? Buffer.from(args.name) : args.name) : Buffer.from("John"))
};
Expand Down Expand Up @@ -132,7 +132,7 @@ export interface IMyStructArgs {
}
export const MyStructCodec: thrift.IStructCodec<IMyStructArgs, IMyStruct> = {
encode(args: IMyStructArgs, output: thrift.TProtocol): void {
const obj = {
const obj: IMyStructArgs = {
idList: args.idList,
idMap: args.idMap,
idMapList: args.idMapList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const MyUnionCodec: thrift.IStructToolkit<MyUnionArgs, MyUnion> = {
},
encode(args: MyUnionArgs, output: thrift.TProtocol): void {
let _fieldsSet: number = 0;
const obj = {
const obj: MyUnionArgs = {
option1: args.option1,
option2: args.option2
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface ICodeArgs {
}
export const CodeCodec: thrift.IStructCodec<ICodeArgs, ICode> = {
encode(args: ICodeArgs, output: thrift.TProtocol): void {
const obj = {
const obj: ICodeArgs = {
status: (typeof args.status === "number" ? new thrift.Int64(args.status) : typeof args.status === "string" ? thrift.Int64.fromDecimalString(args.status) : args.status)
};
output.writeStructBegin("Code");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface IGetUnion__ArgsArgs {
}
export const GetUnion__ArgsCodec: thrift.IStructCodec<IGetUnion__ArgsArgs, IGetUnion__Args> = {
encode(args: IGetUnion__ArgsArgs, output: thrift.TProtocol): void {
const obj = {
const obj: IGetUnion__ArgsArgs = {
index: args.index
};
output.writeStructBegin("GetUnion__Args");
Expand Down Expand Up @@ -179,7 +179,7 @@ export interface IGetUnion__ResultArgs {
}
export const GetUnion__ResultCodec: thrift.IStructCodec<IGetUnion__ResultArgs, IGetUnion__Result> = {
encode(args: IGetUnion__ResultArgs, output: thrift.TProtocol): void {
const obj = {
const obj: IGetUnion__ResultArgs = {
success: args.success
};
output.writeStructBegin("GetUnion__Result");
Expand Down Expand Up @@ -256,7 +256,7 @@ export interface IGetEnum__ResultArgs {
}
export const GetEnum__ResultCodec: thrift.IStructCodec<IGetEnum__ResultArgs, IGetEnum__Result> = {
encode(args: IGetEnum__ResultArgs, output: thrift.TProtocol): void {
const obj = {
const obj: IGetEnum__ResultArgs = {
success: args.success
};
output.writeStructBegin("GetEnum__Result");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface IGetStruct__ArgsArgs {
}
export const GetStruct__ArgsCodec: thrift.IStructCodec<IGetStruct__ArgsArgs, IGetStruct__Args> = {
encode(args: IGetStruct__ArgsArgs, output: thrift.TProtocol): void {
const obj = {
const obj: IGetStruct__ArgsArgs = {
key: args.key
};
output.writeStructBegin("GetStruct__Args");
Expand Down Expand Up @@ -116,7 +116,7 @@ export interface IGetStruct__ResultArgs {
}
export const GetStruct__ResultCodec: thrift.IStructCodec<IGetStruct__ResultArgs, IGetStruct__Result> = {
encode(args: IGetStruct__ResultArgs, output: thrift.TProtocol): void {
const obj = {
const obj: IGetStruct__ResultArgs = {
success: args.success
};
output.writeStructBegin("GetStruct__Result");
Expand Down
Loading

0 comments on commit 9d30b3f

Please sign in to comment.