-
Notifications
You must be signed in to change notification settings - Fork 242
/
go-type.ts
75 lines (62 loc) · 2.25 KB
/
go-type.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { CodeMaker } from 'codemaker';
import { Type } from 'jsii-reflect';
import { SpecialDependencies } from '../dependencies';
import { EmitContext } from '../emit-context';
import { Package } from '../package';
import { JSII_RT_ALIAS } from '../runtime';
import { GoClass } from './class';
import { GoInterface } from './interface';
export abstract class GoType<T extends Type = Type> {
public readonly name: string;
public readonly fqn: string;
public readonly proxyName: string;
public constructor(public readonly pkg: Package, public readonly type: T) {
this.name = type.name;
// Prefix with the nesting parent name(s), using an _ delimiter.
for (
let parent = type.nestingParent;
parent != null;
parent = parent.nestingParent
) {
this.name = `${parent.name}_${this.name}`;
}
// Add "jsiiProxy_" prefix to private struct name to avoid keyword conflicts
// such as "default". See https://github.com/aws/jsii/issues/2637
this.proxyName = `jsiiProxy_${this.name}`;
this.fqn = type.fqn;
}
public abstract emit(context: EmitContext): void;
public abstract emitRegistration(code: CodeMaker): void;
public abstract get dependencies(): Package[];
public abstract get specialDependencies(): SpecialDependencies;
public get namespace() {
return this.pkg.packageName;
}
public emitDocs(context: EmitContext): void {
context.documenter.emit(this.type.docs);
}
protected emitStability(context: EmitContext): void {
context.documenter.emitStability(this.type.docs);
}
protected emitProxyMakerFunction(
code: CodeMaker,
bases: ReadonlyArray<GoClass | GoInterface>,
): void {
code.open('func() interface{} {');
if (bases.length > 0) {
const instanceVar = this.proxyName[0];
code.line(`${instanceVar} := ${this.proxyName}{}`);
for (const base of bases) {
const baseEmbed = this.pkg.resolveEmbeddedType(base);
code.line(
`${JSII_RT_ALIAS}.InitJsiiProxy(&${instanceVar}.${baseEmbed.fieldName})`,
);
}
code.line(`return &${instanceVar}`);
} else {
code.line(`return &${this.proxyName}{}`);
}
// This is always used as a function argument, so we add a trailing comma
code.close('},');
}
}