Skip to content

Commit

Permalink
Utilize PropertyValue, so we can get both numbers and strings from in…
Browse files Browse the repository at this point in the history
…to event properties
  • Loading branch information
nudded committed Nov 5, 2024
1 parent 16557d1 commit 751165b
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 56 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ jobs:
- name: Set up Ruby 3.3
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.3'
- name: install dependencies
run: bundle install
ruby-version: '3.3.4'
bundler-cache: true
- name: build
run: bundle exec rake compile
- name: Run tests
Expand Down
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby 3.3.5
ruby 3.3.4
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion expression-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
bigdecimal = "0.4.5"
bigdecimal = { version = "0.4.6", features = ["serde-json"] }
lazy_static = "1.5.0"
pest = "2.7.13"
pest_derive = "2.7.13"
Expand Down
28 changes: 14 additions & 14 deletions expression-core/src/evaluate.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
use std::{collections::HashMap, fmt::Display};
use std::fmt::Display;

use bigdecimal::{BigDecimal, RoundingMode, ToPrimitive};
use serde::Deserialize;
use thiserror::Error;

use crate::parser::{EventAttribute, Expression, Function, Operation};
use crate::{
parser::{EventAttribute, Expression, Function, Operation},
Event, PropertyValue,
};

#[derive(Debug, PartialEq)]
pub enum ExpressionValue {
Number(BigDecimal),
String(String),
}

#[derive(Debug, Default, PartialEq, Deserialize)]
pub struct Event {
pub code: String,
pub timestamp: u64,
pub properties: HashMap<String, String>,
}

