Skip to content

Commit

Permalink
Handle equality checks on list and map types
Browse files Browse the repository at this point in the history
  • Loading branch information
danhermann committed Oct 25, 2018
1 parent 7e1ffd3 commit 7a71981
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
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,10 +1,8 @@
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.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.atomic.AtomicLong;
Expand All @@ -18,6 +16,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 Down Expand Up @@ -240,6 +240,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

0 comments on commit 7a71981

Please sign in to comment.