Skip to content

Commit

Permalink
support truthy eval of constants
Browse files Browse the repository at this point in the history
  • Loading branch information
danhermann committed Aug 9, 2019
1 parent c60d5e3 commit 25744d1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ private static EventCondition truthy(final Truthy truthy) {
final Expression inner = truthy.getExpression();
if (inner instanceof EventValueExpression) {
condition = truthy((EventValueExpression) inner);
} else if (inner instanceof ValueExpression) {
condition = constant(valueIsTruthy(((ValueExpression) inner).get()));
} else {
throw new EventCondition.Compiler.UnexpectedTypeException(inner);
}
Expand Down Expand Up @@ -455,6 +457,15 @@ private static EventCondition constant(final boolean value) {
return value ? event -> true : event -> false;
}

private static boolean valueIsTruthy(Object object) {
if (object == null) {
return false;
}
final String other = object.toString();
return other != null && !other.isEmpty() &&
!Boolean.toString(false).equals(other);
}

private static final class FieldMatches implements EventCondition {

private final FieldReference field;
Expand Down Expand Up @@ -611,12 +622,7 @@ private FieldTruthy(final FieldReference field) {
@Override
public boolean fulfilled(final JrubyEventExtLibrary.RubyEvent event) {
final Object object = event.getEvent().getUnconvertedField(field);
if (object == null) {
return false;
}
final String other = object.toString();
return other != null && !other.isEmpty() &&
!Boolean.toString(false).equals(other);
return valueIsTruthy(object);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.logstash.config.ir;

import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.jruby.RubyArray;
import org.junit.After;
import org.junit.Before;
Expand All @@ -22,6 +20,8 @@

import static org.logstash.config.ir.CompiledPipelineTest.IDENTITY_FILTER;
import static org.logstash.ext.JrubyEventExtLibrary.RubyEvent;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public final class EventConditionTest extends RubyEnvTestCase {

Expand Down Expand Up @@ -113,10 +113,50 @@ public void testInclusionWithFieldInField() throws Exception {
).buildExecution().compute(inputBatch, false, false);
final RubyEvent[] outputEvents = EVENT_SINKS.get(runId).toArray(new RubyEvent[0]);

MatcherAssert.assertThat(outputEvents.length, CoreMatchers.is(3));
MatcherAssert.assertThat(outputEvents[0], CoreMatchers.is(leftIsString1));
MatcherAssert.assertThat(outputEvents[1], CoreMatchers.is(rightIsList1));
MatcherAssert.assertThat(outputEvents[2], CoreMatchers.is(nonStringValue1));
assertThat(outputEvents.length, is(3));
assertThat(outputEvents[0], is(leftIsString1));
assertThat(outputEvents[1], is(rightIsList1));
assertThat(outputEvents[2], is(nonStringValue1));
}

@Test
public void testConditionWithConstantValue() throws Exception {
testConditionWithConstantValue("\"[abc]\"", 1);
}

@Test
public void testConditionWithConstantFalseLiteralValue() throws Exception {
testConditionWithConstantValue("\"false\"", 0);
}

@Test
public void testConditionWithConstantEmptyStringValue() throws Exception {
testConditionWithConstantValue("\"\"", 0);
}

private void testConditionWithConstantValue(String condition, int expectedMatches) throws Exception {
final PipelineIR pipelineIR = ConfigCompiler.configToPipelineIR(
"input {mockinput{}} filter { " +
"mockfilter {} } " +
"output { " +
" if " + condition + " { " +
" mockoutput{}" +
" } }",
false
);

new CompiledPipeline(
pipelineIR,
new CompiledPipelineTest.MockPluginFactory(
Collections.singletonMap("mockinput", () -> null),
Collections.singletonMap("mockfilter", () -> IDENTITY_FILTER),
Collections.singletonMap("mockoutput", mockOutputSupplier())
))
.buildExecution()
.compute(RubyUtil.RUBY.newArray(RubyEvent.newRubyEvent(RubyUtil.RUBY)), false, false);

final Collection<RubyEvent> outputEvents = EVENT_SINKS.get(runId);
assertThat(outputEvents.size(), is(expectedMatches));
}

private Supplier<Consumer<Collection<RubyEvent>>> mockOutputSupplier() {
Expand Down

0 comments on commit 25744d1

Please sign in to comment.