Skip to content

Commit

Permalink
[Gem] Fixes ES|QL Helper
Browse files Browse the repository at this point in the history
When passing in a parser, if the value was nil, the helper would
error. Now it returns nil for nil values.
  • Loading branch information
picandocodigo committed Apr 18, 2024
1 parent bfe571d commit 2a43b6a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion elasticsearch/lib/elasticsearch/helpers/esql_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def self.query(client, query, params = {}, parser: {})
response['values'].map do |value|
(value.length - 1).downto(0).map do |index|
key = columns[index]['name']
value[index] = parser[key].call value[index] if parser[key]
value[index] = parser[key].call(value[index]) if value[index] && parser[key]
{ key => value[index] }
end.reduce({}, :merge)
end
Expand Down
28 changes: 26 additions & 2 deletions elasticsearch/spec/integration/helpers/esql_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# under the License.
require_relative 'helpers_spec_helper'
require 'elasticsearch/helpers/esql_helper'
require 'ipaddr'

context 'Elasticsearch client helpers' do
let(:index) { 'esql_helper_test' }
Expand Down Expand Up @@ -76,8 +77,6 @@
end

it 'parses iterated objects when procs are passed in' do
require 'ipaddr'

parser = {
'@timestamp' => Proc.new { |t| DateTime.parse(t) },
'client.ip' => Proc.new { |i| IPAddr.new(i) },
Expand All @@ -91,4 +90,29 @@
expect(r['event.duration']).to be_a String
end
end

it 'parser does not error when value is nil, leaves nil' do
client.index(
index: index,
body: {
'@timestamp' => nil,
'client.ip' => nil,
message: 'Connected to 10.1.0.1',
'event.duration' => 1756465
},
refresh: true
)
parser = {
'@timestamp' => Proc.new { |t| DateTime.parse(t) },
'client.ip' => Proc.new { |i| IPAddr.new(i) },
'event.duration' => Proc.new { |d| d.to_s }
}
response = esql_helper.query(client, query, parser: parser)
response.each do |r|
expect [DateTime, NilClass].include?(r['@timestamp'].class)
expect [IPAddr, NilClass].include?(r['client.ip'].class)
expect(r['message']).to be_a String
expect(r['event.duration']).to be_a String
end
end
end

0 comments on commit 2a43b6a

Please sign in to comment.