From 862cea2e7ced0268b6b5ec1fa5bcb30d3e3f02f6 Mon Sep 17 00:00:00 2001 From: Toon Willems Date: Tue, 12 Nov 2024 11:33:47 +0100 Subject: [PATCH] Finalize go version --- expression-go/{main.go => expression.go} | 25 ++++++++++++------------ expression-go/go.mod | 2 +- expression-go/src/lib.rs | 2 -- 3 files changed, 14 insertions(+), 15 deletions(-) rename expression-go/{main.go => expression.go} (52%) diff --git a/expression-go/main.go b/expression-go/expression.go similarity index 52% rename from expression-go/main.go rename to expression-go/expression.go index 688e38c..15d0a61 100644 --- a/expression-go/main.go +++ b/expression-go/expression.go @@ -1,26 +1,27 @@ -package main +package expression -// #cgo LDFLAGS: -L../target/release -lexpression_go +// #cgo LDFLAGS: -lexpression_go // #include // #include // #include "bindings.h" import "C" import "unsafe" -func main() { - cs := C.CString("concat(event.properties.a, 'test')") - event := C.CString("{\"code\":\"13\",\"timestamp\":2,\"properties\":{\"a\": 123.12}}") +func Evaluate(expression string, event_json string) *string { + cs := C.CString(expression) + event := C.CString(event_json) + // Evaluate the expression ptr := C.evaluate(cs, event) - if ptr != nil { - - result := C.GoString(ptr) - println(result) - - C.free_evaluate(ptr) - } C.free(unsafe.Pointer(cs)) C.free(unsafe.Pointer(event)) + if ptr != nil { + result := C.GoString(ptr) + C.free_evaluate(ptr) + return &result + } else { + return nil + } } diff --git a/expression-go/go.mod b/expression-go/go.mod index de1d0a3..08b9943 100644 --- a/expression-go/go.mod +++ b/expression-go/go.mod @@ -1,3 +1,3 @@ -module expressiongo +module github.com/getlago/lago-expression/expression-go go 1.23.2 diff --git a/expression-go/src/lib.rs b/expression-go/src/lib.rs index f10e279..54fdf6e 100644 --- a/expression-go/src/lib.rs +++ b/expression-go/src/lib.rs @@ -18,8 +18,6 @@ pub unsafe extern "C" fn evaluate(input: *const c_char, event: *const c_char) -> let json = unsafe { CStr::from_ptr(event).to_str().unwrap() }; - // TODO: solve the fact that json will contain numbers, deserialize will fail as it's expecting only - // strings. in the Event::properties which is a HashMap let Ok(event) = serde_json::from_str(json) else { return null_mut(); };