Skip to content

Commit 7afc97b

Browse files
authored
[codegen] Improve request parameters generation for reuse of common structs (#40)
1 parent 614f052 commit 7afc97b

2 files changed

Lines changed: 54 additions & 55 deletions

File tree

api/csolution-openapi.yml

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,18 @@ components:
963963
- properties:
964964
result:
965965
$ref: '#/components/schemas/SuccessResult'
966+
LoadSolutionParams:
967+
type: object
968+
properties:
969+
solution:
970+
type: string
971+
description: Path to <solution>.csolution.yml
972+
activeTarget:
973+
type: string
974+
description: Active target set in the format <target-type>[@<set>]
975+
required:
976+
- solution
977+
- activeTarget
966978
LoadSolutionRequest:
967979
allOf:
968980
- $ref: '#/x-jsonrpc-envelope-request-with-params'
@@ -971,17 +983,7 @@ components:
971983
type: string
972984
const: LoadSolution
973985
params:
974-
type: object
975-
properties:
976-
solution:
977-
type: string
978-
description: Path to <solution>.csolution.yml
979-
activeTarget:
980-
type: string
981-
description: Active target set in the format <target-type>[@<set>]
982-
required:
983-
- solution
984-
- activeTarget
986+
$ref: '#/components/schemas/LoadSolutionParams'
985987
LoadSolutionResponse:
986988
allOf:
987989
- $ref: '#/x-jsonrpc-envelope-response'
@@ -1281,6 +1283,15 @@ components:
12811283
- properties:
12821284
result:
12831285
$ref: '#/components/schemas/DraftProjectsInfo'
1286+
ConvertSolutionParams:
1287+
allOf:
1288+
- $ref: '#/components/schemas/LoadSolutionParams'
1289+
- properties:
1290+
updateRte:
1291+
type: boolean
1292+
description: Create/update configuration files
1293+
required:
1294+
- updateRte
12841295
ConvertSolutionRequest:
12851296
allOf:
12861297
- $ref: '#/x-jsonrpc-envelope-request-with-params'
@@ -1289,21 +1300,7 @@ components:
12891300
type: string
12901301
const: ConvertSolution
12911302
params:
1292-
type: object
1293-
properties:
1294-
solution:
1295-
type: string
1296-
description: Path to <solution>.csolution.yml
1297-
activeTarget:
1298-
type: string
1299-
description: Active target set in the format <target-type>[@<set>]
1300-
updateRte:
1301-
type: boolean
1302-
description: Create/update configuration files
1303-
required:
1304-
- solution
1305-
- activeTarget
1306-
- updateRte
1303+
$ref: '#/components/schemas/ConvertSolutionParams'
13071304
ConvertSolutionResult:
13081305
allOf:
13091306
- $ref: '#/components/schemas/SuccessResult'
@@ -1332,17 +1329,7 @@ components:
13321329
type: string
13331330
const: DiscoverLayers
13341331
params:
1335-
type: object
1336-
properties:
1337-
solution:
1338-
type: string
1339-
description: Path to <solution>.csolution.yml
1340-
activeTarget:
1341-
type: string
1342-
description: Active target set in the format <target-type>[@<set>]
1343-
required:
1344-
- solution
1345-
- activeTarget
1332+
$ref: '#/components/schemas/LoadSolutionParams'
13461333
DiscoverLayersResponse:
13471334
allOf:
13481335
- $ref: '#/x-jsonrpc-envelope-response'
@@ -1357,17 +1344,7 @@ components:
13571344
type: string
13581345
const: ListMissingPacks
13591346
params:
1360-
type: object
1361-
properties:
1362-
solution:
1363-
type: string
1364-
description: Path to <solution>.csolution.yml
1365-
activeTarget:
1366-
type: string
1367-
description: Active target set in the format <target-type>[@<set>]
1368-
required:
1369-
- solution
1370-
- activeTarget
1347+
$ref: '#/components/schemas/LoadSolutionParams'
13711348
ListMissingPacksResult:
13721349
allOf:
13731350
- $ref: '#/components/schemas/SuccessResult'

codegen/src/codegen.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,26 @@ using namespace jsonrpccxx;\n`;
271271
}
272272
}
273273

274+
public genCppParams(name: string, cppParams: string[], cppRegParams: string[]) {
275+
const struct = this.structs[name];
276+
if (struct) {
277+
if (struct.extends) {
278+
for (const parent of struct.extends) {
279+
if (parent in this.structs) {
280+
this.genCppParams(parent, cppParams, cppRegParams);
281+
}
282+
}
283+
}
284+
if (struct.members) {
285+
for (const element of struct.members) {
286+
const cppType = element.cppType in this.structs ? `RpcArgs::${element.cppType}` : element.cppType;
287+
cppParams.push(`const ${cppType}& ${element.name}`);
288+
cppRegParams.push(`"${element.name}"`);
289+
}
290+
}
291+
}
292+
}
293+
274294
public collectFunction(name: string, params: any, result: any, description?: string) {
275295
const cppResult = this.getType(name, result, 'Result', 'RpcArgs::').cpp;
276296
let cppFunction = `virtual ${cppResult} ${name}(`;
@@ -282,13 +302,15 @@ using namespace jsonrpccxx;\n`;
282302
const tsFunction = `${this.pascalToCamel(name)}(${tsParamsType ? `args: ${tsParamsType}` : ``}): Promise<${tsResultType}>`;
283303
const tsImplementation = `this.get('${name}'${tsParamsType ? `, args` : ``})`;
284304

285-
if (params && params.properties) {
286-
const cppParams: string[] = [];
287-
const cppRegParams: string[] = [];
288-
for (const [param, item] of Object.entries(params.properties)) {
289-
cppParams.push(`const ${this.getType(name, item, '', 'RpcArgs::').cpp}& ${param}`);
290-
cppRegParams.push(`"${param}"`);
291-
}
305+
let paramsName = this.getTypeName(name, 'Params');
306+
if ((params as any)?.$ref) {
307+
const ref = (params as any).$ref.match(/^#\/components\/schemas\/(.*)/);
308+
paramsName = ref[1];
309+
}
310+
const cppParams: string[] = [];
311+
const cppRegParams: string[] = [];
312+
this.genCppParams(paramsName, cppParams, cppRegParams);
313+
if (cppParams.length > 0) {
292314
cppFunction += cppParams.join(", ");
293315
cppRegistration += `, { ${cppRegParams.join(', ')} }`;
294316
} else {

0 commit comments

Comments
 (0)