This example demonstrates the basic usage of cel2sql to convert CEL (Common Expression Language) expressions to PostgreSQL SQL conditions.
- Schema Definition: How to define a PostgreSQL table schema using
pg.Schema - CEL Environment Setup: Creating a CEL environment with PostgreSQL type provider
- Expression Compilation: Compiling CEL expressions with type checking
- SQL Conversion: Converting compiled CEL expressions to PostgreSQL SQL
go run main.goCEL Expression: employee.name == "John Doe" && employee.age >= 25 && employee.active
PostgreSQL SQL: employee.name = 'John Doe' AND employee.age >= 25 AND employee.active
employeeSchema := pg.Schema{
{Name: "name", Type: "text", Repeated: false},
{Name: "age", Type: "integer", Repeated: false},
{Name: "department", Type: "text", Repeated: false},
{Name: "hired_at", Type: "timestamp with time zone", Repeated: false},
{Name: "active", Type: "boolean", Repeated: false},
}env, err := cel.NewEnv(
cel.CustomTypeProvider(pg.NewTypeProvider(map[string]pg.Schema{
"Employee": employeeSchema,
})),
cel.Variable("employee", cel.ObjectType("Employee")),
)ast, issues := env.Compile(`employee.name == "John Doe" && employee.age >= 25 && employee.active`)
sqlCondition, err := cel2sql.Convert(ast)text- String valuesinteger- Integer numbersboolean- Boolean valuestimestamp with time zone- Timestamps- Arrays (with
Repeated: true) - And many more...
- Try modifying the CEL expression to test different operators
- Add more fields to the schema
- Experiment with array fields by setting
Repeated: true - Check out the LoadTableSchema example for dynamic schema loading