fork of hey-api/openapi-ts because I need some additional things
1import type { AnalysisContext, Ref } from '@hey-api/codegen-core';
2import { ref } from '@hey-api/codegen-core';
3
4import { py } from '../../py-compiler';
5import type { MaybePyDsl } from '../base';
6import { PyDsl } from '../base';
7
8export type PyBinaryOperator =
9 | '+'
10 | '-'
11 | '*'
12 | '/'
13 | '//'
14 | '%'
15 | '**'
16 | '=='
17 | '!='
18 | '>'
19 | '>='
20 | '<'
21 | '<='
22 | 'is'
23 | 'is not'
24 | 'in'
25 | 'not in'
26 | 'and'
27 | 'or';
28
29const Mixed = PyDsl<py.BinaryExpression>;
30
31export class BinaryPyDsl extends Mixed {
32 readonly '~dsl' = 'BinaryPyDsl';
33
34 protected _left?: Ref<MaybePyDsl<py.Expression>>;
35 protected _op?: PyBinaryOperator;
36 protected _right?: Ref<MaybePyDsl<py.Expression>>;
37
38 constructor(
39 left: MaybePyDsl<py.Expression>,
40 op: PyBinaryOperator,
41 right: MaybePyDsl<py.Expression>,
42 ) {
43 super();
44 this._left = ref(left);
45 this._op = op;
46 this._right = ref(right);
47 }
48
49 override analyze(ctx: AnalysisContext): void {
50 super.analyze(ctx);
51 ctx.analyze(this._left);
52 ctx.analyze(this._right);
53 }
54
55 /** Returns true when all required builder calls are present. */
56 get isValid(): boolean {
57 return !this.missingRequiredCalls().length;
58 }
59
60 and(right: MaybePyDsl<py.Expression>): this {
61 return this.opAndExpr('and', right);
62 }
63
64 div(right: MaybePyDsl<py.Expression>): this {
65 return this.opAndExpr('/', right);
66 }
67
68 eq(right: MaybePyDsl<py.Expression>): this {
69 return this.opAndExpr('==', right);
70 }
71
72 floordiv(right: MaybePyDsl<py.Expression>): this {
73 return this.opAndExpr('//', right);
74 }
75
76 gt(right: MaybePyDsl<py.Expression>): this {
77 return this.opAndExpr('>', right);
78 }
79
80 gte(right: MaybePyDsl<py.Expression>): this {
81 return this.opAndExpr('>=', right);
82 }
83
84 in_(right: MaybePyDsl<py.Expression>): this {
85 return this.opAndExpr('in', right);
86 }
87
88 is(right: MaybePyDsl<py.Expression>): this {
89 return this.opAndExpr('is', right);
90 }
91
92 isNot(right: MaybePyDsl<py.Expression>): this {
93 return this.opAndExpr('is not', right);
94 }
95
96 lt(right: MaybePyDsl<py.Expression>): this {
97 return this.opAndExpr('<', right);
98 }
99
100 lte(right: MaybePyDsl<py.Expression>): this {
101 return this.opAndExpr('<=', right);
102 }
103
104 minus(right: MaybePyDsl<py.Expression>): this {
105 return this.opAndExpr('-', right);
106 }
107
108 mod(right: MaybePyDsl<py.Expression>): this {
109 return this.opAndExpr('%', right);
110 }
111
112 neq(right: MaybePyDsl<py.Expression>): this {
113 return this.opAndExpr('!=', right);
114 }
115
116 notIn(right: MaybePyDsl<py.Expression>): this {
117 return this.opAndExpr('not in', right);
118 }
119
120 or(right: MaybePyDsl<py.Expression>): this {
121 return this.opAndExpr('or', right);
122 }
123
124 plus(right: MaybePyDsl<py.Expression>): this {
125 return this.opAndExpr('+', right);
126 }
127
128 pow(right: MaybePyDsl<py.Expression>): this {
129 return this.opAndExpr('**', right);
130 }
131
132 times(right: MaybePyDsl<py.Expression>): this {
133 return this.opAndExpr('*', right);
134 }
135
136 override toAst() {
137 this.$validate();
138
139 return py.factory.createBinaryExpression(
140 this.$node(this._left!),
141 this._op!,
142 this.$node(this._right!),
143 );
144 }
145
146 $validate(): asserts this is this & {
147 _left: MaybePyDsl<py.Expression>;
148 _op: PyBinaryOperator;
149 _right: MaybePyDsl<py.Expression>;
150 } {
151 const missing = this.missingRequiredCalls();
152 if (!missing.length) return;
153 throw new Error(`Binary expression missing ${missing.join(' and ')}`);
154 }
155
156 private missingRequiredCalls(): ReadonlyArray<string> {
157 const missing: Array<string> = [];
158 if (!this._left) missing.push('left operand');
159 if (!this._op) missing.push('operator');
160 if (!this._right) missing.push('right operand');
161 return missing;
162 }
163
164 private opAndExpr(op: PyBinaryOperator, right: MaybePyDsl<py.Expression>): this {
165 this._right = ref(right);
166 this._op = op;
167 return this;
168 }
169}