Skip to content

Conversation

@richardwooding
Copy link
Contributor

@richardwooding richardwooding commented Oct 22, 2025

Summary

This PR completes the migration to schema-based JSON/JSONB field detection by removing all hardcoded field name lists. This work builds on #60 and fully resolves issues #61, #59, and #20.

Changes

Schema Enhancement (Issue #61)

  • Extended pg.FieldSchema with IsJSONB and ElementType fields
  • Updated LoadTableSchema() to populate these fields from PostgreSQL database schema
  • Added schema lookup helper methods:
    • isFieldJSONB(tableName, fieldName) - detect JSONB vs JSON type
    • isFieldArray(tableName, fieldName) - detect array columns
    • getFieldElementType(tableName, fieldName) - get array element type

Removed Hardcoded Field Lists (Issue #61)

Removed all hardcoded field name lists from detection functions in json.go:

  • shouldUseJSONPath() - removed jsonFields list
  • hasJSONFieldInChain() - now uses schema-based detection
  • isJSONArrayField() - removed jsonArrayFields and arrayFieldNames maps
  • isJSONBField() - removed jsonbFields and jsonbParentFields lists
  • getJSONArrayFunction() - removed simpleArrayFields and complexArrayFields lists

API Refactoring - Functional Options Pattern

Refactored the API to use functional options for better extensibility:

  • Added ConvertOption type and WithSchemas() option function
  • Convert() now accepts variadic options instead of separate ConvertWithSchemas() function
  • More flexible and follows Go best practices

Test Updates

  • Updated all tests to use new API: Convert(ast, cel2sql.WithSchemas(schemas))
  • Updated examples to demonstrate new API
  • Disabled test cases that require knowledge of internal JSON structure
    (PostgreSQL's information_schema doesn't provide information about JSON internals)

Breaking Change

Users must now provide schema information via the WithSchemas() option for JSON field detection to work. The Convert() function without schemas will only work for non-JSON fields.

Before:

sql, err := cel2sql.Convert(ast)

After:

schemas := provider.GetSchemas() // or map[string]pg.Schema{...}
sql, err := cel2sql.Convert(ast, cel2sql.WithSchemas(schemas))

Test Plan

  • ✅ All existing tests pass
  • ✅ Lint passes with 0 issues
  • ✅ Coverage maintained at 89.4% for pg package
  • ✅ Integration tests with real PostgreSQL validate correct SQL generation

Related Issues

Closes #61
Closes #59
Closes #20

🤖 Generated with Claude Code

…tion (#61)

This commit removes all hardcoded field name lists from JSON detection
functions and makes them rely entirely on schema information provided via
ConvertWithSchemas().

Changes:
- Extended pg.FieldSchema with IsJSONB and ElementType fields
- Updated LoadTableSchema() to populate these fields from database
- Added schema lookup methods: isFieldJSONB(), isFieldArray(), getFieldElementType()
- Removed hardcoded field lists from:
  - shouldUseJSONPath() - removed jsonFields list
  - hasJSONFieldInChain() - now uses schema-based detection
  - isJSONArrayField() - removed jsonArrayFields and arrayFieldNames maps
  - isJSONBField() - removed jsonbFields and jsonbParentFields lists
  - getJSONArrayFunction() - removed simpleArrayFields and complexArrayFields lists
- Updated all tests to use ConvertWithSchemas()
- Disabled test cases requiring knowledge of internal JSON structure
  (PostgreSQL's information_schema doesn't provide this)

Backward compatibility: Users must now provide schema information via
ConvertWithSchemas() for JSON field detection to work properly.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@richardwooding richardwooding force-pushed the fix/json-path-operators-issue-59-20 branch from 8e1b018 to fe80164 Compare October 22, 2025 10:56
Replace ConvertWithSchemas() with a more flexible API using functional options.
The Convert() function now accepts variadic options, making it more extensible
for future enhancements.

Changes:
- Add ConvertOption type and WithSchemas() option function
- Refactor Convert() to accept variadic options
- Remove ConvertWithSchemas() function
- Update all test files to use new API: Convert(ast, cel2sql.WithSchemas(schemas))
- Update examples/load_table_schema to demonstrate new API

This API design follows Go best practices and allows for easier addition
of new options in the future without breaking changes.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@richardwooding richardwooding merged commit 730fb45 into main Oct 22, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant