@@ -11,7 +11,7 @@ import {
1111import { Writer , type WriterOptions } from "@root/api/writer-generator/writer.ts" ;
1212import { groupByPackages , type TypeSchemaIndex } from "@root/typeschema/utils" ;
1313import type { Field , Identifier , RegularTypeSchema } from "@typeschema/types.ts" ;
14- import { pythonUtils } from "./utils.ts" ;
14+ import { PythonUtils } from "./utils.ts" ;
1515
1616const PRIMITIVE_TYPE_MAP : Record < string , string > = {
1717 boolean : "bool" ,
@@ -37,7 +37,9 @@ const PRIMITIVE_TYPE_MAP: Record<string, string> = {
3737 xhtml : "str" ,
3838} ;
3939
40- const AVAILABLE_STRING_FORMATS : Record < string , ( str : string ) => string > = {
40+ type StringFormatKey = "SnakeCase" | "PascalCase" | "CamelCase" | "CapitalCase" | "KebabCase" ;
41+
42+ const AVAILABLE_STRING_FORMATS : Record < StringFormatKey , ( str : string ) => string > = {
4143 SnakeCase : snakeCase ,
4244 PascalCase : pascalCase ,
4345 CamelCase : camelCase ,
@@ -89,8 +91,8 @@ const MAX_IMPORT_LINE_LENGTH = 100;
8991export interface PythonGeneratorOptions extends WriterOptions {
9092 allowExtraFields ?: boolean ;
9193 staticDir ?: string ;
92- packageName ? : string ;
93- fieldFormat ?: string ;
94+ rootPackageName : string ; /// e.g. <rootPackageName>.hl7_fhir_r4_core.Patient.
95+ fieldFormat : StringFormatKey ;
9496}
9597
9698interface ImportGroup {
@@ -142,21 +144,17 @@ type TypeSchemaPackageGroups = {
142144 groupedComplexTypes : Record < string , RegularTypeSchema [ ] > ;
143145} ;
144146
145- export class Python extends Writer {
146- private readonly allowExtraFields : boolean ;
147- private readonly helper : pythonUtils ;
148- private readonly rootPackage : string ;
147+ export class Python extends Writer < PythonGeneratorOptions > {
149148 private readonly staticDir : string | undefined ;
149+ private readonly helper : PythonUtils ;
150150 private readonly nameFormatFunction : ( name : string ) => string ;
151151
152152 constructor ( options : PythonGeneratorOptions ) {
153153 super ( {
154154 ...options ,
155155 } ) ;
156- this . allowExtraFields = options . allowExtraFields || false ;
157- this . helper = new pythonUtils ( ) ;
156+ this . helper = new PythonUtils ( ) ;
158157 this . staticDir = options . staticDir || undefined ;
159- this . rootPackage = options . packageName || "generated" ;
160158 this . nameFormatFunction = this . getFieldFormatFunction ( options . fieldFormat ) ;
161159 }
162160
@@ -404,7 +402,7 @@ export class Python extends Writer {
404402 }
405403
406404 private generateModelConfig ( ) : void {
407- const extraMode = this . allowExtraFields ? "allow" : "forbid" ;
405+ const extraMode = this . opts . allowExtraFields ? "allow" : "forbid" ;
408406 this . line ( `model_config = ConfigDict(validate_by_name=True, serialize_by_alias=True, extra="${ extraMode } ")` ) ;
409407 }
410408
@@ -583,7 +581,7 @@ export class Python extends Writer {
583581 }
584582
585583 private generateResourceFamilies ( packageResources : RegularTypeSchema [ ] ) : void {
586- const packages = this . helper . getPackages ( packageResources , this . rootPackage ) ;
584+ const packages = this . helper . getPackages ( packageResources , this . opts . rootPackageName ) ;
587585 const families = this . helper . getFamilies ( packageResources ) ;
588586 const exportList = Object . keys ( families ) ;
589587
@@ -649,7 +647,7 @@ export class Python extends Writer {
649647 }
650648
651649 private pyFhirPackageByName ( name : string ) : string {
652- return [ this . rootPackage , this . buildPyPackageName ( name ) ] . join ( "." ) ;
650+ return [ this . opts . rootPackageName , this . buildPyPackageName ( name ) ] . join ( "." ) ;
653651 }
654652
655653 private pyPackage ( identifier : Identifier ) : string {
@@ -667,14 +665,12 @@ export class Python extends Writer {
667665 fs . cpSync ( Path . resolve ( this . staticDir ) , this . opts . outputDir , { recursive : true } ) ;
668666 }
669667
670- getFieldFormatFunction ( format : string | undefined ) : ( name : string ) => string {
671- if ( ! format ) {
672- this . logger ( ) ?. warn ( `No field format specified. Defaulting to SnakeCase.` ) ;
673- return snakeCase ;
674- } else if ( ! AVAILABLE_STRING_FORMATS [ format ] ) {
668+ getFieldFormatFunction ( format : StringFormatKey ) : ( name : string ) => string {
669+ if ( ! AVAILABLE_STRING_FORMATS [ format ] ) {
675670 this . logger ( ) ?. warn ( `Unknown field format '${ format } '. Defaulting to SnakeCase.` ) ;
676671 this . logger ( ) ?. warn ( `Supported formats: ${ Object . keys ( AVAILABLE_STRING_FORMATS ) . join ( ", " ) } ` ) ;
677672 return snakeCase ;
678- } else return AVAILABLE_STRING_FORMATS [ format ] ;
673+ }
674+ return AVAILABLE_STRING_FORMATS [ format ] ;
679675 }
680676}
0 commit comments