-
Notifications
You must be signed in to change notification settings - Fork 242
/
interface.ts
266 lines (226 loc) · 7.42 KB
/
interface.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import { CodeMaker } from 'codemaker';
import { InterfaceType, Method, Property } from 'jsii-reflect';
import * as comparators from '../comparators';
import { SpecialDependencies } from '../dependencies';
import { EmitContext } from '../emit-context';
import { Package } from '../package';
import { JSII_RT_ALIAS, MethodCall } from '../runtime';
import { getMemberDependencies, getParamDependencies } from '../util';
import { GoType } from './go-type';
import { GoTypeRef } from './go-type-reference';
import { GoMethod, GoProperty } from './type-member';
export class GoInterface extends GoType<InterfaceType> {
public readonly methods: InterfaceMethod[];
public readonly reimplementedMethods?: readonly InterfaceMethod[];
public readonly properties: InterfaceProperty[];
public readonly reimplementedProperties?: readonly InterfaceProperty[];
public constructor(pkg: Package, type: InterfaceType) {
super(pkg, type);
this.methods = type.ownMethods
.map((method) => new InterfaceMethod(this, method))
.sort(comparators.byName);
this.properties = type.ownProperties
.map((prop) => new InterfaceProperty(this, prop))
.sort(comparators.byName);
// If there is more than one base, and any ancestor (including transitive)
// comes from a different assembly, we will re-implement all members on the
// proxy struct, as otherwise we run the risk of un-promotable methods
// caused by inheriting the same interface via multiple paths (since we have
// to represent those as embedded types).
if (
type.interfaces.length > 1 &&
type
.getInterfaces(true)
.some((ancestor) => ancestor.assembly.fqn !== type.assembly.fqn)
) {
this.reimplementedMethods = type.allMethods
.filter((method) => !method.static && method.definingType !== type)
.map((method) => new InterfaceMethod(this, method))
.sort(comparators.byName);
this.reimplementedProperties = type.allProperties
.filter(
(property) => !property.static && property.definingType !== type,
)
.map((property) => new InterfaceProperty(this, property))
.sort(comparators.byName);
}
}
public emit(context: EmitContext) {
this.emitDocs(context);
const { code } = context;
code.openBlock(`type ${this.name} interface`);
// embed extended interfaces
for (const iface of this.extends) {
code.line(
new GoTypeRef(this.pkg.root, iface.type.reference).scopedName(this.pkg),
);
}
for (const method of this.methods) {
method.emitDecl(context);
}
for (const prop of this.properties) {
prop.emit(context);
}
code.closeBlock();
code.line();
code.line(`// The jsii proxy for ${this.name}`);
code.openBlock(`type ${this.proxyName} struct`);
if (this.extends.length === 0) {
// Ensure this is not 0-width
code.line('_ byte // padding');
} else {
for (const base of this.extends) {
code.line(this.pkg.resolveEmbeddedType(base).embed);
}
}
code.closeBlock();
code.line();
for (const method of this.methods) {
method.emit(context);
}
for (const method of this.reimplementedMethods ?? []) {
method.emit(context);
}
for (const prop of this.properties) {
prop.emitGetterProxy(context);
if (!prop.immutable) {
prop.emitSetterProxy(context);
}
}
for (const prop of this.reimplementedProperties ?? []) {
prop.emitGetterProxy(context);
if (!prop.immutable) {
prop.emitSetterProxy(context);
}
}
}
public emitRegistration(code: CodeMaker): void {
code.open(`${JSII_RT_ALIAS}.RegisterInterface(`);
code.line(`"${this.fqn}",`);
code.line(`reflect.TypeOf((*${this.name})(nil)).Elem(),`);
const allMembers = [
...this.type.allMethods
.filter((method) => !method.static)
.map((method) => new InterfaceMethod(this, method)),
...this.type.allProperties
.filter((property) => !property.static)
.map((property) => new GoProperty(this, property)),
].sort(comparators.byName);
if (allMembers.length === 0) {
code.line('nil, // no members');
} else {
code.open(`[]${JSII_RT_ALIAS}.Member{`);
for (const member of allMembers) {
code.line(`${member.override},`);
}
code.close('},');
}
this.emitProxyMakerFunction(code, this.extends);
code.close(')');
}
public get specialDependencies(): SpecialDependencies {
return [
...this.properties.map((p) => p.specialDependencies),
...this.methods.map((m) => m.specialDependencies),
].reduce(
(acc, elt) => ({
runtime: acc.runtime || elt.runtime,
init: acc.init || elt.init,
internal: acc.internal,
time: acc.time || elt.time,
}),
{
runtime: false,
init: false,
internal: this.extends.some((base) => this.pkg.isExternalType(base)),
time: false,
},
);
}
public get extends(): GoInterface[] {
return this.type.interfaces
.map((iface) => this.pkg.root.findType(iface.fqn) as GoInterface)
.sort(comparators.byName);
}
public get extendsDependencies(): Package[] {
const packages: Package[] = [];
for (const ifaceRef of this.extends) {
const pkg = ifaceRef.pkg;
if (pkg) {
packages.push(pkg);
}
}
return packages;
}
public get dependencies(): Package[] {
return [
...this.extendsDependencies,
...getMemberDependencies(this.methods),
...getParamDependencies(this.methods),
...getMemberDependencies(this.properties),
];
}
}
class InterfaceProperty extends GoProperty {
public constructor(
public readonly parent: GoInterface,
public readonly property: Property,
) {
super(parent, property);
}
public get returnType(): string {
return this.reference.scopedReference(this.parent.pkg);
}
public emit({ code, documenter }: EmitContext) {
documenter.emit(this.property.docs);
code.line(`${this.name}() ${this.returnType}`);
if (!this.property.immutable) {
documenter.emit(this.property.docs);
code.line(
`Set${this.name}(${this.name[0].toLowerCase()} ${this.returnType})`,
);
}
}
}
class InterfaceMethod extends GoMethod {
public readonly runtimeCall: MethodCall;
public constructor(
public readonly parent: GoInterface,
public readonly method: Method,
) {
super(parent, method);
this.runtimeCall = new MethodCall(this);
}
public emitDecl(context: EmitContext) {
const docs = this.method.docs;
if (docs) {
context.documenter.emit(docs);
}
const { code } = context;
code.line(`${this.name}(${this.paramString()})${this.returnTypeString}`);
}
public emit({ code }: EmitContext) {
const name = this.name;
code.openBlock(
`func (${this.instanceArg} *${
this.parent.proxyName
}) ${name}(${this.paramString()})${this.returnTypeString}`,
);
this.runtimeCall.emit(code);
code.closeBlock();
code.line();
}
public get specialDependencies(): SpecialDependencies {
return {
runtime: true,
init: false,
internal: false,
time:
this.parameters.some((p) => p.reference.specialDependencies.time) ||
!!this.reference?.specialDependencies.time,
};
}
private get returnTypeString(): string {
return this.reference?.void ? '' : ` ${this.returnType}`;
}
}