Skip to content

Commit bd14821

Browse files
committed
feat: add nested objects output format for enum utilities
- Add generateEnumToIntFunctionsNested and generateEnumToStringFunctionsNested - Add outputFormat option: 'switchStatements' | 'nestedObjects' - Generate typed object maps with individual converter functions per enum - Update store logic to support both output formats - Add comprehensive tests for nested objects format - Update documentation with nested objects usage and benefits Benefits of nested objects format: - Better tree-shaking - import only specific enum converters - Direct property access without passing enum name - Type-safe enum property access - More modular code organization
1 parent 0a6bd57 commit bd14821

File tree

12 files changed

+28094
-43
lines changed

12 files changed

+28094
-43
lines changed

IMPLEMENTATION_SUMMARY.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Implementation Summary: Unidirectional Enum Conversion Functions
2+
3+
## What Was Implemented
4+
5+
### 1. New Functions in `src/ast/enums/enums.ts`
6+
7+
- **`generateEnumToIntFunctions`**: Generates a function that converts enum string values to integers
8+
- Function name: `getEnumInt`
9+
- Signature: `(enumType: EnumType, key: string): number`
10+
- Returns typed number values
11+
12+
- **`generateEnumToStringFunctions`**: Generates a function that converts enum integer values to strings
13+
- Function name: `getEnumString`
14+
- Signature: `(enumType: EnumType, key: number): string`
15+
- Returns typed string values
16+
17+
### 2. Configuration Options
18+
19+
Added new options to `PgProtoStoreOptions`:
20+
21+
```typescript
22+
utils: {
23+
enums: {
24+
enabled: boolean; // Default: false
25+
filename: string; // Default: 'utils.ts'
26+
unidirectional?: boolean; // Default: false (NEW)
27+
toIntFilename?: string; // Default: 'enum-to-int.ts' (NEW)
28+
toStringFilename?: string; // Default: 'enum-to-string.ts' (NEW)
29+
}
30+
}
31+
```
32+
33+
### 3. Store Logic Updates
34+
35+
Modified `writeUtilsEnums()` in `store.ts` to:
36+
- Check the `unidirectional` flag
37+
- Generate separate files for each direction when enabled
38+
- Maintain backward compatibility with bidirectional generation
39+
40+
### 4. Key Benefits
41+
42+
1. **Cleaner Types**: Each function has a precise signature (string → number or number → string)
43+
2. **Better IDE Support**: More accurate IntelliSense and autocomplete
44+
3. **Tree-Shaking**: Applications can import only the direction they need
45+
4. **Performance**: No runtime type checking for the input parameter type
46+
47+
### 5. Usage Examples
48+
49+
#### Configuration
50+
```typescript
51+
// Enable unidirectional functions
52+
{
53+
utils: {
54+
enums: {
55+
enabled: true,
56+
unidirectional: true,
57+
toIntFilename: 'enum-to-int.ts',
58+
toStringFilename: 'enum-to-string.ts'
59+
}
60+
}
61+
}
62+
```
63+
64+
#### Generated Code Usage
65+
```typescript
66+
import { getEnumInt } from './enum-to-int';
67+
import { getEnumString } from './enum-to-string';
68+
69+
// Convert string to number
70+
const value = getEnumInt("OverridingKind", "OVERRIDING_USER_VALUE"); // Returns: 1
71+
72+
// Convert number to string
73+
const name = getEnumString("OverridingKind", 1); // Returns: "OVERRIDING_USER_VALUE"
74+
```
75+
76+
### 6. Testing
77+
78+
Added comprehensive tests in `__tests__/enum-utils.test.ts` covering:
79+
- Bidirectional generation (default behavior)
80+
- Unidirectional generation
81+
- Custom filenames
82+
- Explicit bidirectional mode
83+
84+
All tests pass and generate appropriate snapshots.

0 commit comments

Comments
 (0)