From fdd25200f8038d750c3315e61bbb443f1dcde46a Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Tue, 14 Jan 2025 10:47:36 -0800 Subject: [PATCH] wip: use cel2db to CEL -> SQL conversion a proof of concept of cel2db, written in Rust, being consumed by our example service. In the future, this pattern will be productionalized via some simple way to consume the native library in a native go repository. But for now, this proves the viability. --- .gitignore | 2 ++ BUILD.bazel | 0 DEVELOPMENT.md | 15 ++++++++++++++- example/service/cel.go | 16 ++++++++++++++++ example/service/cel_test.go | 5 +++++ 5 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 BUILD.bazel diff --git a/.gitignore b/.gitignore index 6a07036..ef96c38 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ main terraform-provider-bookstore +# libcel2db should be compiled and added. +example/service/libcel2db.a \ No newline at end of file diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 0000000..e69de29 diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 923db5e..f3b043a 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -27,4 +27,17 @@ go install github.com/aep-dev/api-linter/cmd/api-linter@latest The standard GoLang toolchain is used, with the addition of protobuf for compiling the resource definition. 1. `./scripts/regenerate-all.sh` -2. `go build main.go` \ No newline at end of file +2. `go build main.go` + +## Importing cel2db + +Today, the CEL <-> SQL translation in the example service requireds [cel2db](https://github.com/aep-dev/cel2db), written in Rust and therefore not compatible with the standard `go get` approach to managing dependencies. + +This will be streamlined in the future, but for now, do the following (bazel and rust build tooling is required): + +```bash +git clone git@github.com:aep-dev/cel2db.git +cd ./cel2db +bazel build //cel2db:cel2db_static +cp bazel-bin/cel2db/libcel2db.a ${AEPC_REPO}/example/service/ +``` \ No newline at end of file diff --git a/example/service/cel.go b/example/service/cel.go index 14e6a40..8a4977d 100644 --- a/example/service/cel.go +++ b/example/service/cel.go @@ -1,11 +1,27 @@ package service +// required for cel2db. + +// extern char *cel_to_sql(const char *cel_expr, const char *sql_dialect); +// #cgo LDFLAGS: -L ${SRCDIR} -lcel2db +import "C" + import ( + "errors" + "github.com/aep-dev/aepc/pkg/cel2ansisql" "github.com/google/cel-go/cel" ) func convertCELToSQL(expr string) (string, error) { + condition := C.cel_to_sql(C.CString(expr), C.CString("sqlite")) + if condition == nil { + return "", errors.New("failed to convert CEL to SQL") + } + return C.GoString(condition), nil +} + +func convertCELToSQL2(expr string) (string, error) { if expr == "" { return "", nil } diff --git a/example/service/cel_test.go b/example/service/cel_test.go index e19f63a..f65eb11 100644 --- a/example/service/cel_test.go +++ b/example/service/cel_test.go @@ -15,6 +15,11 @@ func TestCELToSQL(t *testing.T) { input: "description.startsWith('tomorrow')", expected: "description LIKE CONCAT('tomorrow', '%')", }, + { + name: "arithemtic", + input: "1 + 2", + expected: "1 + 2", + }, { name: "empty", input: "",