impl Expression {
pub fn evaluate(&self, event: &Event) -> EvaluationResult<ExpressionValue> {
let evaluated_expr = match self {
Expand Down Expand Up @@ -141,10 +136,15 @@ impl EventAttribute {
.get(name)
.ok_or(ExpressionError::MissingVariable(name.clone()))?;

if let Ok(decimal_value) = value.parse() {
ExpressionValue::Number(decimal_value)
} else {
ExpressionValue::String(value.clone())
match value {
PropertyValue::String(s) => {
if let Ok(decimal_value) = s.parse() {
ExpressionValue::Number(decimal_value)
} else {
ExpressionValue::String(s.clone())
}
}
PropertyValue::Number(d) => ExpressionValue::Number(d.to_owned()),
}
}
};
Expand Down
35 changes: 35 additions & 0 deletions expression-core/src/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::collections::HashMap;

use bigdecimal::BigDecimal;
use serde::Deserialize;

#[derive(Debug, Default, PartialEq, Deserialize)]
pub struct Event {
pub code: String,
pub timestamp: u64,
pub properties: HashMap<String, PropertyValue>,
}

#[derive(Debug, PartialEq, Deserialize)]
#[serde(untagged)]
pub enum PropertyValue {
String(String),
Number(BigDecimal),
}

impl From<&str> for PropertyValue {
fn from(value: &str) -> Self {
PropertyValue::String(value.into())
}
}
impl From<String> for PropertyValue {
fn from(value: String) -> Self {
PropertyValue::String(value)
}
}

impl From<u64> for PropertyValue {
fn from(value: u64) -> Self {
PropertyValue::Number(value.into())
}
}
4 changes: 3 additions & 1 deletion expression-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pub use evaluate::{EvaluationResult, Event, ExpressionValue};
pub use evaluate::{EvaluationResult, ExpressionValue};
pub use event::{Event, PropertyValue};
pub use parser::{Expression, ExpressionParser, ParseError};
pub use pest::Parser;

mod evaluate;
mod event;
mod parser;
14 changes: 9 additions & 5 deletions expression-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import "C"
import "unsafe"

func main() {
cs := C.CString("event.timestamp+event.properties.a")
event := C.CString("{\"code\":\"123\",\"timestamp\":2,\"properties\":{\"a\": \"123\"}}")
cs := C.CString("concat(event.properties.a, 'test')")
event := C.CString("{\"code\":\"13\",\"timestamp\":2,\"properties\":{\"a\": 123.12}}")

ptr := C.evaluate(cs, event)
result := C.GoString(ptr)
println(result)
if ptr != nil {

result := C.GoString(ptr)
println(result)

C.free_evaluate(ptr)
}

C.free_evaluate(ptr)
C.free(unsafe.Pointer(cs))
C.free(unsafe.Pointer(event))

Expand Down
18 changes: 8 additions & 10 deletions expression-go/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,28 @@ use expression_core::ExpressionParser;
pub unsafe extern "C" fn evaluate(input: *const c_char, event: *const c_char) -> *mut c_char {
let input = unsafe { CStr::from_ptr(input).to_str().unwrap().to_owned() };

// Cannot parse expression -> return null
let Ok(expr) = ExpressionParser::parse_expression(&input) else {
return null_mut();
};

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<String, String>
let Ok(event) = serde_json::from_str(json) else {
return null_mut();
};

// evaluate expression, errors are not returned, but we do catch them and return null
let Ok(res) = expr.evaluate(&event) else {
return null_mut();
};

match res {
expression_core::ExpressionValue::Number(d) => {
let temp = CString::new(d.to_string()).unwrap();
temp.into_raw()
}
expression_core::ExpressionValue::String(d) => {
let temp = CString::new(d).unwrap();
temp.into_raw()
}
}
let Ok(temp) = CString::new(res.to_string()) else {
return null_mut();
};
temp.into_raw()
}

#[no_mangle]
Expand Down
4 changes: 2 additions & 2 deletions expression-js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ elem.oninput = function () {
error.innerHTML = "";
try {
expression = parseExpression(elem.value);
output.innerHTML = evaluateExpression(expression, "code", 1231254123, {
started_at: 123124123,
output.innerHTML = evaluateExpression(expression, "code", BigInt(1231254123123125), {
started_at: 1231254123123125,
ended_at: 1241231241,
replicas: 8,
});
Expand Down
23 changes: 13 additions & 10 deletions expression-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use std::collections::HashMap;
use js_sys::Reflect;
use wasm_bindgen::prelude::*;

use bigdecimal::ToPrimitive;
use bigdecimal::{BigDecimal, FromPrimitive, ToPrimitive};

use expression_core::{ExpressionParser, ExpressionValue};
use web_sys::console;
use expression_core::{ExpressionParser, ExpressionValue, PropertyValue};
extern crate console_error_panic_hook;

#[wasm_bindgen(start)]
Expand All @@ -29,7 +28,7 @@ pub fn parse_expression(expression: String) -> Result<Expression, String> {
pub fn evaluate_expression(
expression: Expression,
code: String,
timestamp: u32,
timestamp: u64,
js_properties: &JsValue,
) -> Result<JsValue, JsValue> {
let mut properties = HashMap::new();
Expand All @@ -38,21 +37,25 @@ pub fn evaluate_expression(

for key in keys {
let value = Reflect::get(js_properties, &key)?;
console::log_1(&value);

let insert = if value.is_string() {
String::try_from(value)?
let property_value = if value.is_string() {
String::try_from(value)?.into()
} else if value.is_bigint() {
let n = u64::try_from(value)?;
PropertyValue::Number(n.into())
} else {
let n = f64::try_from(value)?;
format!("{n}")
PropertyValue::Number(
BigDecimal::from_f64(n).ok_or("failed to convert property value")?,
)
};

properties.insert(key.as_string().ok_or("expected string")?, insert);
properties.insert(key.as_string().ok_or("expected string")?, property_value);
}

let event = expression_core::Event {
code,
timestamp: timestamp.into(),
timestamp,
properties,
};

Expand Down
2 changes: 1 addition & 1 deletion expression-ruby/Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
source "https://rubygems.org"

ruby "3.3.5"
ruby "3.3.4"

gemspec
2 changes: 1 addition & 1 deletion expression-ruby/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ DEPENDENCIES
rspec (~> 3)

RUBY VERSION
ruby 3.3.5p100
ruby 3.3.4p94

BUNDLED WITH
2.5.11
38 changes: 32 additions & 6 deletions expression-ruby/ext/lago_expression/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::collections::HashMap;

use expression_core::{Event, Expression, ExpressionParser, ExpressionValue};
use magnus::{error, function, method, value::ReprValue, Error, IntoValue, Module, Object, Ruby};
use expression_core::{Event, Expression, ExpressionParser, ExpressionValue, PropertyValue};
use magnus::{
error, function, method, r_hash::ForEach, value::ReprValue, Error, IntoValue, Module, Object,
RHash, Ruby, TryConvert, Value,
};

#[magnus::wrap(class = "Lago::Expression", free_immediately, size)]
struct ExpressionWrapper(Expression);
Expand All @@ -10,12 +13,35 @@ struct ExpressionWrapper(Expression);
struct EventWrapper(Event);

impl EventWrapper {
fn new(code: String, timestamp: u64, map: HashMap<String, String>) -> EventWrapper {
Self(Event {
fn new(ruby: &Ruby, code: String, timestamp: u64, map: RHash) -> error::Result<EventWrapper> {
let mut properties = HashMap::default();

map.foreach(|key: String, value: Value| {
let property_value = if value.is_kind_of(ruby.class_numeric()) {
// Convert ruby numbers to a formatted string, that can be parsed into a BigDecimal
let ruby_string = value.to_r_string()?;
let big_d = ruby_string
.to_string()?
.parse()
.expect("Failed to parse a number as bigdecimal");
PropertyValue::Number(big_d)
} else if value.is_kind_of(ruby.class_string()) {
PropertyValue::String(String::try_convert(value)?)
} else {
return Err(magnus::Error::new(
ruby.exception_runtime_error(),
"Expected string or number".to_owned(),
));
};
properties.insert(key, property_value);
Ok(ForEach::Continue)
})?;

Ok(Self(Event {
code,
timestamp,
properties: map,
})
properties,
}))
}
}

Expand Down
10 changes: 9 additions & 1 deletion expression-ruby/spec/lago-expression/expression_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


RSpec.describe Lago::Expression do
let(:event) { Lago::Event.new("code", 1234, {"property_1" => "1.23", "property_2" => "test", "property_3" => "12.34"}) }
let(:event) { Lago::Event.new("code", 1234, {"property_1" => 1.23, "property_2" => "test", "property_3" => "12.34"}) }

describe '#evaluate' do
context "with a simple math expression" do
Expand Down Expand Up @@ -39,6 +39,14 @@
end
end

context "with a string expression with a decimal value from the event" do
let(:expression) { Lago::ExpressionParser.parse("CONCAT(event.properties.property_1, 'test')") }

it "returns the calculated value" do
expect(expression.evaluate(event)).to eq("1.23test")
end
end

context "with a concat function" do
let(:expression) { Lago::ExpressionParser.parse("concat(event.properties.property_2, '-', 'suffix')") }

Expand Down

0 comments on commit 751165b

Please sign in to comment.