Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be a bit more defensive and don't throw errors for unexpected values. #6

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
nudded marked this conversation as resolved.
Show resolved Hide resolved
};
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
Loading