Commit 53965fe
This commit fixes two critical issues with JSON path operator generation:
**Issue #59: Incorrect JSON Path Operators**
- Generated `obj->>'metadata'->>'user_name'` (applies ->> to table name)
- Should be `obj.metadata->>'user_name'` (table.column then JSON operators)
- Fixed by recognizing direct table.column access and only applying JSON
operators to fields within the JSONB data
**Issue #20: Hardcoded JSON Field Names**
- Replaced hardcoded JSON field lists with schema-based type detection
- Added `IsJSON` field to `pg.FieldSchema` to track JSON/JSONB columns
- Created `ConvertWithSchemas()` function to pass schema information
- Falls back to hardcoded lists for backward compatibility when schemas
not provided
**Changes:**
- Added `IsJSON bool` field to `pg.FieldSchema` in `pg/provider.go`
- Added `GetSchemas()` method to `TypeProvider` interface
- Created `ConvertWithSchemas(ast, schemas)` function in `cel2sql.go`
- Added `isFieldJSON()` and `getTableAndFieldFromSelectChain()` helper
methods to converter
- Updated `shouldUseJSONPath()` to use schema information first, then
fall back to hardcoded list
- Fixed `buildJSONPathInternal()` to properly handle direct table.column
access without applying JSON operators to the table name
- Updated all JSON-related tests to use `ConvertWithSchemas()` and set
`IsJSON: true`
- Updated test expectations to match correct PostgreSQL syntax
**Testing:**
- Added `json_operator_validation_test.go` - validates correct operator
usage with real PostgreSQL
- Added `json_integration_validation_test.go` - verifies generated SQL
executes correctly
- All existing tests pass with updated expectations
- Validated against PostgreSQL 17 using testcontainers
**Correct behavior:**
- `obj.metadata.user_name` → `obj.metadata->>'user_name'` ✅
- `obj.metadata.settings.theme` → `obj.metadata->'settings'->>'theme'` ✅
- Uses `->` for intermediate JSON fields (keeps JSON type)
- Uses `->>` for final JSON field (extracts as text)
Fixes #59
Addresses #20
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude <noreply@anthropic.com>
1 parent c265ffd commit 53965fe
File tree
7 files changed
+464
-75
lines changed- pg
7 files changed
+464
-75
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
21 | 29 | | |
22 | 30 | | |
23 | 31 | | |
24 | 32 | | |
25 | 33 | | |
26 | 34 | | |
| 35 | + | |
27 | 36 | | |
28 | 37 | | |
29 | 38 | | |
| |||
34 | 43 | | |
35 | 44 | | |
36 | 45 | | |
| 46 | + | |
37 | 47 | | |
38 | 48 | | |
39 | 49 | | |
| |||
57 | 67 | | |
58 | 68 | | |
59 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
60 | 110 | | |
61 | 111 | | |
62 | 112 | | |
| |||
377 | 427 | | |
378 | 428 | | |
379 | 429 | | |
380 | | - | |
| 430 | + | |
381 | 431 | | |
382 | 432 | | |
383 | 433 | | |
384 | | - | |
| 434 | + | |
385 | 435 | | |
386 | 436 | | |
387 | 437 | | |
| |||
393 | 443 | | |
394 | 444 | | |
395 | 445 | | |
396 | | - | |
| 446 | + | |
397 | 447 | | |
398 | 448 | | |
399 | 449 | | |
400 | | - | |
| 450 | + | |
401 | 451 | | |
402 | 452 | | |
403 | 453 | | |
404 | 454 | | |
405 | | - | |
| 455 | + | |
406 | 456 | | |
407 | | - | |
| 457 | + | |
408 | 458 | | |
409 | 459 | | |
410 | 460 | | |
| |||
427 | 477 | | |
428 | 478 | | |
429 | 479 | | |
430 | | - | |
| 480 | + | |
431 | 481 | | |
432 | 482 | | |
433 | 483 | | |
| |||
1442 | 1492 | | |
1443 | 1493 | | |
1444 | 1494 | | |
1445 | | - | |
| 1495 | + | |
1446 | 1496 | | |
1447 | | - | |
| 1497 | + | |
1448 | 1498 | | |
1449 | 1499 | | |
1450 | 1500 | | |
1451 | | - | |
| 1501 | + | |
1452 | 1502 | | |
1453 | 1503 | | |
1454 | 1504 | | |
1455 | | - | |
| 1505 | + | |
1456 | 1506 | | |
1457 | 1507 | | |
1458 | | - | |
| 1508 | + | |
1459 | 1509 | | |
1460 | 1510 | | |
1461 | | - | |
| 1511 | + | |
1462 | 1512 | | |
1463 | 1513 | | |
1464 | | - | |
| 1514 | + | |
1465 | 1515 | | |
1466 | 1516 | | |
1467 | | - | |
| 1517 | + | |
1468 | 1518 | | |
1469 | 1519 | | |
1470 | | - | |
| 1520 | + | |
1471 | 1521 | | |
1472 | 1522 | | |
1473 | | - | |
| 1523 | + | |
1474 | 1524 | | |
1475 | 1525 | | |
1476 | 1526 | | |
1477 | 1527 | | |
1478 | 1528 | | |
1479 | 1529 | | |
1480 | 1530 | | |
1481 | | - | |
| 1531 | + | |
1482 | 1532 | | |
1483 | 1533 | | |
1484 | | - | |
| 1534 | + | |
1485 | 1535 | | |
1486 | 1536 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
0 commit comments