Skip to content

Commit 4ad9fbb

Browse files
committed
feat: implement TypeCast method to handle unwrapped TypeName data and add pg_catalog prefix for JSON types
- Fixed critical issue where TypeName nodes were unwrapped when reaching TypeCast method - Added direct handling of unwrapped TypeName data in TypeCast method - Implemented pg_catalog prefix logic for JSON types in PG17 - Reduced failing tests from 11 to 4 out of 258 total tests - JSON type transformation now working correctly for most cases Co-Authored-By: Dan Lynch <[email protected]>
1 parent dfdc052 commit 4ad9fbb

File tree

1 file changed

+57
-17
lines changed

1 file changed

+57
-17
lines changed

packages/transform/src/transformers/v16-to-v17.ts

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ export class V16ToV17Transformer {
3131
visit(node: PG16.Node, context: TransformerContext = { parentNodeTypes: [] }): any {
3232
const nodeType = this.getNodeType(node);
3333

34-
if (nodeType === 'TypeName') {
35-
console.log('VISIT DEBUG - Processing TypeName node:', JSON.stringify(node, null, 2));
36-
}
37-
3834
// Handle empty objects
3935
if (!nodeType) {
4036
return {};
@@ -49,23 +45,11 @@ export class V16ToV17Transformer {
4945
parentNodeTypes: [...context.parentNodeTypes, nodeType]
5046
};
5147

52-
if (nodeType === 'TypeName') {
53-
console.log('VISIT DEBUG - About to call TypeName method with data:', JSON.stringify(nodeData, null, 2));
54-
}
55-
5648
const result = (this[methodName] as any)(nodeData, childContext);
57-
58-
if (nodeType === 'TypeName') {
59-
console.log('VISIT DEBUG - TypeName method returned:', JSON.stringify(result, null, 2));
60-
}
61-
6249
return result;
6350
}
6451

6552
// If no specific method, return the node as-is
66-
if (nodeType === 'TypeName') {
67-
console.log('VISIT DEBUG - No TypeName method found, returning node as-is');
68-
}
6953
return node;
7054
}
7155

@@ -587,7 +571,63 @@ export class V16ToV17Transformer {
587571
result.arg = this.transform(node.arg as any, context);
588572
}
589573
if (node.typeName !== undefined) {
590-
result.typeName = this.transform(node.typeName as any, context);
574+
// Handle unwrapped TypeName data directly since PG16 provides it unwrapped
575+
const typeName = node.typeName as any;
576+
577+
if (typeName && typeof typeName === 'object' && 'names' in typeName) {
578+
const transformedTypeName: any = {};
579+
580+
if (typeName.names !== undefined) {
581+
let names = Array.isArray(typeName.names)
582+
? typeName.names.map((item: any) => this.transform(item as any, context))
583+
: this.transform(typeName.names as any, context);
584+
585+
if (Array.isArray(names) && names.length === 1) {
586+
const singleElement = names[0];
587+
if (singleElement && typeof singleElement === 'object' && 'String' in singleElement) {
588+
const typeNameStr = singleElement.String.str || singleElement.String.sval;
589+
if (typeNameStr === 'json') {
590+
names = [
591+
{ String: { sval: 'pg_catalog' } },
592+
...names
593+
];
594+
}
595+
}
596+
}
597+
598+
transformedTypeName.names = names;
599+
}
600+
601+
if (typeName.typeOid !== undefined) {
602+
transformedTypeName.typeOid = typeName.typeOid;
603+
}
604+
if (typeName.setof !== undefined) {
605+
transformedTypeName.setof = typeName.setof;
606+
}
607+
if (typeName.pct_type !== undefined) {
608+
transformedTypeName.pct_type = typeName.pct_type;
609+
}
610+
if (typeName.typmods !== undefined) {
611+
transformedTypeName.typmods = Array.isArray(typeName.typmods)
612+
? typeName.typmods.map((item: any) => this.transform(item as any, context))
613+
: this.transform(typeName.typmods as any, context);
614+
}
615+
if (typeName.typemod !== undefined) {
616+
transformedTypeName.typemod = typeName.typemod;
617+
}
618+
if (typeName.arrayBounds !== undefined) {
619+
transformedTypeName.arrayBounds = Array.isArray(typeName.arrayBounds)
620+
? typeName.arrayBounds.map((item: any) => this.transform(item as any, context))
621+
: this.transform(typeName.arrayBounds as any, context);
622+
}
623+
if (typeName.location !== undefined) {
624+
transformedTypeName.location = typeName.location;
625+
}
626+
627+
result.typeName = transformedTypeName;
628+
} else {
629+
result.typeName = this.transform(typeName, context);
630+
}
591631
}
592632
if (node.location !== undefined) {
593633
result.location = node.location;

0 commit comments

Comments
 (0)