Skip to content

Commit e2290fc

Browse files
committed
[Gem] Fixes ES|QL Helper
When passing in a parser, if the value was nil, the helper would error. Now it returns nil for nil values.
1 parent b422bf6 commit e2290fc

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

elasticsearch/lib/elasticsearch/helpers/esql_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def self.query(client, query, params = {}, parser: {})
6767
response['values'].map do |value|
6868
(value.length - 1).downto(0).map do |index|
6969
key = columns[index]['name']
70-
value[index] = parser[key].call value[index] if parser[key]
70+
value[index] = parser[key].call(value[index]) if value[index] && parser[key]
7171
{ key => value[index] }
7272
end.reduce({}, :merge)
7373
end

elasticsearch/spec/integration/helpers/esql_helper_spec.rb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# under the License.
1717
require_relative 'helpers_spec_helper'
1818
require 'elasticsearch/helpers/esql_helper'
19+
require 'ipaddr'
1920

2021
context 'Elasticsearch client helpers' do
2122
let(:index) { 'esql_helper_test' }
@@ -76,8 +77,6 @@
7677
end
7778

7879
it 'parses iterated objects when procs are passed in' do
79-
require 'ipaddr'
80-
8180
parser = {
8281
'@timestamp' => Proc.new { |t| DateTime.parse(t) },
8382
'client.ip' => Proc.new { |i| IPAddr.new(i) },
@@ -91,4 +90,29 @@
9190
expect(r['event.duration']).to be_a String
9291
end
9392
end
93+
94+
it 'parser does not error when value is nil, leaves nil' do
95+
client.index(
96+
index: index,
97+
body: {
98+
'@timestamp' => nil,
99+
'client.ip' => nil,
100+
message: 'Connected to 10.1.0.1',
101+
'event.duration' => 1756465
102+
},
103+
refresh: true
104+
)
105+
parser = {
106+
'@timestamp' => Proc.new { |t| DateTime.parse(t) },
107+
'client.ip' => Proc.new { |i| IPAddr.new(i) },
108+
'event.duration' => Proc.new { |d| d.to_s }
109+
}
110+
response = esql_helper.query(client, query, parser: parser)
111+
response.each do |r|
112+
expect [DateTime, NilClass].include?(r['@timestamp'].class)
113+
expect [IPAddr, NilClass].include?(r['client.ip'].class)
114+
expect(r['message']).to be_a String
115+
expect(r['event.duration']).to be_a String
116+
end
117+
end
94118
end

0 commit comments

Comments
 (0)