Skip to content

Commit

Permalink
feat: include method parameters map (#159)
Browse files Browse the repository at this point in the history
* Include method parameters map (#158)

* add method parameters

Keep track of the number of arguments of services

* update test fixtures

* fix: only need methodParams on instances
  • Loading branch information
kevin-greene-ck authored May 24, 2019
1 parent 5ca9aaf commit 1f74c7d
Show file tree
Hide file tree
Showing 16 changed files with 220 additions and 17 deletions.
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
3 changes: 3 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,7 @@ import {
createStructResultName,
renderMethodNamesProperty,
renderMethodNamesStaticProperty,
renderMethodParametersProperty,
renderServiceNameProperty,
renderServiceNameStaticProperty,
} from './utils'
Expand Down Expand Up @@ -89,6 +90,7 @@ export function renderClient(
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 @@ -123,6 +125,7 @@ export function renderClient(
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
67 changes: 66 additions & 1 deletion src/main/render/thrift-server/service/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { DefinitionType, IRenderState } from '../../../types'
import { COMMON_IDENTIFIERS } from '../identifiers'

import { resolveIdentifierDefinition } from '../../../resolver'
import { createStringType } from '../../shared/types'
import { createNumberType, createStringType } from '../../shared/types'

export function capitalize(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1)
Expand Down Expand Up @@ -165,6 +165,71 @@ export function renderMethodNamesStaticProperty(): ts.PropertyDeclaration {
)
}

const methodParamMapType: ts.TypeLiteralNode = ts.createTypeLiteralNode([
ts.createIndexSignature(
undefined,
undefined,
[
ts.createParameter(
undefined,
undefined,
undefined,
COMMON_IDENTIFIERS.methodName,
undefined,
createStringType(),
),
],
createNumberType(),
),
])

export function renderMethodParameters(
service: ServiceDefinition,
state: IRenderState,
): ts.VariableStatement {
return ts.createVariableStatement(
[ts.createToken(ts.SyntaxKind.ExportKeyword)],
ts.createVariableDeclarationList(
[
ts.createVariableDeclaration(
COMMON_IDENTIFIERS.methodParameters,
methodParamMapType,
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,
methodParamMapType,
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,13 @@ 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 @@ -593,6 +600,9 @@ export class Client<Context = any> extends thrift.ThriftClient<Context> {
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
10 changes: 10 additions & 0 deletions src/tests/unit/fixtures/thrift-server/basic_service.solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ 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 @@ -581,6 +588,9 @@ export class Client<Context = any> extends thrift.ThriftClient<Context> {
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,12 @@ 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 @@ -465,6 +471,9 @@ export class Client<Context = any> extends thrift.ThriftClient<Context> {
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,13 @@ 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 @@ -489,6 +496,9 @@ export class Client<Context = any> extends thrift.ThriftClient<Context> {
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,27 @@ 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 @@ -2556,6 +2577,9 @@ export class Client<Context = any> extends __ROOT_NAMESPACE__.SharedService.Clie
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
10 changes: 10 additions & 0 deletions src/tests/unit/fixtures/thrift-server/generated/SharedService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ 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 @@ -489,6 +496,9 @@ export class Client<Context = any> extends thrift.ThriftClient<Context> {
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,27 @@ 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 @@ -2556,6 +2577,9 @@ export class Client<Context = any> extends __ROOT_NAMESPACE__.SharedService.Clie
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
Loading

0 comments on commit 1f74c7d

Please sign in to comment.