-
-
Notifications
You must be signed in to change notification settings - Fork 354
Expand file tree
/
Copy pathexport.ts
More file actions
141 lines (122 loc) · 3.52 KB
/
export.ts
File metadata and controls
141 lines (122 loc) · 3.52 KB
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
import type { Symbol } from '@hey-api/codegen-core';
import { applyNaming, pathToName } from '@hey-api/shared';
import { $ } from '../../../py-dsl';
import type { PydanticPlugin } from '../types';
import { identifiers } from '../v2/constants';
import { createFieldCall } from './field';
import type { ProcessorContext } from './processor';
import type { PydanticField, PydanticFinal } from './types';
export function exportAst({
final,
meta,
naming,
namingAnchor,
path,
plugin,
tags,
}: ProcessorContext & {
final: PydanticFinal;
}): void {
const name = pathToName(path, { anchor: namingAnchor });
const symbol = plugin.symbol(applyNaming(name, naming), {
meta: {
category: 'schema',
path,
tags,
tool: 'pydantic',
...meta,
},
});
if (final.enumMembers) {
exportEnumClass({ final, plugin, symbol });
} else if (final.fields) {
exportClass({ final, plugin, symbol });
} else {
exportTypeAlias({ final, plugin, symbol });
}
}
function exportClass({
final,
plugin,
symbol,
}: {
final: PydanticFinal;
plugin: PydanticPlugin['Instance'];
symbol: Symbol;
}): void {
const baseModel = plugin.external('pydantic.BaseModel');
const classDef = $.class(symbol).extends(baseModel);
if (plugin.config.strict) {
const configDict = plugin.external('pydantic.ConfigDict');
classDef.do(
$.var(identifiers.model_config).assign($(configDict).call($.kwarg('extra', 'forbid'))),
);
}
for (const field of final.fields!) {
const fieldStatement = createFieldStatement(field, plugin);
classDef.do(fieldStatement);
}
plugin.node(classDef);
}
function exportEnumClass({
final,
plugin,
symbol,
}: {
final: PydanticFinal;
plugin: PydanticPlugin['Instance'];
symbol: Symbol;
}): void {
const members = final.enumMembers ?? [];
const hasStrings = members.some((m) => typeof m.value === 'string');
const hasNumbers = members.some((m) => typeof m.value === 'number');
const enumSymbol = plugin.external('enum.Enum');
const classDef = $.class(symbol).extends(enumSymbol);
if (hasStrings && !hasNumbers) {
classDef.extends('str');
} else if (!hasStrings && hasNumbers) {
classDef.extends('int');
}
for (const member of final.enumMembers ?? []) {
classDef.do($.var(member.name).assign($.literal(member.value)));
}
plugin.node(classDef);
}
function createFieldStatement(
field: PydanticField,
plugin: PydanticPlugin['Instance'],
): ReturnType<typeof $.var> {
const fieldSymbol = field.name;
const varStatement = $.var(fieldSymbol).$if(field.typeAnnotation, (v, a) => v.annotate(a));
const originalName = field.originalName ?? fieldSymbol.name;
const needsAlias = field.originalName !== undefined && fieldSymbol.name !== originalName;
const constraints = {
...field.fieldConstraints,
...(needsAlias && !field.fieldConstraints?.alias && { alias: originalName }),
};
if (Object.keys(constraints).length > 0) {
const fieldCall = createFieldCall(constraints, plugin, {
required: !field.isOptional,
});
return varStatement.assign(fieldCall);
}
if (field.isOptional) {
return varStatement.assign('None');
}
return varStatement;
}
function exportTypeAlias({
final,
plugin,
symbol,
}: {
final: PydanticFinal;
plugin: PydanticPlugin['Instance'];
symbol: Symbol;
}): void {
const typeAlias = plugin.external('typing.TypeAlias');
const statement = $.var(symbol)
.annotate(typeAlias)
.assign(final.typeAnnotation ?? plugin.external('typing.Any'));
plugin.node(statement);
}