Skip to content

Commit

Permalink
fix: update how type for temp object in encode is defined to fix stri…
Browse files Browse the repository at this point in the history
…ct unions (#167)

* fix: update how type for temp object in encode is defined to fix strict unions

* Add args interface for unions with default value
  • Loading branch information
hayes authored and kevin-greene-ck committed Aug 12, 2019
1 parent 58fd196 commit 971cb19
Show file tree
Hide file tree
Showing 59 changed files with 368 additions and 302 deletions.
53 changes: 31 additions & 22 deletions src/main/render/thrift-server/struct/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {

import { DefinitionType, IRenderState } from '../../../types'

import { createLetStatement } from '../../shared/utils'
import { looseNameForStruct, throwForField, toolkitName } from './utils'

export function createTempVariables(
Expand All @@ -50,32 +51,40 @@ export function createTempVariables(
},
)

// Unions use reassignment for defaults
const createVariable = withDefault
? createConstStatement
: createLetStatement

if (structFields.length > 0) {
return [
createConstStatement(
createVariable(
COMMON_IDENTIFIERS.obj,
ts.createTypeReferenceNode(
ts.createIdentifier(looseNameForStruct(node, state)),
undefined,
),
ts.createObjectLiteral(
node.fields.map(
(
next: FieldDefinition,
): ts.ObjectLiteralElementLike => {
return ts.createPropertyAssignment(
next.name.value,
getInitializerForField(
COMMON_IDENTIFIERS.args,
next,
state,
withDefault,
true,
),
)
},
undefined,
ts.createAsExpression(
ts.createObjectLiteral(
node.fields.map(
(
next: FieldDefinition,
): ts.ObjectLiteralElementLike => {
return ts.createPropertyAssignment(
next.name.value,
getInitializerForField(
COMMON_IDENTIFIERS.args,
next,
state,
withDefault,
true,
),
)
},
),
true, // multiline
),
ts.createTypeReferenceNode(
ts.createIdentifier(looseNameForStruct(node, state)),
undefined,
),
true, // multiline
),
),
]
Expand Down
22 changes: 12 additions & 10 deletions src/main/render/thrift-server/union/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,19 @@ function checkDefaults(
[
ts.createStatement(
ts.createAssignment(
ts.createPropertyAccess(
COMMON_IDENTIFIERS.obj,
ts.createIdentifier(
defaultField.name.value,
COMMON_IDENTIFIERS.obj,
ts.createObjectLiteral([
ts.createPropertyAssignment(
ts.createIdentifier(
defaultField.name.value,
),
renderValue(
defaultField.fieldType,
defaultField.defaultValue!,
state,
),
),
),
renderValue(
defaultField.fieldType,
defaultField.defaultValue!,
state,
),
]),
),
),
],
Expand Down
43 changes: 43 additions & 0 deletions src/main/render/thrift-server/union/union-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export function fieldInterfaceName(
}
}

export function defaultInterfaceName(nodeName: string) {
return `I${nodeName}DefaultArgs`
}

function renderInterfaceForField(
node: UnionDefinition,
field: FieldDefinition,
Expand Down Expand Up @@ -158,6 +162,30 @@ function renderInterfaceForField(
)
}

function renderInterfaceForDefault(
node: UnionDefinition,
isExported: boolean,
): ts.InterfaceDeclaration {
const signatures = node.fields.map((next: FieldDefinition) => {
return ts.createPropertySignature(
undefined,
next.name.value,
ts.createToken(ts.SyntaxKind.QuestionToken),
createUndefinedType(),
undefined,
)
})

return ts.createInterfaceDeclaration(
undefined,
tokens(isExported),
ts.createIdentifier(defaultInterfaceName(node.name.value)),
[],
[],
signatures,
)
}

export function renderUnionsForFields(
node: UnionDefinition,
state: IRenderState,
Expand All @@ -181,6 +209,14 @@ export function renderUnionsForFields(
undefined,
)
}),
...(isStrict || !hasDefault(node)
? []
: [
ts.createTypeReferenceNode(
defaultInterfaceName(node.name.value),
undefined,
),
]),
]),
),
...node.fields.map(
Expand All @@ -194,5 +230,12 @@ export function renderUnionsForFields(
)
},
),
...(isStrict || !hasDefault(node)
? []
: [renderInterfaceForDefault(node, true)]),
]
}

