-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Event now expects property values in properties map (#1)
* Utilize PropertyValue, so we can get both numbers and strings from into event properties * Add tests for deserializing behaviour
- Loading branch information
Showing
17 changed files
with
241 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
ruby 3.3.5 | ||
ruby 3.3.4 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
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()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use serde_json::json; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_deserialize_with_numbers() { | ||
let json = json!({ | ||
"code": "testing", | ||
"timestamp": 123124123123i64, | ||
"properties": { | ||
"text": "text_value", | ||
"number": 300 | ||
} | ||
}); | ||
|
||
let event = serde_json::from_value::<Event>(json).expect("expected json to parse"); | ||
|
||
let code = "testing".to_owned(); | ||
let timestamp = 123124123123; | ||
let mut properties = HashMap::default(); | ||
properties.insert("number".to_owned(), PropertyValue::Number(300.into())); | ||
properties.insert( | ||
"text".to_owned(), | ||
PropertyValue::String("text_value".into()), | ||
); | ||
|
||
assert_eq!( | ||
event, | ||
Event { | ||
code, | ||
timestamp, | ||
properties | ||
} | ||
) | ||
} | ||
|
||
#[test] | ||
fn test_deserialize_with_floating_point() { | ||
let json = r#"{ | ||
"code": "testing", | ||
"timestamp": 123124123123, | ||
"properties": { | ||
"text": "text_value", | ||
"number": 3.2 | ||
} | ||
}"#; | ||
|
||
let event = serde_json::from_str::<Event>(json).expect("expected json to parse"); | ||
|
||
let code = "testing".to_owned(); | ||
let timestamp = 123124123123; | ||
let mut properties = HashMap::default(); | ||
properties.insert( | ||
"number".to_owned(), | ||
PropertyValue::Number("3.2".parse().unwrap()), | ||
); | ||
properties.insert( | ||
"text".to_owned(), | ||
PropertyValue::String("text_value".into()), | ||
); | ||
|
||
assert_eq!( | ||
event, | ||
Event { | ||
code, | ||
timestamp, | ||
properties | ||
} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,26 @@ | ||
package main | ||
|
||
// #cgo LDFLAGS: -L../target/debug -lexpression_go | ||
// #cgo LDFLAGS: -L../target/release -lexpression_go | ||
// #include <stdio.h> | ||
// #include <stdlib.h> | ||
// #include "bindings.h" | ||
import "C" | ||
import "unsafe" | ||
|
||
func main() { | ||
cs := C.CString("event.timestamp+event.properties.a") | ||
event := C.CString("{\"code\":\"123\",\"timestamp\":2,\"properties\":{\"a\": \"123\"}}") | ||
expr := C.parse(cs) | ||
cs := C.CString("concat(event.properties.a, 'test')") | ||
event := C.CString("{\"code\":\"13\",\"timestamp\":2,\"properties\":{\"a\": 123.12}}") | ||
|
||
result := C.GoString(C.evaluate(expr, event)) | ||
println(result) | ||
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)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,43 @@ | ||
use std::ffi::{c_char, CStr, CString}; | ||
use std::{ | ||
ffi::{c_char, CStr, CString}, | ||
ptr::null_mut, | ||
}; | ||
|
||
use expression_core::{Event, Expression, ExpressionParser}; | ||
use expression_core::ExpressionParser; | ||
|
||
#[no_mangle] | ||
/// # Safety | ||
/// Pass in a valid string | ||
pub unsafe extern "C" fn parse(input: *const c_char) -> *mut Expression { | ||
/// Pass in a valid strings | ||
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() }; | ||
let expression = ExpressionParser::parse_expression(&input).unwrap(); | ||
Box::into_raw(Box::new(expression)) | ||
|
||
// 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(); | ||
}; | ||
|
||
let Ok(temp) = CString::new(res.to_string()) else { | ||
return null_mut(); | ||
}; | ||
temp.into_raw() | ||
} | ||
|
||
#[no_mangle] | ||
/// # Safety | ||
/// Pass in a valid string | ||
pub unsafe extern "C" fn evaluate(expr: *mut Expression, event: *const c_char) -> *const c_char { | ||
let json = unsafe { CStr::from_ptr(event).to_str().unwrap() }; | ||
let event: Event = serde_json::from_str(json).unwrap(); | ||
|
||
let expr = unsafe { expr.as_ref().unwrap() }; | ||
match expr.evaluate(&event).unwrap() { | ||
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() | ||
} | ||
} | ||
/// Only pass in pointers to strings that have been obtained through `evaluate` | ||
pub unsafe extern "C" fn free_evaluate(ptr: *mut c_char) { | ||
unsafe { drop(CString::from_raw(ptr)) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.