Skip to content

Commit 1f74c7d

Browse files
feat: include method parameters map (#159)
* 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
1 parent 5ca9aaf commit 1f74c7d

File tree

16 files changed

+220
-17
lines changed

16 files changed

+220
-17
lines changed

package-lock.json

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/render/shared/identifiers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export const COMMON_IDENTIFIERS = {
1010
thrift: ts.createIdentifier('thrift'),
1111
methodNames: ts.createIdentifier('methodNames'),
1212
_methodNames: ts.createIdentifier('_methodNames'),
13+
methodParameters: ts.createIdentifier('methodParameters'),
14+
_methodParameters: ts.createIdentifier('_methodParameters'),
1315
serviceName: ts.createIdentifier('serviceName'),
1416
fieldAnnotations: ts.createIdentifier('fieldAnnotations'),
1517
methodAnnotations: ts.createIdentifier('methodAnnotations'),

src/main/render/thrift-server/service/client.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
createStructResultName,
1616
renderMethodNamesProperty,
1717
renderMethodNamesStaticProperty,
18+
renderMethodParametersProperty,
1819
renderServiceNameProperty,
1920
renderServiceNameStaticProperty,
2021
} from './utils'
@@ -89,6 +90,7 @@ export function renderClient(
8990
const annotations: ts.PropertyDeclaration = renderServiceAnnotationsProperty()
9091
const methodAnnotations: ts.PropertyDeclaration = renderMethodAnnotationsProperty()
9192
const methodNames: ts.PropertyDeclaration = renderMethodNamesProperty()
93+
const methodParameters: ts.PropertyDeclaration = renderMethodParametersProperty()
9294

9395
const baseMethods: Array<ts.MethodDeclaration> = service.functions.map(
9496
(func: FunctionDefinition) => {
@@ -123,6 +125,7 @@ export function renderClient(
123125
annotations,
124126
methodAnnotations,
125127
methodNames,
128+
methodParameters,
126129
...createCtor(service),
127130
...baseMethods,
128131
], // body

src/main/render/thrift-server/service/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
createStructArgsName,
1616
createStructResultName,
1717
renderMethodNames,
18+
renderMethodParameters,
1819
renderServiceName,
1920
} from './utils'
2021

@@ -51,6 +52,7 @@ export function renderService(
5152
renderServiceAnnotations(collectAllAnnotations(service, state)),
5253
renderMethodAnnotations(collectAllMethods(service, state)),
5354
renderMethodNames(service, state),
55+
renderMethodParameters(service, state),
5456
...renderArgsStruct(service, state),
5557
...renderResultStruct(service, state),
5658
renderClient(service, state),

src/main/render/thrift-server/service/utils.ts

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { DefinitionType, IRenderState } from '../../../types'
1414
import { COMMON_IDENTIFIERS } from '../identifiers'
1515

1616
import { resolveIdentifierDefinition } from '../../../resolver'
17-
import { createStringType } from '../../shared/types'
17+
import { createNumberType, createStringType } from '../../shared/types'
1818

1919
export function capitalize(str: string): string {
2020
return str.charAt(0).toUpperCase() + str.slice(1)
@@ -165,6 +165,71 @@ export function renderMethodNamesStaticProperty(): ts.PropertyDeclaration {
165165
)
166166
}
167167

168+
const methodParamMapType: ts.TypeLiteralNode = ts.createTypeLiteralNode([
169+
ts.createIndexSignature(
170+
undefined,
171+
undefined,
172+
[
173+
ts.createParameter(
174+
undefined,
175+
undefined,
176+
undefined,
177+
COMMON_IDENTIFIERS.methodName,
178+
undefined,
179+
createStringType(),
180+
),
181+
],
182+
createNumberType(),
183+
),
184+
])
185+
186+
export function renderMethodParameters(
187+
service: ServiceDefinition,
188+
state: IRenderState,
189+
): ts.VariableStatement {
190+
return ts.createVariableStatement(
191+
[ts.createToken(ts.SyntaxKind.ExportKeyword)],
192+
ts.createVariableDeclarationList(
193+
[
194+
ts.createVariableDeclaration(
195+
COMMON_IDENTIFIERS.methodParameters,
196+
methodParamMapType,
197+
ts.createObjectLiteral(
198+
[
199+
...collectAllMethods(service, state).map(
200+
(next: FunctionDefinition) => {
201+
return ts.createPropertyAssignment(
202+
next.name.value,
203+
ts.createLiteral(
204+
next.fields.length + 1, // including context
205+
),
206+
)
207+
},
208+
),
209+
],
210+
true,
211+
),
212+
),
213+
],
214+
ts.NodeFlags.Const,
215+
),
216+
)
217+
}
218+
219+
export function renderMethodParametersProperty(): ts.PropertyDeclaration {
220+
return ts.createProperty(
221+
undefined,
222+
[
223+
ts.createToken(ts.SyntaxKind.PublicKeyword),
224+
ts.createToken(ts.SyntaxKind.ReadonlyKeyword),
225+
],
226+
COMMON_IDENTIFIERS._methodParameters,
227+
undefined,
228+
methodParamMapType,
229+
COMMON_IDENTIFIERS.methodParameters,
230+
)
231+
}
232+
168233
function getRawAnnotations(
169234
service: ServiceDefinition,
170235
state: IRenderState,

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ export const methodAnnotations: thrift.IMethodAnnotations = {
144144
}
145145
};
146146
export const methodNames: Array<string> = ["getUser", "saveUser", "ping"];
147+
export const methodParameters: {
148+
[methodName: string]: number;
149+
} = {
150+
getUser: 2,
151+
saveUser: 2,
152+
ping: 1
153+
};
147154
export interface IGetUser__Args {
148155
__name: "GetUser__Args";
149156
id: number;
@@ -593,6 +600,9 @@ export class Client<Context = any> extends thrift.ThriftClient<Context> {
593600
public readonly _annotations: thrift.IThriftAnnotations = annotations;
594601
public readonly _methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
595602
public readonly _methodNames: Array<string> = methodNames;
603+
public readonly _methodParameters: {
604+
[methodName: string]: number;
605+
} = methodParameters;
596606
public getUser(id: number, context?: Context): Promise<IUser> {
597607
const writer: thrift.TTransport = new this.transport();
598608
const output: thrift.TProtocol = new this.protocol(writer);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ export const methodAnnotations: thrift.IMethodAnnotations = {
132132
}
133133
};
134134
export const methodNames: Array<string> = ["getUser", "saveUser", "ping"];
135+
export const methodParameters: {
136+
[methodName: string]: number;
137+
} = {
138+
getUser: 2,
139+
saveUser: 2,
140+
ping: 1
141+
};
135142
export interface IGetUser__Args {
136143
__name: "GetUser__Args";
137144
id: number;
@@ -581,6 +588,9 @@ export class Client<Context = any> extends thrift.ThriftClient<Context> {
581588
public readonly _annotations: thrift.IThriftAnnotations = annotations;
582589
public readonly _methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
583590
public readonly _methodNames: Array<string> = methodNames;
591+
public readonly _methodParameters: {
592+
[methodName: string]: number;
593+
} = methodParameters;
584594
public getUser(id: number, context?: Context): Promise<IUser> {
585595
const writer: thrift.TTransport = new this.transport();
586596
const output: thrift.TProtocol = new this.protocol(writer);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ export const methodAnnotations: thrift.IMethodAnnotations = {
172172
}
173173
};
174174
export const methodNames: Array<string> = ["getUser", "ping"];
175+
export const methodParameters: {
176+
[methodName: string]: number;
177+
} = {
178+
getUser: 2,
179+
ping: 1
180+
};
175181
export interface IGetUser__Args {
176182
__name: "GetUser__Args";
177183
arg1: MyUnion;
@@ -465,6 +471,9 @@ export class Client<Context = any> extends thrift.ThriftClient<Context> {
465471
public readonly _annotations: thrift.IThriftAnnotations = annotations;
466472
public readonly _methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
467473
public readonly _methodNames: Array<string> = methodNames;
474+
public readonly _methodParameters: {
475+
[methodName: string]: number;
476+
} = methodParameters;
468477
public getUser(arg1: MyUnionArgs, context?: Context): Promise<string> {
469478
const writer: thrift.TTransport = new this.transport();
470479
const output: thrift.TProtocol = new this.protocol(writer);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ export const methodAnnotations: thrift.IMethodAnnotations = {
2222
}
2323
};
2424
export const methodNames: Array<string> = ["getStruct", "getUnion", "getEnum"];
25+
export const methodParameters: {
26+
[methodName: string]: number;
27+
} = {
28+
getStruct: 2,
29+
getUnion: 2,
30+
getEnum: 1
31+
};
2532
export interface IGetStruct__Args {
2633
__name: "GetStruct__Args";
2734
key: number;
@@ -489,6 +496,9 @@ export class Client<Context = any> extends thrift.ThriftClient<Context> {
489496
public readonly _annotations: thrift.IThriftAnnotations = annotations;
490497
public readonly _methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
491498
public readonly _methodNames: Array<string> = methodNames;
499+
public readonly _methodParameters: {
500+
[methodName: string]: number;
501+
} = methodParameters;
492502
public getStruct(key: number, context?: Context): Promise<__NAMESPACE__.ISharedStruct> {
493503
const writer: thrift.TTransport = new this.transport();
494504
const output: thrift.TProtocol = new this.protocol(writer);

src/tests/unit/fixtures/thrift-server/generated-strict/com/test/calculator/Calculator.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,27 @@ export const methodAnnotations: thrift.IMethodAnnotations = {
8181
}
8282
};
8383
export const methodNames: Array<string> = ["getStruct", "getUnion", "getEnum", "ping", "add", "addInt64", "addWithContext", "calculate", "echoBinary", "echoString", "checkName", "checkOptional", "mapOneList", "mapValues", "listToMap", "fetchThing", "zip"];
84+
export const methodParameters: {
85+
[methodName: string]: number;
86+
} = {
87+
getStruct: 2,
88+
getUnion: 2,
89+
getEnum: 1,
90+
ping: 1,
91+
add: 3,
92+
addInt64: 3,
93+
addWithContext: 3,
94+
calculate: 3,
95+
echoBinary: 2,
96+
echoString: 2,
97+
checkName: 2,
98+
checkOptional: 2,
99+
mapOneList: 2,
100+
mapValues: 2,
101+
listToMap: 2,
102+
fetchThing: 1,
103+
zip: 1
104+
};
84105
export interface IPing__Args {
85106
__name: "Ping__Args";
86107
}
@@ -2556,6 +2577,9 @@ export class Client<Context = any> extends __ROOT_NAMESPACE__.SharedService.Clie
25562577
public readonly _annotations: thrift.IThriftAnnotations = annotations;
25572578
public readonly _methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
25582579
public readonly _methodNames: Array<string> = methodNames;
2580+
public readonly _methodParameters: {
2581+
[methodName: string]: number;
2582+
} = methodParameters;
25592583
constructor(connection: thrift.IThriftConnection<Context>) {
25602584
super(connection);
25612585
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ export const methodAnnotations: thrift.IMethodAnnotations = {
2222
}
2323
};
2424
export const methodNames: Array<string> = ["getStruct", "getUnion", "getEnum"];
25+
export const methodParameters: {
26+
[methodName: string]: number;
27+
} = {
28+
getStruct: 2,
29+
getUnion: 2,
30+
getEnum: 1
31+
};
2532
export interface IGetStruct__Args {
2633
__name: "GetStruct__Args";
2734
key: number;
@@ -489,6 +496,9 @@ export class Client<Context = any> extends thrift.ThriftClient<Context> {
489496
public readonly _annotations: thrift.IThriftAnnotations = annotations;
490497
public readonly _methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
491498
public readonly _methodNames: Array<string> = methodNames;
499+
public readonly _methodParameters: {
500+
[methodName: string]: number;
501+
} = methodParameters;
492502
public getStruct(key: number, context?: Context): Promise<__NAMESPACE__.ISharedStruct> {
493503
const writer: thrift.TTransport = new this.transport();
494504
const output: thrift.TProtocol = new this.protocol(writer);

src/tests/unit/fixtures/thrift-server/generated/com/test/calculator/Calculator.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,27 @@ export const methodAnnotations: thrift.IMethodAnnotations = {
8181
}
8282
};
8383
export const methodNames: Array<string> = ["getStruct", "getUnion", "getEnum", "ping", "add", "addInt64", "addWithContext", "calculate", "echoBinary", "echoString", "checkName", "checkOptional", "mapOneList", "mapValues", "listToMap", "fetchThing", "zip"];
84+
export const methodParameters: {
85+
[methodName: string]: number;
86+
} = {
87+
getStruct: 2,
88+
getUnion: 2,
89+
getEnum: 1,
90+
ping: 1,
91+
add: 3,
92+
addInt64: 3,
93+
addWithContext: 3,
94+
calculate: 3,
95+
echoBinary: 2,
96+
echoString: 2,
97+
checkName: 2,
98+
checkOptional: 2,
99+
mapOneList: 2,
100+
mapValues: 2,
101+
listToMap: 2,
102+
fetchThing: 1,
103+
zip: 1
104+
};
84105
export interface IPing__Args {
85106
__name: "Ping__Args";
86107
}
@@ -2556,6 +2577,9 @@ export class Client<Context = any> extends __ROOT_NAMESPACE__.SharedService.Clie
25562577
public readonly _annotations: thrift.IThriftAnnotations = annotations;
25572578
public readonly _methodAnnotations: thrift.IMethodAnnotations = methodAnnotations;
25582579
public readonly _methodNames: Array<string> = methodNames;
2580+
public readonly _methodParameters: {
2581+
[methodName: string]: number;
2582+
} = methodParameters;
25592583
constructor(connection: thrift.IThriftConnection<Context>) {
25602584
super(connection);
25612585
}

0 commit comments

Comments
 (0)