export function hasDefault(node: UnionDefinition) {
return node.fields.find((field) => field.defaultValue !== null)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export interface IMyExceptionArgs {
}
export const MyExceptionCodec: thrift.IStructCodec<IMyExceptionArgs, IMyException> = {
encode(args: IMyExceptionArgs, output: thrift.TProtocol): void {
const obj: IMyExceptionArgs = {
const obj = ({
message: args.message,
code: (args.code != null ? args.code : 200)
};
} as IMyExceptionArgs);
output.writeStructBegin("MyException");
if (obj.message != null) {
output.writeFieldBegin("message", thrift.TType.STRING, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export interface IUserArgs {
}
export const UserCodec: thrift.IStructCodec<IUserArgs, IUser> = {
encode(args: IUserArgs, output: thrift.TProtocol): void {
const obj: IUserArgs = {
const obj = ({
name: args.name,
id: args.id
};
} as IUserArgs);
output.writeStructBegin("User");
if (obj.name != null) {
output.writeFieldBegin("name", thrift.TType.STRING, 1);
Expand Down Expand Up @@ -160,9 +160,9 @@ export interface IGetUser__ArgsArgs {
}
export const GetUser__ArgsCodec: thrift.IStructCodec<IGetUser__ArgsArgs, IGetUser__Args> = {
encode(args: IGetUser__ArgsArgs, output: thrift.TProtocol): void {
const obj: IGetUser__ArgsArgs = {
const obj = ({
id: args.id
};
} as IGetUser__ArgsArgs);
output.writeStructBegin("GetUser__Args");
if (obj.id != null) {
output.writeFieldBegin("id", thrift.TType.I32, 1);
Expand Down Expand Up @@ -248,9 +248,9 @@ export interface ISaveUser__ArgsArgs {
}
export const SaveUser__ArgsCodec: thrift.IStructCodec<ISaveUser__ArgsArgs, ISaveUser__Args> = {
encode(args: ISaveUser__ArgsArgs, output: thrift.TProtocol): void {
const obj: ISaveUser__ArgsArgs = {
const obj = ({
user: args.user
};
} as ISaveUser__ArgsArgs);
output.writeStructBegin("SaveUser__Args");
if (obj.user != null) {
output.writeFieldBegin("user", thrift.TType.STRUCT, 1);
Expand Down Expand Up @@ -387,9 +387,9 @@ export interface IGetUser__ResultArgs {
}
export const GetUser__ResultCodec: thrift.IStructCodec<IGetUser__ResultArgs, IGetUser__Result> = {
encode(args: IGetUser__ResultArgs, output: thrift.TProtocol): void {
const obj: IGetUser__ResultArgs = {
let obj = ({
success: args.success
};
} as IGetUser__ResultArgs);
output.writeStructBegin("GetUser__Result");
if (obj.success != null) {
output.writeFieldBegin("success", thrift.TType.STRUCT, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export interface IMyStructArgs {
}
export const MyStructCodec: thrift.IStructCodec<IMyStructArgs, IMyStruct> = {
encode(args: IMyStructArgs, output: thrift.TProtocol): void {
const obj: IMyStructArgs = {
const obj = ({
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"))
};
} as IMyStructArgs);
output.writeStructBegin("MyStruct");
if (obj.id != null) {
output.writeFieldBegin("id", thrift.TType.I32, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export interface IMyUnionArgs {
export const MyUnionCodec: thrift.IStructCodec<IMyUnionArgs, IMyUnion> = {
encode(args: IMyUnionArgs, output: thrift.TProtocol): void {
let _fieldsSet: number = 0;
const obj: IMyUnionArgs = {
let obj = ({
field1: args.field1,
field2: (typeof args.field2 === "number" ? new thrift.Int64(args.field2) : typeof args.field2 === "string" ? thrift.Int64.fromDecimalString(args.field2) : args.field2)
};
} as IMyUnionArgs);
output.writeStructBegin("MyUnion");
if (obj.field1 != null) {
_fieldsSet++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export interface IMyExceptionArgs {
}
export const MyExceptionCodec: thrift.IStructCodec<IMyExceptionArgs, IMyException> = {
encode(args: IMyExceptionArgs, output: thrift.TProtocol): void {
const obj: IMyExceptionArgs = {
const obj = ({
message: args.message,
code: (args.code != null ? args.code : 200)
};
} as IMyExceptionArgs);
output.writeStructBegin("MyException");
if (obj.message != null) {
output.writeFieldBegin("message", thrift.TType.STRING, 1);
Expand Down
16 changes: 8 additions & 8 deletions src/tests/unit/fixtures/thrift-server/basic_service.solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export interface IUserArgs {
}
export const UserCodec: thrift.IStructCodec<IUserArgs, IUser> = {
encode(args: IUserArgs, output: thrift.TProtocol): void {
const obj: IUserArgs = {
const obj = ({
name: args.name,
id: args.id
};
} as IUserArgs);
output.writeStructBegin("User");
if (obj.name != null) {
output.writeFieldBegin("name", thrift.TType.STRING, 1);
Expand Down Expand Up @@ -148,9 +148,9 @@ export interface IGetUser__ArgsArgs {
}
export const GetUser__ArgsCodec: thrift.IStructCodec<IGetUser__ArgsArgs, IGetUser__Args> = {
encode(args: IGetUser__ArgsArgs, output: thrift.TProtocol): void {
const obj: IGetUser__ArgsArgs = {
const obj = ({
id: args.id
};
} as IGetUser__ArgsArgs);
output.writeStructBegin("GetUser__Args");
if (obj.id != null) {
output.writeFieldBegin("id", thrift.TType.I32, 1);
Expand Down Expand Up @@ -236,9 +236,9 @@ export interface ISaveUser__ArgsArgs {
}
export const SaveUser__ArgsCodec: thrift.IStructCodec<ISaveUser__ArgsArgs, ISaveUser__Args> = {
encode(args: ISaveUser__ArgsArgs, output: thrift.TProtocol): void {
const obj: ISaveUser__ArgsArgs = {
const obj = ({
user: args.user
};
} as ISaveUser__ArgsArgs);
output.writeStructBegin("SaveUser__Args");
if (obj.user != null) {
output.writeFieldBegin("user", thrift.TType.STRUCT, 1);
Expand Down Expand Up @@ -375,9 +375,9 @@ export interface IGetUser__ResultArgs {
}
export const GetUser__ResultCodec: thrift.IStructCodec<IGetUser__ResultArgs, IGetUser__Result> = {
encode(args: IGetUser__ResultArgs, output: thrift.TProtocol): void {
const obj: IGetUser__ResultArgs = {
let obj = ({
success: args.success
};
} as IGetUser__ResultArgs);
output.writeStructBegin("GetUser__Result");
if (obj.success != null) {
output.writeFieldBegin("success", thrift.TType.STRUCT, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface IMyUnionWithField2 {
field1?: undefined;
field2: thrift.Int64;
}
export type MyUnionArgs = IMyUnionWithField1Args | IMyUnionWithField2Args;
export type MyUnionArgs = IMyUnionWithField1Args | IMyUnionWithField2Args | IMyUnionDefaultArgs;
export interface IMyUnionWithField1Args {
field1: number;
field2?: undefined;
Expand All @@ -24,6 +24,10 @@ export interface IMyUnionWithField2Args {
field1?: undefined;
field2: number | string | thrift.Int64;
}
export interface IMyUnionDefaultArgs {
field1?: undefined;
field2?: undefined;
}
export const MyUnionCodec: thrift.IStructToolkit<MyUnionArgs, MyUnion> = {
create(args: MyUnionArgs): MyUnion {
let _fieldsSet: number = 0;
Expand Down Expand Up @@ -66,10 +70,10 @@ export const MyUnionCodec: thrift.IStructToolkit<MyUnionArgs, MyUnion> = {
},
encode(args: MyUnionArgs, output: thrift.TProtocol): void {
let _fieldsSet: number = 0;
const obj: MyUnionArgs = {
let obj = ({
field1: args.field1,
field2: (typeof args.field2 === "number" ? new thrift.Int64(args.field2) : typeof args.field2 === "string" ? thrift.Int64.fromDecimalString(args.field2) : args.field2)
};
} as MyUnionArgs);
output.writeStructBegin("MyUnion");
if (obj.field1 != null) {
_fieldsSet++;
Expand Down Expand Up @@ -187,9 +191,9 @@ export interface IGetUser__ArgsArgs {
}
export const GetUser__ArgsCodec: thrift.IStructCodec<IGetUser__ArgsArgs, IGetUser__Args> = {
encode(args: IGetUser__ArgsArgs, output: thrift.TProtocol): void {
const obj: IGetUser__ArgsArgs = {
let obj = ({
arg1: args.arg1
};
} as IGetUser__ArgsArgs);
output.writeStructBegin("GetUser__Args");
if (obj.arg1 != null) {
output.writeFieldBegin("arg1", thrift.TType.STRUCT, 1);
Expand Down Expand Up @@ -326,9 +330,9 @@ export interface IGetUser__ResultArgs {
}
export const GetUser__ResultCodec: thrift.IStructCodec<IGetUser__ResultArgs, IGetUser__Result> = {
encode(args: IGetUser__ResultArgs, output: thrift.TProtocol): void {
const obj: IGetUser__ResultArgs = {
let obj = ({
success: args.success
};
} as IGetUser__ResultArgs);
output.writeStructBegin("GetUser__Result");
if (obj.success != null) {
output.writeFieldBegin("success", thrift.TType.STRING, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export interface IMyStructArgs {
}
export const MyStructCodec: thrift.IStructCodec<IMyStructArgs, IMyStruct> = {
encode(args: IMyStructArgs, output: thrift.TProtocol): void {
const obj: IMyStructArgs = {
const obj = ({
id: args.id
};
} as IMyStructArgs);
output.writeStructBegin("MyStruct");
if (obj.id != null) {
output.writeFieldBegin("id", thrift.TType.I32, 1);
Expand Down
Loading

0 comments on commit 971cb19

Please sign in to comment.