Commit 36e95bb
feat(compiler): Add JavaScript/TypeScript IDL code generation (#3394)
## Summary
Implements TypeScript code generation for Fory IDL within the
fory-compiler, converting FDL (Fory Definition Language) schema files
into pure TypeScript type definitions. Zero runtime dependencies, with
comprehensive test coverage (12/12 tests passing), supporting messages,
enums, unions, and all primitive types.
## Changes
### Core Implementation
- **compiler/fory_compiler/generators/typescript.py** - TypeScript code
generator extending BaseGenerator (365 lines)
- Generates type-safe TypeScript interfaces, enums, and discriminated
unions
- Supports nested types, collections, and optional fields
- Proper type mapping for all 25 FDL primitive kinds
- Field name conversion (snake_case → camelCase)
- Registration helper function generation for Fory serialization
integration
- **compiler/fory_compiler/generators/__init__.py** - Registration of
TypeScriptGenerator in the compiler ecosystem
- **compiler/fory_compiler/cli.py** - Added --typescript_out CLI
argument for TypeScript code generation
- **compiler/fory_compiler/tests/test_typescript_codegen.py** - 12
golden codegen tests covering:
- Enum and message generation
- Nested types (messages and enums)
- Discriminated unions
- All primitive type mappings
- Collection types (arrays, maps)
- Field naming conventions
- File structure and licensing
- Zero runtime dependencies validation
### Features
- **Message-to-Interface Generation**: Auto-converts FDL messages to
TypeScript interfaces
- **Enum Support**: Generates TypeScript enums with proper value
stripping and type IDs
- **Discriminated Unions**: Creates union types with discriminator enums
for type safety
- **Type Mappings**: Full support for all FDL primitives (bool→boolean,
int32→number, int64→bigint|number, float/double→number, etc.)
- **Nested Types**: Supports nested messages and enums within parent
types
- **Collection Types**: Arrays (repeated fields) and maps with type-safe
generics
- **Zero Runtime Dependencies**: Pure TypeScript type definitions, no
gRPC or external imports
- **Field Naming**: Automatic conversion to camelCase per TypeScript
conventions
- **Package/Module Handling**: Uses last segment of package name for
module name and registration functions
- **License Headers**: All generated files include Apache 2.0 license
headers
- **Registration Helpers**: Generates registration functions for Fory
serialization framework integration
### AI Assistance Checklist
- [x] Substantial AI assistance was used in this PR (yes)
- [x] I included the standardized AI Usage Disclosure block below
- [x] I can explain and defend all important changes without AI help
- [x] I reviewed AI-assisted code changes line by line before submission
- [x] I ran adequate human verification and recorded evidence (local/CI
checks, pass/fail summary, and review confirmation)
- [x] I added/updated tests and specs where required
- [x] I validated protocol/performance impacts with evidence when
applicable
- [x] I verified licensing and provenance compliance
AI Usage Disclosure
- substantial_ai_assistance: yes
- scope: <design drafting | code drafting>
- affected_files_or_subsystems: <high-level paths/modules>
- human_verification: <checks run locally or in CI + pass/fail summary +
contributor reviewed results>
- performance_verification: <N/A>
- provenance_license_confirmation: <Apache-2.0-compatible provenance
confirmed; no incompatible third-party code introduced>
### AI Review Evidence
#### General AI Review
Initial Review
• Identified issues in decimal type mapping, array type precedence, tag
id drop, and traversalContainer doesn't traverse union cases
Fixes Applied
• Updated DECIMAL mapping to string to avoid precision loss
• fix array type precedence issue.
• tag id drop prevention for id ==
0
• Aligned map type documentation with generated output
Final Review (Fresh Session)
• Re reviewed updated changes
• All issues resolved
• All tests pass. No
actionable issues remain
#### Fory-Specific AI Review
Scope
• Reviewed serialization and deserialization logic
• Verified typeInfo
handling, struct embedding, and reference tracking
• Checked alignment with Java reference implementation
• Validated cross
language compatibility and code generation
Result
• No actionable issues found
• Implementation aligns with Fory wire format and runtime behavior
Fixes #3280
---------
Co-authored-by: Shawn Yang <shawn.ck.yang@gmail.com>1 parent f129b21 commit 36e95bb
File tree
37 files changed
+3035
-259
lines changed- .github/workflows
- compiler
- fory_compiler
- generators
- tests
- docs/compiler
- integration_tests/idl_tests
- javascript
- test
- java/src/test/java/org/apache/fory/idl_tests
- javascript
- packages/core/lib
- gen
- meta
- reader
- writer
- test
37 files changed
+3035
-259
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
494 | 494 | | |
495 | 495 | | |
496 | 496 | | |
| 497 | + | |
497 | 498 | | |
498 | 499 | | |
499 | 500 | | |
| |||
502 | 503 | | |
503 | 504 | | |
504 | 505 | | |
| 506 | + | |
| 507 | + | |
505 | 508 | | |
506 | 509 | | |
507 | 510 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | | - | |
| 67 | + | |
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
| 73 | + | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
| 76 | + | |
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| |||
185 | 185 | | |
186 | 186 | | |
187 | 187 | | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
201 | 201 | | |
202 | 202 | | |
203 | 203 | | |
| |||
285 | 285 | | |
286 | 286 | | |
287 | 287 | | |
288 | | - | |
| 288 | + | |
| 289 | + | |
289 | 290 | | |
290 | 291 | | |
291 | 292 | | |
| |||
422 | 423 | | |
423 | 424 | | |
424 | 425 | | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
425 | 445 | | |
426 | 446 | | |
427 | 447 | | |
| |||
431 | 451 | | |
432 | 452 | | |
433 | 453 | | |
434 | | - | |
| 454 | + | |
435 | 455 | | |
436 | 456 | | |
437 | 457 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
264 | 264 | | |
265 | 265 | | |
266 | 266 | | |
267 | | - | |
| 267 | + | |
268 | 268 | | |
269 | 269 | | |
270 | 270 | | |
| |||
343 | 343 | | |
344 | 344 | | |
345 | 345 | | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
346 | 354 | | |
347 | 355 | | |
348 | 356 | | |
| |||
650 | 658 | | |
651 | 659 | | |
652 | 660 | | |
| 661 | + | |
653 | 662 | | |
654 | 663 | | |
655 | 664 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| |||
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
| 37 | + | |
36 | 38 | | |
37 | 39 | | |
38 | 40 | | |
| |||
44 | 46 | | |
45 | 47 | | |
46 | 48 | | |
| 49 | + | |
47 | 50 | | |
48 | 51 | | |
49 | 52 | | |
0 commit comments