Skip to content

Commit

Permalink
Updating this plugin to the v2.0 api
Browse files Browse the repository at this point in the history
  • Loading branch information
ph committed May 2, 2016
1 parent 3edb9c2 commit 549d581
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 58 deletions.
10 changes: 7 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
---
sudo: false
language: ruby
cache: bundler
rvm:
- jruby-1.7.23
script:
- bundle exec rspec spec
- jruby-1.7.25
script:
- bundle exec rspec spec
jdk: oraclejdk8
before_install:
- git clone -b feature/event_interface https://github.com/elastic/logstash
6 changes: 5 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
source 'https://rubygems.org'
gemspec
gemspec
gem "logstash-core", :path => "./logstash/logstash-core"
gem "logstash-core-plugin-api", :path => "./logstash/logstash-core-plugin-api"
gem "logstash-core-event-java", :path => "./logstash/logstash-core-event-java"
gem "logstash-devutils", :github => "elastic/logstash-devutils", :branch => "feature/plugin-api-2_0"
12 changes: 4 additions & 8 deletions lib/logstash/filters/xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def filter(event)

@logger.debug? && @logger.debug("Running xml filter", :event => event)

value = event[@source]
value = event.get(@source)
return unless value

if value.is_a?(Array)
Expand Down Expand Up @@ -149,21 +149,17 @@ def filter(event)
# the array should probably be created once, filled in the loop and set at after the loop but the return
# statement above screws this strategy and is likely a bug anyway so I will not touch this until I can
# deep a big deeper and verify there is a sufficient test harness to refactor this.
data = event[xpath_dest] || []
data = event.get(xpath_dest) || []
data << value.to_s
event[xpath_dest] = data

# do not use the following construct to set the event, we cannot assume anymore that the field values are in-place mutable
# event[xpath_dest] ||= []
# event[xpath_dest] << value.to_s
event.set(xpath_dest, data)
end
end
end
end

if @store_xml
begin
event[@target] = XmlSimple.xml_in(value, "ForceArray" => @force_array)
event.set(@target, XmlSimple.xml_in(value, "ForceArray" => @force_array))
matched = true
rescue => e
event.tag(XMLPARSEFAILURE_TAG)
Expand Down
2 changes: 1 addition & 1 deletion logstash-filter-xml.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Gem::Specification.new do |s|
s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }

# Gem dependencies
s.add_runtime_dependency "logstash-core-plugin-api", "~> 1.0"
s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.0"
s.add_runtime_dependency 'nokogiri'
s.add_runtime_dependency 'xml-simple'

Expand Down
89 changes: 44 additions & 45 deletions spec/filters/xml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,55 @@
CONFIG

sample("raw" => '<foo key="value"/>') do
insist { subject["tags"] }.nil?
insist { subject["data"]} == {"key" => "value"}
insist { subject.get("tags") }.nil?
insist { subject.get("data")} == {"key" => "value"}
end

#From parse xml with array as a value
sample("raw" => '<foo><key>value1</key><key>value2</key></foo>') do
insist { subject["tags"] }.nil?
insist { subject["data"]} == {"key" => ["value1", "value2"]}
insist { subject.get("tags") }.nil?
insist { subject.get("data")} == {"key" => ["value1", "value2"]}
end

#From parse xml with hash as a value
sample("raw" => '<foo><key1><key2>value</key2></key1></foo>') do
insist { subject["tags"] }.nil?
insist { subject["data"]} == {"key1" => [{"key2" => ["value"]}]}
insist { subject.get("tags") }.nil?
insist { subject.get("data")} == {"key1" => [{"key2" => ["value"]}]}
end

# parse xml in single item array
sample("raw" => ["<foo bar=\"baz\"/>"]) do
insist { subject["tags"] }.nil?
insist { subject["data"]} == {"bar" => "baz"}
insist { subject.get("tags") }.nil?
insist { subject.get("data")} == {"bar" => "baz"}
end

# fail in multi items array
sample("raw" => ["<foo bar=\"baz\"/>", "jojoba"]) do
insist { subject["tags"] }.include?("_xmlparsefailure")
insist { subject["data"]} == nil
insist { subject.get("tags") }.include?("_xmlparsefailure")
insist { subject.get("data")} == nil
end

# fail in empty array
sample("raw" => []) do
insist { subject["tags"] }.include?("_xmlparsefailure")
insist { subject["data"]} == nil
insist { subject.get("tags") }.include?("_xmlparsefailure")
insist { subject.get("data")} == nil
end

# fail for non string field
sample("raw" => {"foo" => "bar"}) do
insist { subject["tags"] }.include?("_xmlparsefailure")
insist { subject["data"]} == nil
insist { subject.get("tags") }.include?("_xmlparsefailure")
insist { subject.get("data")} == nil
end

# fail for non string single item array
sample("raw" => [{"foo" => "bar"}]) do
insist { subject["tags"] }.include?("_xmlparsefailure")
insist { subject["data"]} == nil
insist { subject.get("tags") }.include?("_xmlparsefailure")
insist { subject.get("data")} == nil
end

#From bad xml
sample("raw" => '<foo /') do
insist { subject["tags"] }.include?("_xmlparsefailure")
insist { subject.get("tags") }.include?("_xmlparsefailure")
end
end

Expand All @@ -79,8 +79,8 @@
CONFIG

sample("raw" => '<foo key="value"/>') do
insist { subject["tags"] }.nil?
insist { subject["data"]} == nil
insist { subject.get("tags") }.nil?
insist { subject.get("data")} == nil
end
end

