Skip to content

Commit

Permalink
Include method parameters map (#158)
Browse files Browse the repository at this point in the history
* add method parameters

Keep track of the number of arguments of services

* update test fixtures
  • Loading branch information
angelo-yu-ck authored and kevin-greene-ck committed May 24, 2019
1 parent 5ca9aaf commit 187aec6
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/render/shared/identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const COMMON_IDENTIFIERS = {
thrift: ts.createIdentifier('thrift'),
methodNames: ts.createIdentifier('methodNames'),
_methodNames: ts.createIdentifier('_methodNames'),
methodParameters: ts.createIdentifier('methodParameters'),
_methodParameters: ts.createIdentifier('_methodParameters'),
serviceName: ts.createIdentifier('serviceName'),
fieldAnnotations: ts.createIdentifier('fieldAnnotations'),
methodAnnotations: ts.createIdentifier('methodAnnotations'),
Expand Down
6 changes: 6 additions & 0 deletions src/main/render/thrift-server/service/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
createStructResultName,
renderMethodNamesProperty,
renderMethodNamesStaticProperty,
renderMethodParametersProperty,
renderMethodParametersStaticProperty,
renderServiceNameProperty,
renderServiceNameStaticProperty,
} from './utils'
Expand Down Expand Up @@ -84,11 +86,13 @@ export function renderClient(
const staticAnnotations: ts.PropertyDeclaration = renderServiceAnnotationsStaticProperty()
const staticMethodAnnotations: ts.PropertyDeclaration = renderMethodAnnotationsStaticProperty()
const staticMethodNames: ts.PropertyDeclaration = renderMethodNamesStaticProperty()
const staticMethodParameters: ts.PropertyDeclaration = renderMethodParametersStaticProperty()

const serviceName: ts.PropertyDeclaration = renderServiceNameProperty()
const annotations: ts.PropertyDeclaration = renderServiceAnnotationsProperty()
const methodAnnotations: ts.PropertyDeclaration = renderMethodAnnotationsProperty()
const methodNames: ts.PropertyDeclaration = renderMethodNamesProperty()
const methodParameters: ts.PropertyDeclaration = renderMethodParametersProperty()

const baseMethods: Array<ts.MethodDeclaration> = service.functions.map(
(func: FunctionDefinition) => {
Expand Down Expand Up @@ -119,10 +123,12 @@ export function renderClient(
staticAnnotations,
staticMethodAnnotations,
staticMethodNames,
staticMethodParameters,
serviceName,
annotations,
methodAnnotations,
methodNames,
methodParameters,
...createCtor(service),
...baseMethods,
], // body
Expand Down
2 changes: 2 additions & 0 deletions src/main/render/thrift-server/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
createStructArgsName,
createStructResultName,
renderMethodNames,
renderMethodParameters,
renderServiceName,
} from './utils'

Expand Down Expand Up @@ -51,6 +52,7 @@ export function renderService(
renderServiceAnnotations(collectAllAnnotations(service, state)),
renderMethodAnnotations(collectAllMethods(service, state)),
renderMethodNames(service, state),
renderMethodParameters(service, state),
...renderArgsStruct(service, state),
...renderResultStruct(service, state),
renderClient(service, state),
Expand Down
71 changes: 71 additions & 0 deletions src/main/render/thrift-server/service/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,77 @@ export function renderMethodNamesStaticProperty(): ts.PropertyDeclaration {
)
}

export function renderMethodParameters(
service: ServiceDefinition,
state: IRenderState,
): ts.VariableStatement {
return ts.createVariableStatement(
[ts.createToken(ts.SyntaxKind.ExportKeyword)],
ts.createVariableDeclarationList(
[
ts.createVariableDeclaration(
COMMON_IDENTIFIERS.methodParameters,
ts.createTypeReferenceNode(
'{ [methodName: string]: number }',
undefined,
),
ts.createObjectLiteral(
[
...collectAllMethods(service, state).map(
(next: FunctionDefinition) => {
return ts.createPropertyAssignment(
next.name.value,
ts.createLiteral(
next.fields.length + 1, // including context
),
)
},
),
],
true,
),
),
],
ts.NodeFlags.Const,
),
)
}

export function renderMethodParametersProperty(): ts.PropertyDeclaration {
return ts.createProperty(
undefined,
[
ts.createToken(ts.SyntaxKind.PublicKeyword),
ts.createToken(ts.SyntaxKind.ReadonlyKeyword),
],
COMMON_IDENTIFIERS._methodParameters,
undefined,
ts.createTypeReferenceNode(
'{ [methodName: string]: number }',
undefined,
),
COMMON_IDENTIFIERS.methodParameters,
)
}

export function renderMethodParametersStaticProperty(): ts.PropertyDeclaration {
return ts.createProperty(
undefined,
[
ts.createToken(ts.SyntaxKind.PublicKeyword),
ts.createToken(ts.SyntaxKind.StaticKeyword),
ts.createToken(ts.SyntaxKind.ReadonlyKeyword),
],
COMMON_IDENTIFIERS.methodParameters,
undefined,
ts.createTypeReferenceNode(
'{ [methodName: string]: number }',
undefined,
),
COMMON_IDENTIFIERS.methodParameters,
)
}

function getRawAnnotations(
service: ServiceDefinition,
state: IRenderState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ export const methodAnnotations: thrift.IMethodAnnotations = {
}
};
export const methodNames: Array<string> = ["getUser", "saveUser", "ping"];
export const methodParameters: { [methodName: string]: number } = {
getUser: 2,
saveUser: 2,
ping: 1
};
export interface IGetUser__Args {
__name: "GetUser__Args";
id: number;
Expand Down Expand Up @@ -589,10 +594,12 @@ export class Client<Context = any> extends thrift.ThriftClient<Context> {
public static readonly annotations: thrift.IThriftAnnotations = annotations;
public static readonly methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
public static readonly methodNames: Array<string> = methodNames;
public static readonly methodParameters: { [methodName: string]: number } = methodParameters;
public readonly _serviceName: string = serviceName;
public readonly _annotations: thrift.IThriftAnnotations = annotations;
public readonly _methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
public readonly _methodNames: Array<string> = methodNames;
public readonly _methodParameters: { [methodName: string]: number } = methodParameters;
public getUser(id: number, context?: Context): Promise<IUser> {
const writer: thrift.TTransport = new this.transport();
const output: thrift.TProtocol = new this.protocol(writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ export const methodAnnotations: thrift.IMethodAnnotations = {
}
};
export const methodNames: Array<string> = ["getUser", "saveUser", "ping"];
export const methodParameters: { [methodName: string]: number } = {
getUser: 2,
saveUser: 2,
ping: 1
};
export interface IGetUser__Args {
__name: "GetUser__Args";
id: number;
Expand Down Expand Up @@ -577,10 +582,12 @@ export class Client<Context = any> extends thrift.ThriftClient<Context> {
public static readonly annotations: thrift.IThriftAnnotations = annotations;
public static readonly methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
public static readonly methodNames: Array<string> = methodNames;
public static readonly methodParameters: { [methodName: string]: number } = methodParameters;
public readonly _serviceName: string = serviceName;
public readonly _annotations: thrift.IThriftAnnotations = annotations;
public readonly _methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
public readonly _methodNames: Array<string> = methodNames;
public readonly _methodParameters: { [methodName: string]: number } = methodParameters;
public getUser(id: number, context?: Context): Promise<IUser> {
const writer: thrift.TTransport = new this.transport();
const output: thrift.TProtocol = new this.protocol(writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ export const methodAnnotations: thrift.IMethodAnnotations = {
}
};
export const methodNames: Array<string> = ["getUser", "ping"];
export const methodParameters: { [methodName: string]: number } = {
getUser: 2,
ping: 1
};
export interface IGetUser__Args {
__name: "GetUser__Args";
arg1: MyUnion;
Expand Down Expand Up @@ -461,10 +465,12 @@ export class Client<Context = any> extends thrift.ThriftClient<Context> {
public static readonly annotations: thrift.IThriftAnnotations = annotations;
public static readonly methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
public static readonly methodNames: Array<string> = methodNames;
public static readonly methodParameters: { [methodName: string]: number } = methodParameters;
public readonly _serviceName: string = serviceName;
public readonly _annotations: thrift.IThriftAnnotations = annotations;
public readonly _methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
public readonly _methodNames: Array<string> = methodNames;
public readonly _methodParameters: { [methodName: string]: number } = methodParameters;
public getUser(arg1: MyUnionArgs, context?: Context): Promise<string> {
const writer: thrift.TTransport = new this.transport();
const output: thrift.TProtocol = new this.protocol(writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export const methodAnnotations: thrift.IMethodAnnotations = {
}
};
export const methodNames: Array<string> = ["getStruct", "getUnion", "getEnum"];
export const methodParameters: { [methodName: string]: number } = {
getStruct: 2,
getUnion: 2,
getEnum: 1
};
export interface IGetStruct__Args {
__name: "GetStruct__Args";
key: number;
Expand Down Expand Up @@ -485,10 +490,12 @@ export class Client<Context = any> extends thrift.ThriftClient<Context> {
public static readonly annotations: thrift.IThriftAnnotations = annotations;
public static readonly methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
public static readonly methodNames: Array<string> = methodNames;
public static readonly methodParameters: { [methodName: string]: number } = methodParameters;
public readonly _serviceName: string = serviceName;
public readonly _annotations: thrift.IThriftAnnotations = annotations;
public readonly _methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
public readonly _methodNames: Array<string> = methodNames;
public readonly _methodParameters: { [methodName: string]: number } = methodParameters;
public getStruct(key: number, context?: Context): Promise<__NAMESPACE__.ISharedStruct> {
const writer: thrift.TTransport = new this.transport();
const output: thrift.TProtocol = new this.protocol(writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ export const methodAnnotations: thrift.IMethodAnnotations = {
}
};
export const methodNames: Array<string> = ["getStruct", "getUnion", "getEnum", "ping", "add", "addInt64", "addWithContext", "calculate", "echoBinary", "echoString", "checkName", "checkOptional", "mapOneList", "mapValues", "listToMap", "fetchThing", "zip"];
export const methodParameters: { [methodName: string]: number } = {
getStruct: 2,
getUnion: 2,
getEnum: 1,
ping: 1,
add: 3,
addInt64: 3,
addWithContext: 3,
calculate: 3,
echoBinary: 2,
echoString: 2,
checkName: 2,
checkOptional: 2,
mapOneList: 2,
mapValues: 2,
listToMap: 2,
fetchThing: 1,
zip: 1
};
export interface IPing__Args {
__name: "Ping__Args";
}
Expand Down Expand Up @@ -2552,10 +2571,12 @@ export class Client<Context = any> extends __ROOT_NAMESPACE__.SharedService.Clie
public static readonly annotations: thrift.IThriftAnnotations = annotations;
public static readonly methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
public static readonly methodNames: Array<string> = methodNames;
public static readonly methodParameters: { [methodName: string]: number } = methodParameters;
public readonly _serviceName: string = serviceName;
public readonly _annotations: thrift.IThriftAnnotations = annotations;
public readonly _methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
public readonly _methodNames: Array<string> = methodNames;
public readonly _methodParameters: { [methodName: string]: number } = methodParameters;
constructor(connection: thrift.IThriftConnection<Context>) {
super(connection);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export const methodAnnotations: thrift.IMethodAnnotations = {
}
};
export const methodNames: Array<string> = ["getStruct", "getUnion", "getEnum"];
export const methodParameters: { [methodName: string]: number } = {
getStruct: 2,
getUnion: 2,
getEnum: 1
};
export interface IGetStruct__Args {
__name: "GetStruct__Args";
key: number;
Expand Down Expand Up @@ -485,10 +490,12 @@ export class Client<Context = any> extends thrift.ThriftClient<Context> {
public static readonly annotations: thrift.IThriftAnnotations = annotations;
public static readonly methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
public static readonly methodNames: Array<string> = methodNames;
public static readonly methodParameters: { [methodName: string]: number } = methodParameters;
public readonly _serviceName: string = serviceName;
public readonly _annotations: thrift.IThriftAnnotations = annotations;
public readonly _methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
public readonly _methodNames: Array<string> = methodNames;
public readonly _methodParameters: { [methodName: string]: number } = methodParameters;
public getStruct(key: number, context?: Context): Promise<__NAMESPACE__.ISharedStruct> {
const writer: thrift.TTransport = new this.transport();
const output: thrift.TProtocol = new this.protocol(writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ export const methodAnnotations: thrift.IMethodAnnotations = {
}
};
export const methodNames: Array<string> = ["getStruct", "getUnion", "getEnum", "ping", "add", "addInt64", "addWithContext", "calculate", "echoBinary", "echoString", "checkName", "checkOptional", "mapOneList", "mapValues", "listToMap", "fetchThing", "zip"];
export const methodParameters: { [methodName: string]: number } = {
getStruct: 2,
getUnion: 2,
getEnum: 1,
ping: 1,
add: 3,
addInt64: 3,
addWithContext: 3,
calculate: 3,
echoBinary: 2,
echoString: 2,
checkName: 2,
checkOptional: 2,
mapOneList: 2,
mapValues: 2,
listToMap: 2,
fetchThing: 1,
zip: 1
};
export interface IPing__Args {
__name: "Ping__Args";
}
Expand Down Expand Up @@ -2552,10 +2571,12 @@ export class Client<Context = any> extends __ROOT_NAMESPACE__.SharedService.Clie
public static readonly annotations: thrift.IThriftAnnotations = annotations;
public static readonly methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
public static readonly methodNames: Array<string> = methodNames;
public static readonly methodParameters: { [methodName: string]: number } = methodParameters;
public readonly _serviceName: string = serviceName;
public readonly _annotations: thrift.IThriftAnnotations = annotations;
public readonly _methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
public readonly _methodNames: Array<string> = methodNames;
public readonly _methodParameters: { [methodName: string]: number } = methodParameters;
constructor(connection: thrift.IThriftConnection<Context>) {
super(connection);
}
Expand Down
6 changes: 6 additions & 0 deletions src/tests/unit/fixtures/thrift-server/i64_service.solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ export const methodAnnotations: thrift.IMethodAnnotations = {
}
};
export const methodNames: Array<string> = ["peg", "pong"];
export const methodParameters: { [methodName: string]: number } = {
peg: 2,
pong: 2
};
export interface IPeg__Args {
__name: "Peg__Args";
name: string;
Expand Down Expand Up @@ -412,10 +416,12 @@ export class Client<Context = any> extends thrift.ThriftClient<Context> {
public static readonly annotations: thrift.IThriftAnnotations = annotations;
public static readonly methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
public static readonly methodNames: Array<string> = methodNames;
public static readonly methodParameters: { [methodName: string]: number } = methodParameters;
public readonly _serviceName: string = serviceName;
public readonly _annotations: thrift.IThriftAnnotations = annotations;
public readonly _methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
public readonly _methodNames: Array<string> = methodNames;
public readonly _methodParameters: { [methodName: string]: number } = methodParameters;
public peg(name: string, context?: Context): Promise<string> {
const writer: thrift.TTransport = new this.transport();
const output: thrift.TProtocol = new this.protocol(writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export const methodAnnotations: thrift.IMethodAnnotations = {
}
};
export const methodNames: Array<string> = ["ping"];
export const methodParameters: { [methodName: string]: number } = {
ping: 2
};
export interface IPing__Args {
__name: "Ping__Args";
id: thrift.Int64;
Expand Down Expand Up @@ -169,10 +172,12 @@ export class Client<Context = any> extends thrift.ThriftClient<Context> {
public static readonly annotations: thrift.IThriftAnnotations = annotations;
public static readonly methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
public static readonly methodNames: Array<string> = methodNames;
public static readonly methodParameters: { [methodName: string]: number } = methodParameters;
public readonly _serviceName: string = serviceName;
public readonly _annotations: thrift.IThriftAnnotations = annotations;
public readonly _methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
public readonly _methodNames: Array<string> = methodNames;
public readonly _methodParameters: { [methodName: string]: number } = methodParameters;
public ping(id: number | string | thrift.Int64, context?: Context): Promise<void> {
const writer: thrift.TTransport = new this.transport();
const output: thrift.TProtocol = new this.protocol(writer);
Expand Down
Loading

0 comments on commit 187aec6

Please sign in to comment.