-
Notifications
You must be signed in to change notification settings - Fork 242
/
struct.ts
62 lines (53 loc) · 1.71 KB
/
struct.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
import * as assert from 'assert';
import { CodeMaker } from 'codemaker';
import { InterfaceType } from 'jsii-reflect';
import { SpecialDependencies } from '../dependencies';
import { EmitContext } from '../emit-context';
import { Package } from '../package';
import { JSII_RT_ALIAS } from '../runtime';
import { getMemberDependencies } from '../util';
import { GoType } from './go-type';
import { GoProperty } from './type-member';
/*
* Struct wraps a JSII datatype interface aka, structs
*/
export class Struct extends GoType<InterfaceType> {
private readonly properties: readonly GoProperty[];
public constructor(parent: Package, type: InterfaceType) {
super(parent, type);
assert(
type.isDataType(),
`The provided interface ${type.fqn} is not a struct!`,
);
this.properties = type.allProperties.map(
(prop) => new GoProperty(this, prop),
);
}
public get dependencies(): Package[] {
return getMemberDependencies(this.properties);
}
public get specialDependencies(): SpecialDependencies {
return {
runtime: false,
init: false,
internal: false,
time: this.properties.some((prop) => prop.specialDependencies.time),
};
}
public emit(context: EmitContext): void {
const { code, documenter } = context;
documenter.emit(this.type.docs);
code.openBlock(`type ${this.name} struct`);
for (const property of this.properties) {
property.emitStructMember(context);
}
code.closeBlock();
code.line();
}
public emitRegistration(code: CodeMaker): void {
code.open(`${JSII_RT_ALIAS}.RegisterStruct(`);
code.line(`"${this.fqn}",`);
code.line(`reflect.TypeOf((*${this.name})(nil)).Elem(),`);
code.close(')');
}
}