Expand All @@ -97,14 +97,14 @@

# Single value
sample("raw" => '<foo><key>value</key></foo>') do
insist { subject["tags"] }.nil?
insist { subject["xpath_field"]} == ["value"]
insist { subject.get("tags") }.nil?
insist { subject.get("xpath_field")} == ["value"]
end

#Multiple values
sample("raw" => '<foo><key>value1</key><key>value2</key></foo>') do
insist { subject["tags"] }.nil?
insist { subject["xpath_field"]} == ["value1","value2"]
insist { subject.get("tags") }.nil?
insist { subject.get("xpath_field")} == ["value1","value2"]
end
end

Expand All @@ -121,25 +121,25 @@
CONFIG

sample("xmldata" => '<foo key="value"/>') do
insist { subject["tags"] }.nil?
insist { subject["data"]} == {"key" => "value"}
insist { subject.get("tags") }.nil?
insist { subject.get("data") } == {"key" => "value"}
end

#From parse xml with array as a value
sample("xmldata" => '<foo><key>value1</key><key>value2</key></foo>') do
insist { subject["tags"] }.nil?
insist { subject["data"]} == {"key" => ["value1", "value2"]}
insist { subject.get("tags") }.nil?
insist { subject.get("data") } == {"key" => ["value1", "value2"]}
end

#From parse xml with hash as a value
sample("xmldata" => '<foo><key1><key2>value</key2></key1></foo>') do
insist { subject["tags"] }.nil?
insist { subject["data"]} == {"key1" => [{"key2" => ["value"]}]}
insist { subject.get("tags") }.nil?
insist { subject.get("data") } == {"key1" => [{"key2" => ["value"]}]}
end

#From bad xml
sample("xmldata" => '<foo /') do
insist { subject["tags"] }.include?("_xmlparsefailure")
insist { subject.get("tags") }.include?("_xmlparsefailure")
end
end

Expand All @@ -155,8 +155,8 @@
CONFIG

sample("xmldata" => '<foo key="value"/>') do
insist { subject["tags"] }.nil?
insist { subject["data"]} == nil
insist { subject.get("tags") }.nil?
insist { subject.get("data")} == nil
end
end

Expand All @@ -173,14 +173,14 @@

# Single value
sample("xmldata" => '<foo><key>value</key></foo>') do
insist { subject["tags"] }.nil?
insist { subject["xpath_field"]} == ["value"]
insist { subject.get("tags") }.nil?
insist { subject.get("xpath_field") } == ["value"]
end

#Multiple values
sample("xmldata" => '<foo><key>value1</key><key>value2</key></foo>') do
insist { subject["tags"] }.nil?
insist { subject["xpath_field"]} == ["value1","value2"]
insist { subject.get("tags") }.nil?
insist { subject.get("xpath_field") } == ["value1","value2"]
end
end

Expand All @@ -197,8 +197,8 @@

# Single value
sample("xmldata" => '<foo><key>Français</key></foo>') do
insist { subject["tags"] }.nil?
insist { subject["xpath_field"]} == ["Français"]
insist { subject.get("tags") }.nil?
insist { subject.get("xpath_field")} == ["Français"]
end
end

Expand All @@ -215,7 +215,7 @@

# Single value
sample("xmldata" => '<foo xmlns:h="http://www.w3.org/TR/html4/"><h:div>Content</h:div></foo>') do
insist { subject["xpath_field"] } == ["<h:div>Content</h:div>"]
insist { subject.get("xpath_field") } == ["<h:div>Content</h:div>"]
end
end

Expand All @@ -233,7 +233,7 @@

# Single value
sample("xmldata" => '<foo xmlns:h="http://www.w3.org/TR/html4/"><h:div>Content</h:div></foo>') do
insist { subject["xpath_field"] } == ["<h:div>Content</h:div>"]
insist { subject.get("xpath_field") } == ["<h:div>Content</h:div>"]
end
end

Expand All @@ -251,7 +251,7 @@

# Single value
sample("xmldata" => '<foo><h:div xmlns:h="http://www.w3.org/TR/html4/">Content</h:div></foo>') do
insist { subject["xpath_field"] } == ["<h:div xmlns:h=\"http://www.w3.org/TR/html4/\">Content</h:div>"]
insist { subject.get("xpath_field") } == ["<h:div xmlns:h=\"http://www.w3.org/TR/html4/\">Content</h:div>"]
end
end

Expand All @@ -268,7 +268,7 @@

# Single value
sample("xmldata" => '<foo xmlns:h="http://www.w3.org/TR/html4/"><h:div>Content</h:div></foo>') do
insist { subject["xpath_field"] } == ["<div>Content</div>"]
insist { subject.get("xpath_field") } == ["<div>Content</div>"]
end
end

Expand All @@ -285,7 +285,7 @@

# Single value
sample("xmldata" => '<foo><bar>Content</bar></foo>') do
insist { subject["parseddata"] } == { "bar" => ["Content"] }
insist { subject.get("parseddata") } == { "bar" => ["Content"] }
end
end

Expand All @@ -302,8 +302,7 @@

# Single value
sample("xmldata" => '<foo><bar>Content</bar></foo>') do
insist { subject["parseddata"] } == { "bar" => "Content" }
insist { subject.get("parseddata") } == { "bar" => "Content" }
end
end

end

0 comments on commit 549d581

Please sign in to comment.