Skip to content

Commit c01f340

Browse files
committed
docs: update README with enum utility enhancements
- Add documentation for unidirectional enum conversion functions - Document nested objects output format option - Include examples for all three generation modes - Update features list to highlight new capabilities - Add configuration options table for new enum utility settings
1 parent bd14821 commit c01f340

File tree

2 files changed

+167
-6
lines changed

2 files changed

+167
-6
lines changed

IMPLEMENTATION_SUMMARY.md

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Implementation Summary: Unidirectional Enum Conversion Functions
1+
# Implementation Summary: Enum Conversion Functions Enhancements
22

33
## What Was Implemented
44

@@ -77,8 +77,81 @@ const name = getEnumString("OverridingKind", 1); // Returns: "OVERRIDING_USER_VA
7777

7878
Added comprehensive tests in `__tests__/enum-utils.test.ts` covering:
7979
- Bidirectional generation (default behavior)
80-
- Unidirectional generation
81-
- Custom filenames
80+
- Unidirectional generation with switch statements
81+
- Unidirectional generation with nested objects
82+
- Custom filenames for both formats
8283
- Explicit bidirectional mode
8384

84-
All tests pass and generate appropriate snapshots.
85+
All tests pass and generate appropriate snapshots.
86+
87+
## Part 2: Nested Objects Output Format
88+
89+
### 1. New Generator Functions
90+
91+
Added two new functions for nested objects format:
92+
93+
- **`generateEnumToIntFunctionsNested`**: Generates an object map where each enum has its own converter function
94+
- **`generateEnumToStringFunctionsNested`**: Similar but for number to string conversions
95+
96+
### 2. Configuration Enhancement
97+
98+
Added `outputFormat` option:
99+
100+
```typescript
101+
utils: {
102+
enums: {
103+
outputFormat?: 'switchStatements' | 'nestedObjects'; // Default: 'switchStatements'
104+
}
105+
}
106+
```
107+
108+
### 3. Generated Output Structure
109+
110+
#### Switch Statements Format (Default)
111+
```typescript
112+
export const getEnumInt = (enumType: EnumType, key: string): number => {
113+
switch (enumType) {
114+
case "EnumName": {
115+
switch (key) {
116+
case "VALUE": return 0;
117+
// ...
118+
}
119+
}
120+
}
121+
}
122+
```
123+
124+
#### Nested Objects Format
125+
```typescript
126+
export const enumToIntMap: EnumToIntMap = {
127+
"EnumName": (key: string): number => {
128+
switch (key) {
129+
case "VALUE": return 0;
130+
// ...
131+
}
132+
},
133+
// ... more enums
134+
}
135+
```
136+
137+
### 4. Usage Comparison
138+
139+
#### Switch Statements
140+
```typescript
141+
import { getEnumInt } from './enum-to-int';
142+
const value = getEnumInt("OverridingKind", "OVERRIDING_USER_VALUE");
143+
```
144+
145+
#### Nested Objects
146+
```typescript
147+
import { enumToIntMap } from './enum-to-int-map';
148+
const value = enumToIntMap.OverridingKind("OVERRIDING_USER_VALUE");
149+
```
150+
151+
### 5. Benefits of Nested Objects Format
152+
153+
1. **Better Tree-Shaking**: Bundlers can eliminate unused enum converters
154+
2. **Direct Access**: No need to pass enum name as parameter
155+
3. **Type-Safe Properties**: TypeScript knows exactly which enums are available
156+
4. **Modular Organization**: Each enum has its own isolated converter function
157+
5. **Cleaner API**: More intuitive object-oriented interface

packages/proto-parser/README.md

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818

1919
- Parses protobuf definitions and creates a structured representation in TypeScript.
2020
- Generates TypeScript interfaces for protobuf messages.
21-
- Creates utility functions for enum value conversions.
21+
- Creates utility functions for enum value conversions with multiple output formats:
22+
- Bidirectional conversion functions (string ↔ number)
23+
- Unidirectional conversion functions with precise types
24+
- Switch statement or nested object output formats
2225
- Produces JSON or TypeScript files mapping enum names to integer values and vice versa
26+
- Supports tree-shakable enum utilities for optimal bundle sizes
2327

