Skip to content

Commit 40b1b92

Browse files
Remove old generateTsAstCodeFromPgAst and rename schema-based function
- Remove implementation of generateTsAstCodeFromPgAst (old function without schema) - Rename generateTsAstCodeFromPgAstWithSchema to generateTsAstCodeFromPgAst - Update all test imports and function calls to use renamed function - Update README documentation to reflect schema requirement - All tests passing with updated snapshots The function now always uses runtime schema for proper node wrapping decisions. Co-Authored-By: Dan Lynch <[email protected]>
1 parent bcd44d3 commit 40b1b92

File tree

3 files changed

+13
-54
lines changed

3 files changed

+13
-54
lines changed

packages/proto-parser/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,23 +253,25 @@ Each of these options can be set when initializing the `PgProtoParser` to custom
253253

254254
`generateTsAstCodeFromPgAst` is a method that transforms a PostgreSQL Abstract Syntax Tree (AST) into TypeScript code capable of generating an equivalent AST. This function facilitates the dynamic creation of ASTs, allowing for programmable query construction and manipulation in TypeScript.
255255

256-
It generates code with syntax for [@pgsql/utils](https://github.com/launchql/pgsql-parser/tree/main/packages/utils), assuming you import the `ast` as as default import from `@pgsql/utils`:
256+
It generates code with syntax for [@pgsql/utils](https://github.com/launchql/pgsql-parser/tree/main/packages/utils), using runtime schema information to determine whether nodes should be wrapped (`t.nodes.*`) or unwrapped (`t.ast.*`).
257257

258258
```ts
259259
import {
260260
generateTsAstCodeFromPgAst
261261
} from 'pg-proto-parser';
262262
import { parse } from 'pgsql-parser';
263+
import { runtimeSchema } from 'pg-proto-parser/runtime-schema';
263264

264265
// Example SQL query
265266
const sql = 'SELECT * FROM my_table WHERE id = 1';
266267

267268
// Parse the SQL query to get the PostgreSQL AST
268269
const pgAst = parse(sql);
269270

270-
// Generate TypeScript AST builder code from the PostgreSQL AST
271+
// Generate TypeScript AST builder code from the PostgreSQL AST using runtime schema
271272
const tsAstBuilderCode = generateTsAstCodeFromPgAst(
272-
pgAst[0].RawStmt.stmt
273+
pgAst[0].RawStmt.stmt,
274+
runtimeSchema
273275
);
274276

275277
console.log(tsAstBuilderCode);
@@ -330,4 +332,4 @@ ast.selectStmt({
330332

331333
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
332334

333-
No developer or entity involved in creating 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 Software code or Software CLI, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
335+
No developer or entity involved in creating 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 Software code or Software CLI, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.

packages/proto-parser/__tests__/meta.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as t from '../test-utils/meta';
22
import { SelectStmt } from '@pgsql/types';
3-
import { generateTsAstCodeFromPgAst, generateTsAstCodeFromPgAstWithSchema } from '../src/utils'
3+
import { generateTsAstCodeFromPgAst } from '../src/utils'
44
import { runtimeSchema } from '../test-utils/meta/runtime-schema';
55
import generate from '@babel/generator';
66

@@ -24,7 +24,7 @@ it('AST to AST to create AST — meta 🤯', () => {
2424
op: 'SETOP_NONE'
2525
});
2626

27-
const astForAst = generateTsAstCodeFromPgAst(selectStmt);
27+
const astForAst = generateTsAstCodeFromPgAst(selectStmt, runtimeSchema);
2828
expect(generate(astForAst).code).toMatchSnapshot();
2929
});
3030

@@ -366,7 +366,7 @@ it('Complex AST — Advanced SQL with CTEs, Window Functions, Joins, and Subquer
366366
op: 'SETOP_NONE'
367367
});
368368

369-
const astForComplexAst = generateTsAstCodeFromPgAstWithSchema(complexSelectStmt, runtimeSchema);
369+
const astForComplexAst = generateTsAstCodeFromPgAst(complexSelectStmt, runtimeSchema);
370370
expect(generate(astForComplexAst).code).toMatchSnapshot();
371371
});
372372

@@ -390,7 +390,7 @@ it('Enhanced AST generation with runtime schema — wrapped vs unwrapped nodes',
390390
op: 'SETOP_NONE'
391391
});
392392

393-
const enhancedAst = generateTsAstCodeFromPgAstWithSchema(selectStmt, runtimeSchema);
393+
const enhancedAst = generateTsAstCodeFromPgAst(selectStmt, runtimeSchema);
394394
const generatedCode = generate(enhancedAst).code;
395395

396396
expect(generatedCode).toMatchSnapshot();
@@ -430,7 +430,7 @@ it('Complex AST with runtime schema — mixed wrapped/unwrapped patterns', () =>
430430
op: 'SETOP_NONE'
431431
});
432432

433-
const enhancedAst = generateTsAstCodeFromPgAstWithSchema(complexStmt, runtimeSchema);
433+
const enhancedAst = generateTsAstCodeFromPgAst(complexStmt, runtimeSchema);
434434
const generatedCode = generate(enhancedAst).code;
435435

436436
expect(generatedCode).toMatchSnapshot();

packages/proto-parser/src/utils/meta.ts

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,9 @@ import { NodeSpec, FieldSpec } from '../runtime-schema/types';
1313

1414
// TODO — handle TypeName and SPECIAL_TYPES cases
1515

16-
export function generateTsAstCodeFromPgAst(ast: any): any {
17-
function createAstNode(functionName: string, properties: any) {
18-
const args = properties.map(([propKey, propValue]: [string, any]) => {
19-
return t.objectProperty(t.identifier(propKey), getValueNode(propValue));
20-
});
21-
return t.callExpression(
22-
t.memberExpression(t.identifier('ast'), t.identifier(functionName)),
23-
[t.objectExpression(args)]
24-
);
25-
}
26-
27-
function getValueNode(value: any): t.Expression {
28-
if (typeof value === 'object') {
29-
return value === null ? t.nullLiteral() : traverse(value);
30-
}
31-
switch (typeof value) {
32-
case 'boolean':
33-
return t.booleanLiteral(value);
34-
case 'number':
35-
return t.numericLiteral(value);
36-
case 'string':
37-
return t.stringLiteral(value);
38-
default:
39-
return t.stringLiteral(String(value)); // Fallback for other types
40-
}
41-
}
42-
43-
function traverse(node: any): t.Expression {
44-
if (Array.isArray(node)) {
45-
return t.arrayExpression(node.map(traverse));
46-
} else if (node && typeof node === 'object') {
47-
const entries = Object.entries(node);
48-
if (entries.length === 0) return t.objectExpression([]);
49-
50-
const [key, value] = entries[0]; // Processing one key-value pair per object
51-
const functionName = toSpecialCamelCase(key);
52-
return createAstNode(functionName, Object.entries(value));
53-
}
54-
55-
return getValueNode(node);
56-
}
57-
58-
return traverse(ast);
59-
}
6016

61-
export function generateTsAstCodeFromPgAstWithSchema(ast: any, runtimeSchema: NodeSpec[]): any {
17+
18+
export function generateTsAstCodeFromPgAst(ast: any, runtimeSchema: NodeSpec[]): any {
6219
const schemaMap = new Map<string, NodeSpec>();
6320
runtimeSchema.forEach(spec => {
6421
schemaMap.set(spec.name, spec);

0 commit comments

Comments
 (0)