Skip to content

Commit

Permalink
Add support for multiline conditionals with the else if statements
Browse files Browse the repository at this point in the history
This is a followup of the issues elastic#2850 and elastic#3281.

The following configuration:

    if [condition] {

    } else if [condition1]
      or [condition2] {
        ..
    }

Was compiled to ruby like this:
    elsif condition or condition2 # else if [condition1]
      or [condition2]

and making the intepreter fails.

Fixes elastic#3386
  • Loading branch information
ph authored and jordansissel committed Jun 5, 2015
1 parent 43b13e0 commit 37d1033
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
10 changes: 7 additions & 3 deletions lib/logstash/config/config_ast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ def self.defered_conditionals_index=(val)
@defered_conditionals_index = val
end

class Node < Treetop::Runtime::SyntaxNode; end
class Node < Treetop::Runtime::SyntaxNode
def text_value_for_comments
text_value.gsub(/[\r\n]/, " ")
end
end

class Config < Node
def compile
Expand Down Expand Up @@ -412,14 +416,14 @@ class BranchEntry < Node; end
class If < BranchEntry
def compile
children = recursive_inject { |e| e.is_a?(Branch) || e.is_a?(Plugin) }
return "if #{condition.compile} # if #{condition.text_value.gsub(/[\r\n]/, " ")}\n" \
return "if #{condition.compile} # if #{condition.text_value_for_comments}\n" \
<< children.collect(&:compile).map { |s| s.split("\n", -1).map { |l| " " + l }.join("\n") }.join("") << "\n"
end
end
class Elsif < BranchEntry
def compile
children = recursive_inject { |e| e.is_a?(Branch) || e.is_a?(Plugin) }
return "elsif #{condition.compile} # else if #{condition.text_value}\n" \
return "elsif #{condition.compile} # else if #{condition.text_value_for_comments}\n" \
<< children.collect(&:compile).map { |s| s.split("\n", -1).map { |l| " " + l }.join("\n") }.join("") << "\n"
end
end
Expand Down
22 changes: 21 additions & 1 deletion spec/core/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
end

context "#compile" do
context "with multiline conditionals" do
context "if with multiline conditionals" do
let(:config) { <<-CONFIG }
filter {
if [something]
Expand All @@ -66,6 +66,26 @@
end
end

context "elsif with multiline conditionals" do
let(:config) { <<-CONFIG }
filter {
if [notathing] {
} else if [something]
or [anotherthing]
or [onemorething] {
}
}
CONFIG
subject { LogStashConfigParser.new }

it "should compile successfully" do
result = subject.parse(config)
expect(result).not_to(be_nil)
expect { eval(result.compile) }.not_to(raise_error)
end
end


context "invalid configuration" do
it "rejects duplicate hash key" do
parser = LogStashConfigParser.new
Expand Down

0 comments on commit 37d1033

Please sign in to comment.