-
Notifications
You must be signed in to change notification settings - Fork 243
/
type-name.ts
503 lines (434 loc) · 15 KB
/
type-name.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
import {
Assembly,
OptionalValue,
TypeReference,
isCollectionTypeReference,
CollectionKind,
isNamedTypeReference,
isPrimitiveTypeReference,
PrimitiveType,
PrimitiveTypeReference,
isUnionTypeReference,
Type,
isInterfaceType,
} from '@jsii/spec';
import { toSnakeCase } from 'codemaker';
import { createHash } from 'crypto';
import { die, toPythonIdentifier } from './util';
export interface TypeName {
pythonType(context: NamingContext): string;
requiredImports(context: NamingContext): PythonImports;
}
export interface PythonImports {
/**
* For a given source module, what elements to import. The empty string value
* indicates a need to import the module fully ("import <name>") instead of
* doing a piecemeal import ("from <name> import <item>").
*/
readonly [sourcePackage: string]: ReadonlySet<string>;
}
/**
* The context in which a PythonType is being considered.
*/
export interface NamingContext {
/** The assembly in which the PythonType is expressed. */
readonly assembly: Assembly;
/** A resolver to obtain complete information about a type. */
readonly typeResolver: (fqn: string) => Type;
/** The submodule of the assembly in which the PythonType is expressed (could be the module root) */
readonly submodule: string;
/**
* The declaration is made in the context of a type annotation (so it can be quoted)
*
* @default true
*/
readonly typeAnnotation?: boolean;
/**
* A an array representing the stack of declarations currently being
* initialized. All of these names can only be referred to using a forward
* reference (stringified type name) in the context of type signatures (but
* they can be used safely from implementations so long as those are not *run*
* as part of the declaration).
*
* @default []
*/
readonly surroundingTypeFqns?: readonly string[];
/**
* Disables generating typing.Optional wrappers
* @default false
* @internal
*/
readonly ignoreOptional?: boolean;
/**
* The set of jsii type FQNs that have already been emitted so far. This is
* used to determine whether a given type reference is a forward declaration
* or not when emitting type signatures.
*/
readonly emittedTypes: Set<string>;
/**
* Whether the type is emitted for a parameter or not. This may change the
* exact type signature being emitted (e.g: Arrays are typing.Sequence[T] for
* parameters, and typing.List[T] otherwise).
*/
readonly parameterType?: boolean;
}
export function toTypeName(ref?: OptionalValue | TypeReference): TypeName {
if (ref == null) {
return Primitive.NONE;
}
const type = isOptionalValue(ref) ? ref.type : ref;
const optional = isOptionalValue(ref) && ref.optional;
let result: TypeName = Primitive.ANY;
if (isPrimitiveTypeReference(type)) {
result = Primitive.of(type);
} else if (isCollectionTypeReference(type)) {
const elt = toTypeName(type.collection.elementtype);
if (type.collection.kind === CollectionKind.Array) {
result = new List(elt);
} else {
result = new Dict(elt);
}
} else if (isUnionTypeReference(type)) {
result = new Union(type.union.types.map(toTypeName));
} else if (isNamedTypeReference(type)) {
result = new UserType(type.fqn);
}
return optional ? new Optional(result) : result;
}
/**
* Obtains the Python package name for a given submodule FQN.
*
* @param fqn the submodule FQN for which a package name is needed.
* @param rootAssm the assembly this FQN belongs to.
*/
export function toPackageName(fqn: string, rootAssm: Assembly): string {
return getPackageName(fqn, rootAssm).packageName;
}
export function mergePythonImports(
...pythonImports: readonly PythonImports[]
): PythonImports {
const result: Record<string, Set<string>> = {};
for (const bag of pythonImports) {
for (const [packageName, items] of Object.entries(bag)) {
if (!(packageName in result)) {
result[packageName] = new Set();
}
for (const item of items) {
result[packageName].add(item);
}
}
}
return result;
}
function isOptionalValue(
type: OptionalValue | TypeReference,
): type is OptionalValue {
return (type as unknown as OptionalValue).type != null;
}
class Dict implements TypeName {
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
readonly #element: TypeName;
public constructor(element: TypeName) {
this.#element = element;
}
public pythonType(context: NamingContext) {
return `typing.Mapping[builtins.str, ${this.#element.pythonType(context)}]`;
}
public requiredImports(context: NamingContext) {
return this.#element.requiredImports(context);
}
}
class List implements TypeName {
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
readonly #element: TypeName;
public constructor(element: TypeName) {
this.#element = element;
}
public pythonType(context: NamingContext) {
const type = context.parameterType ? 'Sequence' : 'List';
return `typing.${type}[${this.#element.pythonType(context)}]`;
}
public requiredImports(context: NamingContext) {
return this.#element.requiredImports(context);
}
}
class Optional implements TypeName {
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
readonly #wrapped: TypeName;
public constructor(wrapped: TypeName) {
this.#wrapped = wrapped;
}
public pythonType(context: NamingContext) {
const optionalType = this.#wrapped.pythonType({
...context,
ignoreOptional: true,
});
if (context.ignoreOptional || this.#wrapped === Primitive.ANY) {
return optionalType;
}
return `typing.Optional[${optionalType}]`;
}
public requiredImports(context: NamingContext) {
return this.#wrapped.requiredImports({ ...context, ignoreOptional: true });
}
}
class Primitive implements TypeName {
private static readonly BOOL = new Primitive('builtins.bool');
private static readonly DATE = new Primitive('datetime.datetime');
private static readonly JSII_NUMBER = new Primitive('jsii.Number'); // "jsii" is always already imported!
private static readonly STR = new Primitive('builtins.str');
private static readonly JSON = new Primitive(
'typing.Mapping[typing.Any, typing.Any]',
);
public static readonly ANY = new Primitive('typing.Any');
public static readonly NONE = new Primitive('None');
public static of(type: PrimitiveTypeReference): TypeName {
switch (type.primitive) {
case PrimitiveType.Boolean:
return Primitive.BOOL;
case PrimitiveType.Date:
return Primitive.DATE;
case PrimitiveType.Number:
return Primitive.JSII_NUMBER;
case PrimitiveType.String:
return Primitive.STR;
case PrimitiveType.Json:
return Primitive.JSON;
case PrimitiveType.Any:
default:
return Primitive.ANY;
}
}
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
readonly #pythonType: string;
private constructor(pythonType: string) {
this.#pythonType = pythonType;
}
public pythonType() {
return this.#pythonType;
}
public requiredImports() {
return {};
}
}
class Union implements TypeName {
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
readonly #options: readonly TypeName[];
public constructor(options: readonly TypeName[]) {
this.#options = options;
}
public pythonType(context: NamingContext) {
return `typing.Union[${this.#options
.map((o) => o.pythonType(context))
.join(', ')}]`;
}
public requiredImports(context: NamingContext) {
return mergePythonImports(
...this.#options.map((o) => o.requiredImports(context)),
);
}
}
class UserType implements TypeName {
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
readonly #fqn: string;
public constructor(fqn: string) {
this.#fqn = fqn;
}
public pythonType(context: NamingContext) {
return this.resolve(context).pythonType;
}
public requiredImports(context: NamingContext) {
const requiredImport = this.resolve(context).requiredImport;
if (requiredImport == null) {
return {};
}
return { [requiredImport.sourcePackage]: new Set([requiredImport.item]) };
}
private resolve({
assembly,
emittedTypes,
submodule,
surroundingTypeFqns,
typeAnnotation = true,
parameterType,
typeResolver,
}: NamingContext) {
const { assemblyName, packageName, pythonFqn } = toPythonFqn(
this.#fqn,
assembly,
);
// If this is a type annotation for a parameter, allow dicts to be passed where structs are expected.
const type = typeResolver(this.#fqn);
const isStruct = isInterfaceType(type) && !!type.datatype;
const wrapType =
typeAnnotation && parameterType && isStruct
? (pyType: string) =>
`typing.Union[${pyType}, typing.Dict[str, typing.Any]]`
: (pyType: string) => pyType;
if (assemblyName !== assembly.name) {
return {
// If it's a struct, then we allow passing as a dict, too...
pythonType: wrapType(pythonFqn),
requiredImport: {
sourcePackage: packageName,
item: '',
},
};
}
const submodulePythonName = toPythonFqn(submodule, assembly).pythonFqn;
const typeSubmodulePythonName = toPythonFqn(
findParentSubmodule(assembly.types![this.#fqn], assembly),
assembly,
).pythonFqn;
if (typeSubmodulePythonName === submodulePythonName) {
// Identify declarations that are not yet initialized and hence cannot be
// used as part of a type qualification. Since this is not a forward
// reference, the type was already emitted and its un-qualified name must
// be used instead of its locally qualified name.
const nestingParent = surroundingTypeFqns
?.map((fqn) => toPythonFqn(fqn, assembly).pythonFqn)
?.reverse()
?.find((parent) => pythonFqn.startsWith(`${parent}.`));
if (
typeAnnotation &&
(!emittedTypes.has(this.#fqn) || nestingParent != null)
) {
// Possibly a forward reference, outputting the stringifierd python FQN
return {
pythonType: wrapType(
JSON.stringify(pythonFqn.substring(submodulePythonName.length + 1)),
),
};
}
if (!typeAnnotation && nestingParent) {
// This is not for a type annotation, so we should be at a point in time
// where the surrounding symbol has been defined entirely, so we can
// refer to it "normally" now.
return { pythonType: pythonFqn.slice(packageName.length + 1) };
}
// We'll just make a module-qualified reference at this point.
return {
pythonType: wrapType(
pythonFqn.substring(submodulePythonName.length + 1),
),
};
}
const [toImport, ...nested] = pythonFqn
.substring(typeSubmodulePythonName.length + 1)
.split('.');
const aliasSuffix = createHash('sha256')
.update(typeSubmodulePythonName)
.update('.')
.update(toImport)
.digest('hex')
.substring(0, 8);
const alias = `_${toImport}_${aliasSuffix}`;
return {
pythonType: wrapType([alias, ...nested].join('.')),
requiredImport: {
sourcePackage: relativeImportPath(
submodulePythonName,
typeSubmodulePythonName,
),
item: `${toImport} as ${alias}`,
},
};
}
}
export function toPythonFqn(fqn: string, rootAssm: Assembly) {
const { assemblyName, packageName, tail } = getPackageName(fqn, rootAssm);
const fqnParts: string[] = [packageName];
for (const part of tail) {
fqnParts.push(toPythonIdentifier(part));
}
return { assemblyName, packageName, pythonFqn: fqnParts.join('.') };
}
/**
* Computes the nesting-qualified name of a type.
*
* @param fqn the fully qualified jsii name of the type.
* @param rootAssm the root assembly for the project.
*
* @returns the nesting-qualified python type name (the name of the class,
* qualified with all nesting parent classes).
*/
export function toPythonFullName(fqn: string, rootAssm: Assembly): string {
const { packageName, pythonFqn } = toPythonFqn(fqn, rootAssm);
return pythonFqn.slice(packageName.length + 1);
}
/**
* Computes the python relative import path from `fromModule` to `toModule`.
*
* @param fromPkg the package where the relative import statement is located.
* @param toPkg the package that needs to be relatively imported.
*
* @returns a relative import path.
*
* @example
* relativeImportPath('A.B.C.D', 'A.B.E') === '...E';
* relativeImportPath('A.B.C', 'A.B') === '..';
* relativeImportPath('A.B', 'A.B.C') === '.C';
*/
function relativeImportPath(fromPkg: string, toPkg: string): string {
if (toPkg.startsWith(fromPkg)) {
// from A.B to A.B.C === .C
return `.${toPkg.substring(fromPkg.length + 1)}`;
}
// from A.B.E to A.B.C === .<from A.B to A.B.C>
const fromPkgParent = fromPkg.substring(0, fromPkg.lastIndexOf('.'));
return `.${relativeImportPath(fromPkgParent, toPkg)}`;
}
function getPackageName(fqn: string, rootAssm: Assembly) {
const segments = fqn.split('.');
const assemblyName = segments[0];
const config =
assemblyName === rootAssm.name
? rootAssm
: rootAssm.dependencyClosure?.[assemblyName] ??
die(
`Unable to find configuration for assembly "${assemblyName}" in dependency closure`,
);
const rootPkg =
config.targets?.python?.module ??
die(`No Python target was configured in assembly "${assemblyName}"`);
const pkg = new Array<string>();
const tail = new Array<string>();
for (let len = segments.length; len > 0; len--) {
const submodule = segments.slice(0, len).join('.');
if (submodule === assemblyName) {
pkg.unshift(rootPkg);
break;
}
const submoduleConfig = config.submodules?.[submodule];
if (submoduleConfig == null) {
// Not in a submodule - so the current lead name is not a package name part.
tail.unshift(segments[len - 1]);
continue;
}
const subPackage: string | undefined =
submoduleConfig.targets?.python?.module;
if (subPackage != null) {
// Found a sub-package. Confirm it's nested right in, and make this the head end of our package name.
if (!subPackage.startsWith(`${rootPkg}.`)) {
die(
`Submodule "${submodule}" is mapped to Python sub-package "${subPackage}" which isn't nested under "${rootPkg}"!`,
);
}
pkg.unshift(subPackage);
break;
}
// Just use whatever the default name is for this package name part.
pkg.unshift(toSnakeCase(toPythonIdentifier(segments[len - 1])));
}
return { assemblyName, packageName: pkg.join('.'), tail };
}
function findParentSubmodule(type: Type, assm: Assembly): string {
if (type.namespace == null) {
return assm.name;
}
const namespaceFqn = `${assm.name}.${type.namespace}`;
if (assm.types?.[namespaceFqn] != null) {
return findParentSubmodule(assm.types?.[namespaceFqn], assm);
}
return namespaceFqn;
}