Skip to content

Commit 888df7e

Browse files
committed
py: refactoring: simplify generator parameter flow.
1 parent ed99c7d commit 888df7e

3 files changed

Lines changed: 19 additions & 23 deletions

File tree

src/api/builder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ export class APIBuilder {
264264

265265
const defaultPyOpts: PythonGeneratorOptions = {
266266
...defaultWriterOpts,
267-
// FIXME: change name
268-
packageName: "generated",
267+
rootPackageName: "generated",
268+
fieldFormat: "SnakeCase",
269269
};
270270

271271
const opts: PythonGeneratorOptions = {

src/api/writer-generator/python/python.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import { Writer, type WriterOptions } from "@root/api/writer-generator/writer.ts";
1212
import { groupByPackages, type TypeSchemaIndex } from "@root/typeschema/utils";
1313
import type { Field, Identifier, RegularTypeSchema } from "@typeschema/types.ts";
14-
import { pythonUtils } from "./utils.ts";
14+
import { PythonUtils } from "./utils.ts";
1515

1616
const 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;
8991
export 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

9698
interface 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
}

src/api/writer-generator/python/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Identifier } from "@root/typeschema";
22
import type { RegularTypeSchema } from "@typeschema/types.ts";
33

4-
export class pythonUtils {
4+
export class PythonUtils {
55
private resourceHierarchy: { parent: Identifier; child: Identifier }[] | null = [];
66

77
constructor() {

0 commit comments

Comments
 (0)