|
18 | 18 |
|
19 | 19 | - Parses protobuf definitions and creates a structured representation in TypeScript. |
20 | 20 | - 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 |
22 | 25 | - Produces JSON or TypeScript files mapping enum names to integer values and vice versa |
| 26 | +- Supports tree-shakable enum utilities for optimal bundle sizes |
23 | 27 |
|
24 | 28 | ## Parsing and Generating Files |
25 | 29 |
|
@@ -90,6 +94,86 @@ export const enumToStrMap = { |
90 | 94 | export type EnumToStrMap = typeof enumToStrMap; |
91 | 95 | ``` |
92 | 96 |
|
| 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 | + |
93 | 177 | ## Configuration |
94 | 178 |
|
95 | 179 | 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 |
145 | 229 | | Option | Description | Default Value | |
146 | 230 | |----------------------------------------|---------------------------------------------------------------------------------|--------------------| |
147 | 231 | | `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'` | |
149 | 237 | | `utils.astHelpers.enabled` | Outputs TypeScript helpers for building PostgreSQL ASTs. | `false` | |
150 | 238 | | `utils.astHelpers.wrappedTypesSource` | Path to the TypeScript types to use when generating AST helpers. | `'./wrapped'` | |
151 | 239 | | `utils.astHelpers.inlineNestedObj` | Whether to inline `nested-obj` code within the generated file. | `false` | |
|
0 commit comments