-
Notifications
You must be signed in to change notification settings - Fork 242
/
emit-arguments.ts
39 lines (36 loc) · 1.33 KB
/
emit-arguments.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { CodeMaker } from 'codemaker';
import { GoParameter } from '../types';
import { slugify } from './util';
/**
* Packages arguments such that they can be sent correctly to the jsii runtime
* library.
*
* @returns the expression to use in place of the arguments for the jsii
* runtime library call.
*/
export function emitArguments(
code: CodeMaker,
parameters: readonly GoParameter[],
returnVarName: string,
): string | undefined {
const argsList = parameters.map((param) => param.name);
if (argsList.length === 0) {
return undefined;
}
if (parameters[parameters.length - 1].isVariadic) {
// For variadic methods, we must build up the []interface{} slice by hand,
// as there would not be any implicit conversion happening when passing
// the variadic argument as a splat to the append function...
const head = argsList.slice(0, argsList.length - 1);
const tail = argsList[argsList.length - 1];
const variable = slugify('args', [...argsList, returnVarName]);
const elt = slugify('a', [variable]);
code.line(`${variable} := []interface{}{${head.join(', ')}}`);
code.openBlock(`for _, ${elt} := range ${tail}`);
code.line(`${variable} = append(${variable}, ${elt})`);
code.closeBlock();
code.line();
return variable;
}
return `[]interface{}{${argsList.join(', ')}}`;
}