cel-proto-parser 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +136 -0
- package/ast/enums.d.ts +14 -0
- package/ast/enums.js +59 -0
- package/ast/index.d.ts +2 -0
- package/ast/index.js +18 -0
- package/ast/types.d.ts +40 -0
- package/ast/types.js +158 -0
- package/converter/index.d.ts +22 -0
- package/converter/index.js +242 -0
- package/deparser/index.d.ts +95 -0
- package/deparser/index.js +504 -0
- package/esm/ast/enums.js +22 -0
- package/esm/ast/index.js +2 -0
- package/esm/ast/types.js +117 -0
- package/esm/converter/index.js +239 -0
- package/esm/deparser/index.js +499 -0
- package/esm/index.js +6 -0
- package/esm/options/defaults.js +68 -0
- package/esm/options/index.js +2 -0
- package/esm/options/types.js +4 -0
- package/esm/parser.js +211 -0
- package/esm/utils/index.js +79 -0
- package/index.d.ts +6 -0
- package/index.js +28 -0
- package/options/defaults.d.ts +2 -0
- package/options/defaults.js +71 -0
- package/options/index.d.ts +2 -0
- package/options/index.js +18 -0
- package/options/types.d.ts +84 -0
- package/options/types.js +5 -0
- package/package.json +55 -0
- package/parser.d.ts +68 -0
- package/parser.js +251 -0
- package/utils/index.d.ts +46 -0
- package/utils/index.js +127 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Constructive <developers@constructive.io>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# cel-proto-parser
|
|
2
|
+
|
|
3
|
+
CEL (Common Expression Language) proto parser and deparser for TypeScript.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This package provides tools for working with CEL (Common Expression Language) Abstract Syntax Trees (ASTs):
|
|
8
|
+
|
|
9
|
+
1. **Proto Parser**: Parse CEL protobuf definitions to generate TypeScript types
|
|
10
|
+
2. **Deparser**: Convert CEL AST back to CEL expression strings
|
|
11
|
+
3. **AST Helpers**: Factory functions for constructing CEL AST nodes
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install cel-proto-parser
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
### Deparser
|
|
22
|
+
|
|
23
|
+
Convert CEL AST to expression strings:
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { deparse, Expr } from 'cel-proto-parser';
|
|
27
|
+
|
|
28
|
+
// Simple expression: x > 5
|
|
29
|
+
const expr: Expr = {
|
|
30
|
+
callExpr: {
|
|
31
|
+
function: '_>_',
|
|
32
|
+
args: [
|
|
33
|
+
{ identExpr: { name: 'x' } },
|
|
34
|
+
{ constExpr: { int64Value: 5 } }
|
|
35
|
+
]
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
console.log(deparse(expr)); // Output: "x > 5"
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Proto Parser
|
|
43
|
+
|
|
44
|
+
Generate TypeScript types from CEL proto files:
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { CelProtoParser } from 'cel-proto-parser';
|
|
48
|
+
|
|
49
|
+
const parser = new CelProtoParser('path/to/syntax.proto', {
|
|
50
|
+
outDir: './generated',
|
|
51
|
+
types: { enabled: true },
|
|
52
|
+
enums: { enabled: true },
|
|
53
|
+
utils: { astHelpers: { enabled: true } },
|
|
54
|
+
deparser: { enabled: true }
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
parser.write();
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## CEL Expression Types
|
|
61
|
+
|
|
62
|
+
The deparser supports all CEL expression types:
|
|
63
|
+
|
|
64
|
+
- **Constants**: `null`, `true`, `false`, integers, floats, strings, bytes
|
|
65
|
+
- **Identifiers**: `request`, `user`, etc.
|
|
66
|
+
- **Field Access**: `request.auth.claims`
|
|
67
|
+
- **Function Calls**: `size(list)`, `str.startsWith("prefix")`
|
|
68
|
+
- **Operators**: `+`, `-`, `*`, `/`, `%`, `==`, `!=`, `<`, `<=`, `>`, `>=`, `&&`, `||`, `!`, `-` (unary)
|
|
69
|
+
- **Ternary**: `condition ? trueExpr : falseExpr`
|
|
70
|
+
- **Index Access**: `list[0]`, `map["key"]`
|
|
71
|
+
- **Lists**: `[1, 2, 3]`
|
|
72
|
+
- **Maps**: `{"key": value}`
|
|
73
|
+
- **Message Construction**: `MyMessage{field: value}`
|
|
74
|
+
- **Macros**: `has()`, `all()`, `exists()`, etc.
|
|
75
|
+
|
|
76
|
+
## API Reference
|
|
77
|
+
|
|
78
|
+
### `deparse(expr: Expr, options?: DeparserOptions): string`
|
|
79
|
+
|
|
80
|
+
Converts a CEL AST expression to a string.
|
|
81
|
+
|
|
82
|
+
Options:
|
|
83
|
+
- `spaces`: Whether to add spaces around operators (default: `true`)
|
|
84
|
+
|
|
85
|
+
### `CelProtoParser`
|
|
86
|
+
|
|
87
|
+
Parses CEL proto files and generates TypeScript code.
|
|
88
|
+
|
|
89
|
+
Constructor options:
|
|
90
|
+
- `outDir`: Output directory for generated files
|
|
91
|
+
- `types.enabled`: Generate TypeScript interfaces
|
|
92
|
+
- `enums.enabled`: Generate TypeScript enums
|
|
93
|
+
- `utils.astHelpers.enabled`: Generate AST helper functions
|
|
94
|
+
- `deparser.enabled`: Generate deparser module
|
|
95
|
+
|
|
96
|
+
## License
|
|
97
|
+
|
|
98
|
+
MIT
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## Development
|
|
103
|
+
|
|
104
|
+
### Setup
|
|
105
|
+
|
|
106
|
+
1. Clone the repository:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
git clone https://github.com/constructive-io/dev-utils.git
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
2. Install dependencies:
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
cd dev-utils
|
|
116
|
+
pnpm install
|
|
117
|
+
pnpm build
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
3. Test the package of interest:
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
cd packages/<packagename>
|
|
124
|
+
pnpm test:watch
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Credits
|
|
128
|
+
|
|
129
|
+
Built for developers, with developers.
|
|
130
|
+
👉 https://launchql.com | https://hyperweb.io
|
|
131
|
+
|
|
132
|
+
## Disclaimer
|
|
133
|
+
|
|
134
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
135
|
+
|
|
136
|
+
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
package/ast/enums.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as t from '@babel/types';
|
|
2
|
+
interface ProtoEnum {
|
|
3
|
+
name: string;
|
|
4
|
+
values: Record<string, number>;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Convert a protobuf Enum to a TypeScript string union type
|
|
8
|
+
*/
|
|
9
|
+
export declare function convertEnumToTsUnionType(enumNode: ProtoEnum): t.ExportNamedDeclaration;
|
|
10
|
+
/**
|
|
11
|
+
* Convert a protobuf Enum to a TypeScript enum declaration
|
|
12
|
+
*/
|
|
13
|
+
export declare function convertEnumToTsEnumDeclaration(enumNode: ProtoEnum): t.ExportNamedDeclaration;
|
|
14
|
+
export {};
|
package/ast/enums.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.convertEnumToTsUnionType = convertEnumToTsUnionType;
|
|
37
|
+
exports.convertEnumToTsEnumDeclaration = convertEnumToTsEnumDeclaration;
|
|
38
|
+
const t = __importStar(require("@babel/types"));
|
|
39
|
+
/**
|
|
40
|
+
* Convert a protobuf Enum to a TypeScript string union type
|
|
41
|
+
*/
|
|
42
|
+
function convertEnumToTsUnionType(enumNode) {
|
|
43
|
+
const enumName = enumNode.name;
|
|
44
|
+
const values = enumNode.values;
|
|
45
|
+
// Create string literal types for each enum value
|
|
46
|
+
const unionTypes = Object.keys(values).map((key) => t.tsLiteralType(t.stringLiteral(key)));
|
|
47
|
+
const typeAlias = t.tsTypeAliasDeclaration(t.identifier(enumName), null, t.tsUnionType(unionTypes));
|
|
48
|
+
return t.exportNamedDeclaration(typeAlias, []);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Convert a protobuf Enum to a TypeScript enum declaration
|
|
52
|
+
*/
|
|
53
|
+
function convertEnumToTsEnumDeclaration(enumNode) {
|
|
54
|
+
const enumName = enumNode.name;
|
|
55
|
+
const values = enumNode.values;
|
|
56
|
+
const members = Object.entries(values).map(([key, value]) => t.tsEnumMember(t.identifier(key), t.numericLiteral(value)));
|
|
57
|
+
const enumDecl = t.tsEnumDeclaration(t.identifier(enumName), members);
|
|
58
|
+
return t.exportNamedDeclaration(enumDecl, []);
|
|
59
|
+
}
|
package/ast/index.d.ts
ADDED
package/ast/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./types"), exports);
|
|
18
|
+
__exportStar(require("./enums"), exports);
|
package/ast/types.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as t from '@babel/types';
|
|
2
|
+
import { ResolvedCelProtoParserOptions } from '../options';
|
|
3
|
+
/**
|
|
4
|
+
* Resolve a protobuf type name to a TypeScript type reference
|
|
5
|
+
*/
|
|
6
|
+
export declare function resolveTypeName(typeName: string): t.TSType;
|
|
7
|
+
/**
|
|
8
|
+
* Generate import statements for enums
|
|
9
|
+
*/
|
|
10
|
+
export declare function generateEnumImports(enums: Array<{
|
|
11
|
+
name: string;
|
|
12
|
+
}>, source: string): t.ImportDeclaration;
|
|
13
|
+
/**
|
|
14
|
+
* Generate a Node union type that includes all AST node types
|
|
15
|
+
*/
|
|
16
|
+
export declare function generateNodeUnionType(types: Array<{
|
|
17
|
+
name: string;
|
|
18
|
+
}>): t.ExportNamedDeclaration;
|
|
19
|
+
interface ProtoField {
|
|
20
|
+
name: string;
|
|
21
|
+
type: string;
|
|
22
|
+
rule?: string;
|
|
23
|
+
}
|
|
24
|
+
interface ProtoType {
|
|
25
|
+
name: string;
|
|
26
|
+
fields: Record<string, ProtoField>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Convert a protobuf Type to a TypeScript interface
|
|
30
|
+
*/
|
|
31
|
+
export declare function convertTypeToTsInterface(type: ProtoType, options: ResolvedCelProtoParserOptions): t.ExportNamedDeclaration;
|
|
32
|
+
/**
|
|
33
|
+
* Generate AST helper factory methods
|
|
34
|
+
*/
|
|
35
|
+
export declare function generateAstHelperMethods(types: ProtoType[]): t.ExportDefaultDeclaration;
|
|
36
|
+
/**
|
|
37
|
+
* Generate import specifiers for types
|
|
38
|
+
*/
|
|
39
|
+
export declare function generateTypeImportSpecifiers(types: ProtoType[], options: ResolvedCelProtoParserOptions): t.ImportDeclaration;
|
|
40
|
+
export {};
|
package/ast/types.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.resolveTypeName = resolveTypeName;
|
|
37
|
+
exports.generateEnumImports = generateEnumImports;
|
|
38
|
+
exports.generateNodeUnionType = generateNodeUnionType;
|
|
39
|
+
exports.convertTypeToTsInterface = convertTypeToTsInterface;
|
|
40
|
+
exports.generateAstHelperMethods = generateAstHelperMethods;
|
|
41
|
+
exports.generateTypeImportSpecifiers = generateTypeImportSpecifiers;
|
|
42
|
+
const t = __importStar(require("@babel/types"));
|
|
43
|
+
const utils_1 = require("../utils");
|
|
44
|
+
// Type mapping from protobuf to TypeScript
|
|
45
|
+
const PROTO_TO_TS_TYPE = {
|
|
46
|
+
string: 'string',
|
|
47
|
+
bool: 'boolean',
|
|
48
|
+
int32: 'number',
|
|
49
|
+
int64: 'bigint',
|
|
50
|
+
uint32: 'number',
|
|
51
|
+
uint64: 'bigint',
|
|
52
|
+
float: 'number',
|
|
53
|
+
double: 'number',
|
|
54
|
+
bytes: 'Uint8Array'
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Resolve a protobuf type name to a TypeScript type reference
|
|
58
|
+
*/
|
|
59
|
+
function resolveTypeName(typeName) {
|
|
60
|
+
// Handle well-known Google protobuf types
|
|
61
|
+
if (typeName.startsWith('google.protobuf.')) {
|
|
62
|
+
const shortName = typeName.replace('google.protobuf.', '');
|
|
63
|
+
// Return as a qualified type reference
|
|
64
|
+
return t.tsTypeReference(t.tsQualifiedName(t.tsQualifiedName(t.identifier('google'), t.identifier('protobuf')), t.identifier(shortName)));
|
|
65
|
+
}
|
|
66
|
+
// Handle primitive types
|
|
67
|
+
const tsType = PROTO_TO_TS_TYPE[typeName];
|
|
68
|
+
if (tsType) {
|
|
69
|
+
switch (tsType) {
|
|
70
|
+
case 'string':
|
|
71
|
+
return t.tsStringKeyword();
|
|
72
|
+
case 'boolean':
|
|
73
|
+
return t.tsBooleanKeyword();
|
|
74
|
+
case 'number':
|
|
75
|
+
return t.tsNumberKeyword();
|
|
76
|
+
case 'bigint':
|
|
77
|
+
return t.tsBigIntKeyword();
|
|
78
|
+
default:
|
|
79
|
+
return t.tsTypeReference(t.identifier(tsType));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// Handle custom types (reference to other interfaces)
|
|
83
|
+
return t.tsTypeReference(t.identifier(typeName));
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Generate import statements for enums
|
|
87
|
+
*/
|
|
88
|
+
function generateEnumImports(enums, source) {
|
|
89
|
+
return (0, utils_1.createNamedImport)(enums.map((e) => e.name), source);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Generate a Node union type that includes all AST node types
|
|
93
|
+
*/
|
|
94
|
+
function generateNodeUnionType(types) {
|
|
95
|
+
// Generate wrapped object types: { TypeName: TypeName }
|
|
96
|
+
const unionTypeNames = types.map((type) => t.tsTypeLiteral([
|
|
97
|
+
t.tsPropertySignature(t.identifier(type.name), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(type.name))))
|
|
98
|
+
]));
|
|
99
|
+
const unionTypeAlias = t.tsTypeAliasDeclaration(t.identifier('Node'), null, t.tsUnionType(unionTypeNames));
|
|
100
|
+
return t.exportNamedDeclaration(unionTypeAlias, []);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Convert a protobuf Type to a TypeScript interface
|
|
104
|
+
*/
|
|
105
|
+
function convertTypeToTsInterface(type, options) {
|
|
106
|
+
const typeName = type.name;
|
|
107
|
+
const fields = type.fields;
|
|
108
|
+
const properties = Object.entries(fields).map(([fieldName, field]) => {
|
|
109
|
+
const resolvedType = resolveTypeName(field.type);
|
|
110
|
+
const fieldType = field.rule === 'repeated' ? t.tsArrayType(resolvedType) : resolvedType;
|
|
111
|
+
const prop = t.tsPropertySignature(t.identifier((0, utils_1.getFieldName)(field, fieldName)), t.tsTypeAnnotation(fieldType));
|
|
112
|
+
prop.optional = options.types.optionalFields;
|
|
113
|
+
return prop;
|
|
114
|
+
});
|
|
115
|
+
const interfaceDecl = t.tsInterfaceDeclaration(t.identifier(typeName), null, [], t.tsInterfaceBody(properties));
|
|
116
|
+
return t.exportNamedDeclaration(interfaceDecl, []);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Generate AST helper factory methods
|
|
120
|
+
*/
|
|
121
|
+
function generateAstHelperMethods(types) {
|
|
122
|
+
const creators = types.map((type) => {
|
|
123
|
+
const typeName = type.name;
|
|
124
|
+
const param = t.identifier('_p');
|
|
125
|
+
param.optional = true;
|
|
126
|
+
param.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier(typeName)));
|
|
127
|
+
const fields = type.fields;
|
|
128
|
+
// const _j = {} as TypeName;
|
|
129
|
+
const astNodeInit = t.variableDeclaration('const', [
|
|
130
|
+
t.variableDeclarator(t.identifier('_j'), t.tsAsExpression(t.objectExpression([]), t.tsTypeReference(t.identifier(typeName))))
|
|
131
|
+
]);
|
|
132
|
+
// _o.set(_j, 'fieldName', _p?.fieldName);
|
|
133
|
+
const setStatements = Object.entries(fields).map(([fName, field]) => {
|
|
134
|
+
const fieldName = (0, utils_1.getFieldName)(field, fName);
|
|
135
|
+
return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('_o'), t.identifier('set')), [
|
|
136
|
+
t.identifier('_j'),
|
|
137
|
+
t.stringLiteral(fieldName),
|
|
138
|
+
t.optionalMemberExpression(t.identifier('_p'), t.identifier(fieldName), false, true)
|
|
139
|
+
]));
|
|
140
|
+
});
|
|
141
|
+
const methodName = (0, utils_1.toCamelCase)(typeName);
|
|
142
|
+
const method = t.objectMethod('method', t.identifier(methodName), [param], t.blockStatement([
|
|
143
|
+
astNodeInit,
|
|
144
|
+
...setStatements,
|
|
145
|
+
t.returnStatement(t.identifier('_j'))
|
|
146
|
+
]));
|
|
147
|
+
method.returnType = t.tsTypeAnnotation(t.tsTypeReference(t.identifier(typeName)));
|
|
148
|
+
return method;
|
|
149
|
+
});
|
|
150
|
+
return t.exportDefaultDeclaration(t.objectExpression(creators));
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Generate import specifiers for types
|
|
154
|
+
*/
|
|
155
|
+
function generateTypeImportSpecifiers(types, options) {
|
|
156
|
+
const importSpecifiers = types.map((type) => t.importSpecifier(t.identifier(type.name), t.identifier(type.name)));
|
|
157
|
+
return t.importDeclaration(importSpecifiers, t.stringLiteral(options.utils.astHelpers.typesSource));
|
|
158
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CEL AST Converter
|
|
3
|
+
*
|
|
4
|
+
* Converts AST from @marcbachmann/cel-js format to CEL proto format
|
|
5
|
+
* used by our deparser.
|
|
6
|
+
*/
|
|
7
|
+
import type { Expr } from '../deparser';
|
|
8
|
+
/**
|
|
9
|
+
* AST node from @marcbachmann/cel-js
|
|
10
|
+
*/
|
|
11
|
+
interface MarcAstNode {
|
|
12
|
+
op: string;
|
|
13
|
+
args: unknown;
|
|
14
|
+
macro?: Record<string, unknown>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Convert @marcbachmann/cel-js AST to CEL proto Expr format
|
|
18
|
+
*/
|
|
19
|
+
export declare function convertToProtoExpr(node: MarcAstNode, idCounter?: {
|
|
20
|
+
value: number;
|
|
21
|
+
}): Expr;
|
|
22
|
+
export { MarcAstNode };
|