fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at feat/use-query-options 73 lines 2.0 kB view raw
1import type { AnalysisContext, NodeName, Ref } from '@hey-api/codegen-core'; 2import { isSymbol, ref } from '@hey-api/codegen-core'; 3 4import { py } from '../../py-compiler'; 5import { PyDsl } from '../base'; 6import { ValueMixin } from '../mixins/value'; 7import { safeRuntimeName } from '../utils/name'; 8 9const Mixed = ValueMixin(PyDsl<py.Assignment>); 10 11export type VarType = NodeName | PyDsl<py.Expression>; 12 13export class VarPyDsl extends Mixed { 14 readonly '~dsl' = 'VarPyDsl'; 15 override readonly nameSanitizer = safeRuntimeName; 16 17 protected _type?: Ref<VarType>; 18 19 constructor(name?: NodeName) { 20 super(); 21 if (name) this.name.set(name); 22 if (isSymbol(name)) { 23 name.setKind('var'); 24 } 25 } 26 27 override analyze(ctx: AnalysisContext): void { 28 super.analyze(ctx); 29 ctx.analyze(this.name); 30 ctx.analyze(this._type); 31 } 32 33 /** Returns true when all required builder calls are present. */ 34 get isValid(): boolean { 35 return !this.missingRequiredCalls().length; 36 } 37 38 /** Sets the type annotation for the variable. */ 39 type(type: VarType): this { 40 this._type = ref(type); 41 return this; 42 } 43 44 override toAst() { 45 this.$validate(); 46 const target = this.$node(this.name)!; 47 const type = this.$type(); 48 const value = this.$value(); 49 50 return py.factory.createAssignment(target, type, value); 51 } 52 53 $validate(): asserts this { 54 const missing = this.missingRequiredCalls(); 55 if (!missing.length) return; 56 throw new Error(`Variable assignment missing ${missing.join(' and ')}`); 57 } 58 59 protected $type(): py.Expression | undefined { 60 return this.$node(this._type); 61 } 62 63 private missingRequiredCalls(): ReadonlyArray<string> { 64 const missing: Array<string> = []; 65 if (!this.$node(this.name)) missing.push('name'); 66 const hasAnnotation = this.$type(); 67 const hasValue = this.$value(); 68 if (!hasAnnotation && !hasValue) { 69 missing.push('.type() or .assign()'); 70 } 71 return missing; 72 } 73}