fork of hey-api/openapi-ts because I need some additional things
1import type { AnalysisContext } from '@hey-api/codegen-core';
2
3import { py } from '../../ts-python';
4import type { MaybePyDsl } from '../base';
5import { PyDsl } from '../base';
6import { LayoutMixin } from '../mixins/layout';
7
8const Mixed = LayoutMixin(PyDsl<py.DictExpression>);
9
10export class DictPyDsl extends Mixed {
11 readonly '~dsl' = 'DictPyDsl';
12
13 protected _entries: Array<{
14 key: MaybePyDsl<py.Expression>;
15 value: MaybePyDsl<py.Expression>;
16 }> = [];
17
18 constructor(
19 ...entries: Array<{ key: MaybePyDsl<py.Expression>; value: MaybePyDsl<py.Expression> }>
20 ) {
21 super();
22 this._entries = entries;
23 }
24
25 override analyze(ctx: AnalysisContext): void {
26 super.analyze(ctx);
27 for (const entry of this._entries) {
28 ctx.analyze(entry.key);
29 ctx.analyze(entry.value);
30 }
31 }
32
33 entry(key: MaybePyDsl<py.Expression>, value: MaybePyDsl<py.Expression>): this {
34 this._entries.push({ key, value });
35 return this;
36 }
37
38 entries(
39 ...entries: ReadonlyArray<{ key: MaybePyDsl<py.Expression>; value: MaybePyDsl<py.Expression> }>
40 ): this {
41 this._entries.push(...entries);
42 return this;
43 }
44
45 override toAst(): py.DictExpression {
46 const astEntries = this._entries.map((entry) => ({
47 key: this.$node(entry.key) as py.Expression,
48 value: this.$node(entry.value) as py.Expression,
49 }));
50 return py.factory.createDictExpression(astEntries);
51 }
52}