Skip to content

Commit

Permalink
don't crash on non-number / string objects
Browse files Browse the repository at this point in the history
  • Loading branch information
nudded committed Dec 4, 2024
1 parent 72d919a commit 02733a3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
5 changes: 1 addition & 4 deletions expression-ruby/ext/lago_expression/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ impl EventWrapper {
} 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(),
));
PropertyValue::String(value.to_string())
};
properties.insert(key, property_value);
Ok(ForEach::Continue)
Expand Down
25 changes: 24 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,14 @@


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

class Dummy < Struct.new(:v)
def to_s
v
end
end

let(:event) { Lago::Event.new("code", 1234, {"property_1" => 1.23, "dummy" => Dummy.new(1), "decimal_property" => BigDecimal("2.3"), "property_2" => "test", "property_3" => "12.34"}) }

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

context "with a decimal expression with a decimal value from the event" do
let(:expression) { Lago::ExpressionParser.parse("event.properties.property_1 + event.properties.decimal_property") }

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

context "with a non-number / string value in the event payload" do
let(:expression) { Lago::ExpressionParser.parse("event.properties.dummy") }

it "returns the calculated value" do
expect(expression.evaluate(event)).not_to be(nil)
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 02733a3

Please sign in to comment.