2428
## Parsing and Generating Files
2529

@@ -90,6 +94,86 @@ export const enumToStrMap = {
9094
export type EnumToStrMap = typeof enumToStrMap;
9195
```
9296

97+
### Example: Generating Enum Utility Functions
98+
99+
You can generate utility functions for runtime enum conversions:
100+
101+
```js
102+
// Bidirectional function (default)
103+
const parser = new PgProtoParser(inFile, {
104+
outDir,
105+
utils: {
106+
enums: {
107+
enabled: true,
108+
filename: 'enum-utils.ts'
109+
}
110+
}
111+
});
112+
113+
// Unidirectional functions with switch statements
114+
const parser = new PgProtoParser(inFile, {
115+
outDir,
116+
utils: {
117+
enums: {
118+
enabled: true,
119+
unidirectional: true,
120+
toIntFilename: 'enum-to-int.ts',
121+
toStringFilename: 'enum-to-string.ts'
122+
}
123+
}
124+
});
125+
126+
// Unidirectional functions with nested objects format
127+
const parser = new PgProtoParser(inFile, {
128+
outDir,
129+
utils: {
130+
enums: {
131+
enabled: true,
132+
unidirectional: true,
133+
outputFormat: 'nestedObjects',
134+
toIntFilename: 'enum-to-int-map.ts',
135+
toStringFilename: 'enum-to-string-map.ts'
136+
}
137+
}
138+
});
139+
```
140+
141+
#### Generated Utility Functions
142+
143+
**Bidirectional (default):**
144+
```ts
145+
// utils.ts
146+
export const getEnumValue = (enumType: EnumType, key: string | number) => {
147+
// Returns number for string input, string for number input
148+
};
149+
```
150+
151+
**Unidirectional with switch statements:**
152+
```ts
153+
// enum-to-int.ts
154+
export const getEnumInt = (enumType: EnumType, key: string): number => {
155+
// Converts enum string to number
156+
};
157+
158+
// enum-to-string.ts
159+
export const getEnumString = (enumType: EnumType, key: number): string => {
160+
// Converts enum number to string
161+
};
162+
```
163+
164+
**Unidirectional with nested objects:**
165+
```ts
166+
// enum-to-int-map.ts
167+
export const enumToIntMap = {
168+
OverridingKind: (key: string): number => { /* ... */ },
169+
QuerySource: (key: string): number => { /* ... */ },
170+
// ... more enums
171+
};
172+
173+
// Usage
174+
const value = enumToIntMap.OverridingKind("OVERRIDING_USER_VALUE"); // Returns: 1
175+
```
176+
93177
## Configuration
94178

95179
You can configure `pg-proto-parser` by passing different parameters to the `ProtoStore` constructor:
@@ -145,7 +229,11 @@ The options for `PgProtoParserOptions` are organized into the following categori
145229
| Option | Description | Default Value |
146230
|----------------------------------------|---------------------------------------------------------------------------------|--------------------|
147231
| `utils.enums.enabled` | Whether to generate TypeScript utility functions for enums. | `false` |
148-
| `utils.enums.filename` | Filename for the generated enums utilities. | `'utils.ts'` |
232+
| `utils.enums.filename` | Filename for the generated enums utilities (bidirectional). | `'utils.ts'` |
233+
| `utils.enums.unidirectional` | Generate separate unidirectional conversion functions instead of bidirectional. | `false` |
234+
| `utils.enums.toIntFilename` | Filename for string-to-int conversion utilities (when unidirectional). | `'enum-to-int.ts'` |
235+
| `utils.enums.toStringFilename` | Filename for int-to-string conversion utilities (when unidirectional). | `'enum-to-string.ts'` |
236+
| `utils.enums.outputFormat` | Output format: 'switchStatements' or 'nestedObjects'. | `'switchStatements'` |
149237
| `utils.astHelpers.enabled` | Outputs TypeScript helpers for building PostgreSQL ASTs. | `false` |
150238
| `utils.astHelpers.wrappedTypesSource` | Path to the TypeScript types to use when generating AST helpers. | `'./wrapped'` |
151239
| `utils.astHelpers.inlineNestedObj` | Whether to inline `nested-obj` code within the generated file. | `false` |

0 commit comments

Comments
 (0)