@@ -37,13 +37,15 @@ This is a FHIR code generation toolkit with a **three-stage pipeline**:
3737
3838### 2. High-Level API (` src/api/ ` )
3939- ** APIBuilder** (` builder.ts ` ): Fluent interface for chaining operations
40- - ** Generators** (` generators /` ): Language-specific code generators
40+ - ** Generators** (` writer-generator /` ): Language-specific code generators
4141 - ` typescript.ts ` : Generates TypeScript interfaces and types
42+ - ` python.ts ` : Generates Python/Pydantic models
43+ - ` csharp.ts ` : Generates C# classes
4244
4345### 3. CLI Interface (` src/cli/ ` )
4446- ** Commands** (` commands/ ` ):
4547 - ` typeschema ` : Generate and validate TypeSchema from FHIR packages
46- - ` generate ` : Generate code from TypeSchema (TypeScript)
48+ - ` generate ` : Generate code from TypeSchema (TypeScript, Python, C# )
4749- ** Main entry** (` index.ts ` ): CLI setup with yargs
4850
4951### Key Data Flow
@@ -89,26 +91,152 @@ FHIR Package → TypeSchema Generator → TypeSchema Format → Code Generators
8991## Important Implementation Details
9092
9193### FHIR Package Processing
92- - Supports FHIR R4 packages (R5 planned )
94+ - Supports FHIR R4 packages (R5 in progress )
9395- Handles profiles and extensions (US Core in development)
9496- Caches parsed schemas for performance
95- - Multi-package dependency resolution
97+ - Multi-package dependency resolution via Canonical Manager
9698
9799### TypeSchema Format
98100- Intermediate representation between FHIR and target languages
99101- Enables multi-language code generation
100102- Supports field validation and constraints
101103- Handles nested types and references
104+ - Flattens FHIR's hierarchical structure for easier generation
102105
103106### Code Generation
104- - Modular generator system
107+ - Modular generator system via APIBuilder
108+ - Language-specific writers in ` src/api/writer-generator/ `
105109- TypeScript generator creates interfaces with proper inheritance
106- - Extensible for future languages (Python, Rust planned)
110+ - Extensible architecture for new languages
107111- Supports custom naming conventions and output formats
108112
113+ ## APIBuilder Flow
114+
115+ The ` APIBuilder ` class (` src/api/builder.ts ` ) provides the fluent API for the three-stage pipeline:
116+
117+ ``` typescript
118+ // Input stage - Choose one or combine:
119+ .fromPackage (" hl7.fhir.r4.core" , " 4.0.1" ) // NPM registry
120+ .fromPackageRef (" https://example.com/package.tgz" ) // Remote TGZ
121+ .localStructureDefinitions ({... }) // Local files
122+ .fromSchemas (array ) // TypeSchema objects
123+
124+ // Processing stage - Optional:
125+ .treeShake ({... }) // Filter types
126+ .writeTypeSchemas (" ./schemas" ) // Debug output
127+
128+ // Output stage - Choose one:
129+ .typescript ({... }) // TypeScript
130+ .python ({... }) // Python
131+ .csharp (" Namespace" , " ./path" ) // C#
132+
133+ // Finalize:
134+ .outputTo (" ./output" ) // Output directory
135+ .cleanOutput (true ) // Clean before generation
136+ .generate () // Execute
137+ ```
138+
139+ ## Core Concepts
140+
141+ ### TypeSchema
142+ - Universal intermediate format for FHIR data
143+ - Defined in ` src/typeschema/types.ts `
144+ - Contains: identifier, description, fields, dependencies, base type
145+ - Fields include type, required flag, array flag, binding info
146+ - Supports enums for constrained value sets
147+
148+ ### Transformers
149+ Located in ` src/typeschema/core/ ` :
150+ - ` transformer.ts ` : Main conversion logic from FHIR to TypeSchema
151+ - Handles different FHIR element types
152+ - Processes inheritance and choice types
153+ - Manages field flattening and snapshot generation
154+
155+ ### Writers
156+ Located in ` src/api/writer-generator/ ` :
157+ - Base ` Writer ` class: Handles I/O, indentation, formatting
158+ - Language writers: TypeScript, Python, C#, Mustache
159+ - Each writer traverses TypeSchema index and generates code
160+ - Maintains language-specific idioms and conventions
161+
162+ ## Common Development Patterns
163+
164+ ### Adding a New Generator Feature
165+ 1 . Extend the transformer in ` src/typeschema/core/transformer.ts ` to produce TypeSchema data
166+ 2 . Add logic to the language writer in ` src/api/writer-generator/[language].ts `
167+ 3 . Add tests in ` test/unit/typeschema/ ` and ` test/unit/api/ `
168+ 4 . Document in design docs if it's a major feature
169+
170+ ### Debugging TypeSchema Generation
171+ 1 . Use ` builder.writeTypeSchemas("./debug-schemas") ` to inspect intermediate output
172+ 2 . Check ` src/typeschema/types.ts ` for TypeSchema structure
173+ 3 . Review ` src/typeschema/core/transformer.ts ` for transformation logic
174+ 4 . Enable verbose logging with ` builder.setLogLevel("DEBUG") `
175+
176+ ### Testing Generated Code
177+ 1 . Use ` builder.build() ` instead of ` generate() ` to avoid file I/O
178+ 2 . Tests are organized by component in ` test/unit/ `
179+ 3 . Run ` bun test:coverage ` to see coverage metrics
180+ 4 . Use ` bun test --watch ` for development
181+
182+ ### Working with Tree Shaking
183+ - Configured via ` builder.treeShake({...}) `
184+ - Specify which resources and fields to include
185+ - Automatically resolves dependencies
186+ - Reference format: ` "hl7.fhir.r4.core#4.0.1" `
187+
188+ ## Key File Locations
189+
190+ ### Core Logic
191+ - ` src/index.ts ` - Main entry point and exports
192+ - ` src/config.ts ` - Configuration type definitions
193+ - ` src/api/builder.ts ` - APIBuilder implementation
194+ - ` src/typeschema/types.ts ` - TypeSchema type definitions
195+ - ` src/typeschema/generator.ts ` - TypeSchema generation orchestration
196+
197+ ### Generators
198+ - ` src/api/writer-generator/typescript.ts ` - TypeScript code generation
199+ - ` src/api/writer-generator/python.ts ` - Python/Pydantic generation
200+ - ` src/api/writer-generator/csharp.ts ` - C# generation
201+ - ` src/api/writer-generator/base.ts ` - Common writer utilities
202+
203+ ### FHIR Processing
204+ - ` src/typeschema/register.ts ` - Package registration and canonical resolution
205+ - ` src/typeschema/core/transformer.ts ` - FHIR → TypeSchema conversion
206+ - ` src/typeschema/core/field-builder.ts ` - Field extraction logic
207+ - ` src/typeschema/core/binding.ts ` - Value set and binding handling
208+
209+ ### Testing
210+ - ` test/unit/typeschema/ ` - TypeSchema processor tests
211+ - ` test/unit/api/ ` - Generator and builder tests
212+ - ` test/assets/ ` - Test fixtures and sample data
213+
214+ ## Known Limitations & Gotchas
215+
216+ 1 . ** R5 Support** : Limited, still in development
217+ 2 . ** Profile Extensions** : Basic parsing only, US Core in progress
218+ 3 . ** Choice Types** : Supported but representation differs by language
219+ 4 . ** Circular References** : Handled but may affect tree shaking
220+ 5 . ** Large Packages** : May require increased Node.js memory (` --max-old-space-size ` )
221+
222+ ## Performance Optimization Tips
223+
224+ 1 . Use tree shaking to reduce schema count
225+ 2 . Enable caching in APIBuilder
226+ 3 . Process large packages in batches
227+ 4 . Use ` build() ` instead of ` generate() ` for testing
228+ 5 . Run ` bun run quality ` before committing (combines typecheck, lint, test: unit )
229+
109230## Roadmap Context
110231
111232This toolkit focuses on type generation and code generation:
112- - ** Current** : TypeScript interface generation from FHIR schemas
113- - ** Next** : Profile/extension support and expanded TypeScript generator capabilities
114- - ** Future** : Multi-language support, GraphQL schemas, validation functions
233+ - ** Current** : TypeScript, Python, C# interface/class generation from FHIR R4
234+ - ** In Progress** : R5 support, profile/extension enhancements
235+ - ** Planned** : Rust, GraphQL, OpenAPI, validation functions, mock data generation
236+
237+ ## Useful External Resources
238+
239+ - [ FHIR Specification] ( https://www.hl7.org/fhir/ )
240+ - [ Canonical Manager] ( https://github.com/atomic-ehr/canonical-manager )
241+ - [ FHIR Schema] ( https://github.com/fhir-schema/fhir-schema )
242+ - [ TypeSchema Spec] ( https://www.health-samurai.io/articles/type-schema-a-pragmatic-approach-to-build-fhir-sdk )
0 commit comments