-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgen-python.ts
More file actions
297 lines (269 loc) · 8.48 KB
/
gen-python.ts
File metadata and controls
297 lines (269 loc) · 8.48 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
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
import { type Doc, isComplexType, type Node } from "./parser.ts";
import { match, P } from "npm:ts-pattern";
import { Writer } from "./gen-helpers.ts";
import { assert } from "jsr:@std/assert";
const header = (relativePath: string) =>
`# DO NOT EDIT: This file is auto-generated by ${relativePath}\n` +
"from enum import Enum\n" +
"import msgspec\n\n";
export function extractExportedNames(content: string): string[] {
const names = new Set<string>();
// Match class definitions and union type assignments
const classRegex = /^class\s+([a-zA-Z_][a-zA-Z0-9_]*)/gm;
const unionRegex = /^([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*.+\|.+/gm;
let match;
while ((match = classRegex.exec(content)) !== null) {
names.add(match[1]);
}
while ((match = unionRegex.exec(content)) !== null) {
names.add(match[1]);
}
return [...names].sort();
}
export function generateAll(names: string[]): string {
return `__all__ = ${JSON.stringify(names, null, 4)}\n\n`;
}
// Track generated definitions to avoid duplicates
const generatedDefinitions = new Set<string>();
const generatedDependentClasses = new Set<string>();
export function generatePython(
doc: Doc,
name: string,
relativePath: string,
): string {
// Only include header for the first schema
const shouldIncludeHeader = generatedDefinitions.size === 0;
const content = generateTypes(doc, name);
let output = "";
if (shouldIncludeHeader) {
output += header(relativePath);
}
return output + content;
}
function generateTypes(doc: Doc, name: string) {
const writer = new Writer();
let definitions = "";
const skipAssignments = ["object", "intersection", "enum", "union"];
for (const [defName, definition] of Object.entries(doc.definitions)) {
// Skip if we've already generated this definition
if (generatedDefinitions.has(defName)) {
continue;
}
generatedDefinitions.add(defName);
const definitionWriter = new Writer();
const { w, wn } = definitionWriter.shorthand();
if (!skipAssignments.includes(definition.type)) {
w(defName, " = ");
}
generateNode(definition, definitionWriter);
if (definition.description) {
wn('"""');
wn(`${definition.description}`);
wn('"""');
}
definitions += definitionWriter.output();
}
const { w, wn } = writer.shorthand();
if (!generatedDefinitions.has(name)) {
if (!skipAssignments.includes(doc.root.type)) {
w(name, " = ");
}
generateNode(doc.root, writer);
if (doc.description && doc.root.type !== "object") {
wn('"""');
wn(`${doc.description}`);
wn('"""');
}
}
return definitions + writer.output();
}
function sortByRequired<T extends { required: boolean }>(properties: T[]): T[] {
return [...properties].sort((a, b) => {
if (a.required === b.required) return 0;
return a.required ? -1 : 1;
});
}
function generateNode(node: Node, writer: Writer) {
const { w, wn } = writer.shorthand();
using context = new Context(node);
match(node)
.with({ type: "reference" }, ({ name }) => w(name))
.with({ type: "int" }, () => w("int"))
.with({ type: "float" }, () => w("float"))
.with({ type: "boolean" }, () => w("bool"))
.with({ type: "string" }, () => w("str"))
.with({ type: "literal" }, (node) => w(`Literal["${node.value}"]`))
.with({ type: "record" }, (node) =>
w(`dict[str, ${mapPythonType(node.valueType)}]`),
)
.with({ type: "enum" }, (node) => {
wn(`class ${node.name}(str, Enum):`);
for (const value of node.members) {
wn(` ${value} = "${value}"`);
}
wn("");
})
.with({ type: "union" }, (node) => {
const depWriter = new Writer();
const classes = node.members.map((m) => {
let name: string = "";
if (m.name) {
name = m.name;
} else {
const ident =
m.type === "object"
? (m.properties?.find((p) => p.required)?.key ?? "")
: "";
name = `${node.name}${cap(ident)}`;
}
if (!generatedDependentClasses.has(name)) {
generatedDependentClasses.add(name);
if (isComplexType(m)) {
generateNode(m, depWriter);
}
}
return name;
});
writer.append(depWriter.output());
wn(`${node.name} = ${classes.join(" | ")}`);
})
.with({ type: "object" }, (node) => {
match(context.parent)
.with({ type: "union" }, () => {
const name = context.closestName();
const ident = node.properties.find((p) => p.required)?.key ?? "";
wn(
`class ${name}${cap(
ident,
)}(msgspec.Struct, kw_only=True, omit_defaults=True):`,
);
})
.with(P.nullish, () => {
wn(`class ${node.name}(msgspec.Struct, omit_defaults=True):`);
})
.otherwise(() => {
wn(
`class ${node.name}(msgspec.Struct, kw_only=True, omit_defaults=True):`,
);
});
if (node.description) {
wn(` """`);
wn(` ${node.description}`);
wn(` """`);
}
const sortedProperties = sortByRequired(node.properties);
for (const { key, required, description, value } of sortedProperties) {
w(` ${key}: `);
generateNode(value, writer);
if (!required) w(" | None = None");
wn("");
if (description) {
wn(` """${description}"""`);
}
}
wn("");
})
.with({ type: "descriminated-union" }, (node) => {
const depWriter = new Writer();
const { w: d, wn: dn } = depWriter.shorthand();
const classes: string[] = [];
for (const [name, properties] of Object.entries(node.members)) {
for (const { value } of properties) {
if (isComplexType(value)) {
generateNode(value, depWriter);
}
}
const className = `${cap(name)}${cap(node.name!)}`;
classes.push(className);
if (!generatedDependentClasses.has(className)) {
generatedDependentClasses.add(className);
dn(
`class ${className}(msgspec.Struct, tag_field="${node.descriminator}", tag="${name}"):`,
);
if (properties.length === 0) {
dn(" pass");
}
const sortedProperties = sortByRequired(properties);
for (const {
key,
required,
description,
value,
} of sortedProperties) {
d(` ${key}: `);
!isComplexType(value)
? generateNode(value, depWriter)
: d(value.name ?? value.type);
if (!required) d(" | None = None");
dn("");
if (description) {
dn(` """${description}"""`);
}
}
dn("");
}
}
w(classes.join(" | "));
writer.prepend(depWriter.output());
wn("");
})
.with({ type: "intersection" }, (node) => {
assert(
node.members.length === 2,
"Intersection must have exactly 2 members",
);
assert(
node.members[0]?.type === "object",
"First member of intersection must be an object",
);
for (const member of node.members) {
generateNode(member, writer);
}
})
.with({ type: "unknown" }, () => {
w("Any");
})
.exhaustive();
}
class Context {
private static stack: Node[] = [];
constructor(public readonly currentNode: Node) {
Context.stack.push(this.currentNode);
}
get parent(): Node | undefined {
return Context.stack.at(-2);
}
/**
* When `n` is 1, this returns the parent.
* When `n` is 2, this returns the grandparent.
* etc.
*/
getNthParent(n: number): Node | undefined {
return Context.stack.at(-(n + 1));
}
closestName(): string | undefined {
for (const node of [...Context.stack].reverse()) {
// @ts-expect-error - We're looking for names on roots or declarations, this should be fine.
const name = node.name || node.title;
if (name) {
return name;
}
}
return undefined;
}
[Symbol.dispose]() {
Context.stack = Context.stack.filter((n) => n !== this.currentNode);
}
}
function cap(str: string): string {
if (!str) return "";
return str.charAt(0).toUpperCase() + str.slice(1);
}
function mapPythonType(type: string): string {
return match(type)
.with("string", () => "str")
.with("number", () => "float")
.with("integer", () => "int")
.with("boolean", () => "bool")
.otherwise(() => "Any");
}