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

Handle equality checks on list and map types #10074

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ private static EventCondition rubyFieldEquals(final Comparable<IRubyObject> left
final String field) {
final FieldReference reference = FieldReference.from(field);
return event ->
left.equals((IRubyObject) event.getEvent().getUnconvertedField(reference));
left.equals(Rubyfier.deep(RubyUtil.RUBY, event.getEvent().getUnconvertedField(reference)));
}

private static EventCondition constant(final boolean value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
package org.logstash.config.ir;

import com.google.common.base.Strings;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.jruby.RubyInteger;
Expand All @@ -18,6 +9,8 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.logstash.ConvertedList;
import org.logstash.ConvertedMap;
import org.logstash.Event;
import org.logstash.RubyUtil;
import org.logstash.common.IncompleteSourceWithMetadataException;
Expand All @@ -26,6 +19,17 @@
import org.logstash.config.ir.compiler.RubyIntegration;
import org.logstash.ext.JrubyEventExtLibrary;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
* Tests for {@link CompiledPipeline}.
*/
Expand Down Expand Up @@ -240,6 +244,37 @@ public void correctlyCompilesGreaterOrEqualThan() throws Exception {
assertCorrectFieldToFieldComparison(gte, 7, 8, false);
}

@Test
public void equalityCheckOnCompositeField() throws Exception {
final PipelineIR pipelineIR = ConfigCompiler.configToPipelineIR(
"input {mockinput{}} filter { if 4 == [list] { mockaddfilter {} } if 5 == [map] { mockaddfilter {} } } output {mockoutput{} }",
false
);
final Collection<String> s = new ArrayList<>();
s.add("foo");
final Map<String, Object> m = new HashMap<>();
m.put("foo", "bar");
final JrubyEventExtLibrary.RubyEvent testEvent =
JrubyEventExtLibrary.RubyEvent.newRubyEvent(RubyUtil.RUBY, new Event());
testEvent.getEvent().setField("list", ConvertedList.newFromList(s));
testEvent.getEvent().setField("map", ConvertedMap.newFromMap(m));

final Map<String, Supplier<IRubyObject>> filters = new HashMap<>();
filters.put("mockaddfilter", () -> ADD_FIELD_FILTER);
new CompiledPipeline(
pipelineIR,
new CompiledPipelineTest.MockPluginFactory(
Collections.singletonMap("mockinput", () -> null),
filters,
Collections.singletonMap("mockoutput", mockOutputSupplier())
)
).buildExecution().compute(RubyUtil.RUBY.newArray(testEvent), false, false);
final Collection<JrubyEventExtLibrary.RubyEvent> outputEvents = EVENT_SINKS.get(runId);
MatcherAssert.assertThat(outputEvents.size(), CoreMatchers.is(1));
MatcherAssert.assertThat(outputEvents.contains(testEvent), CoreMatchers.is(true));
MatcherAssert.assertThat(testEvent.getEvent().getField("foo"), CoreMatchers.nullValue());
}

@Test
public void conditionalWithNullField() throws Exception {
final PipelineIR pipelineIR = ConfigCompiler.configToPipelineIR(
Expand Down