logstash-filter-mutate 3.2.0 → 3.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e8196cb41a7cc83ef6ff48ab4dc9be6f130a00048cfc16821ca93f62a00955fe
4
- data.tar.gz: 7fccc907cd8ef3fc7c8c8f0b3ae91b7e61ebd7ff949a9265d3e35c0d33d92674
3
+ metadata.gz: e5423b0b68566ee643d458b6fb38c40a2b0fd336d9e2f23dd8921431adb1e7af
4
+ data.tar.gz: 470f2d561ea225dad08e4848146001fea9ba1eaba0f13ba652f00037703dd70e
5
5
  SHA512:
6
- metadata.gz: 4ff52c37f5bc4525e97f73eaa605d646aae1a7474e0627c334ac0d3bfdc4d33736d201a3aaae0b4d0d6e7bf8ff1db3f08f3e1e8835719e5c33f15008c7746896
7
- data.tar.gz: 7b2d637b7902aec46732838b327c307963da5ae279c1ff9ce91b5303adbf2a6411b75adc0944aec421226bd2c5e9efe2c0e059e79405c4e0d12d5b42e1a3dcd8
6
+ metadata.gz: 43b35a6bbe4a59e4999eec61169812bf917e17b09e41d158078b09fc476965c37c0d75c05eff5fc21238631c1bd56743d9ddf31395d70905090e7f648273d27e
7
+ data.tar.gz: f9e28cf3e4c431f8769106b9e856f9b85cb5218d5e713116354fc357947e561a2e0aebf9b054ff03a71204fb2b830e91705858dd43f2ff20de7704d2452a26ed
@@ -1,6 +1,12 @@
1
+ ## 3.3.1
2
+ - Fix: Number strings using a **decimal comma** (e.g. 1,23), added convert support to specify integer_eu and float_eu.
3
+
4
+ ## 3.3.0
5
+ - feature: Added capitalize feature.
6
+
1
7
  ## 3.2.0
2
8
  - Support boolean to integer conversion #107
3
-
9
+
4
10
  ## 3.1.7
5
11
  - Update gemspec summary
6
12
 
@@ -24,6 +24,7 @@ Contributors:
24
24
  * Suyog Rao (suyograo)
25
25
  * Tal Levy (talevy)
26
26
  * piavlo
27
+ * Abdul Haseeb Hussain (AbdulHaseebHussain)
27
28
 
28
29
  Note: If you've sent us patches, bug reports, or otherwise contributed to
29
30
  Logstash, and you aren't on the list above and want to be, please let us know
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012–2016 Elasticsearch <http://www.elastic.co>
1
+ Copyright (c) 2012-2018 Elasticsearch <http://www.elastic.co>
2
2
 
3
3
  Licensed under the Apache License, Version 2.0 (the "License");
4
4
  you may not use this file except in compliance with the License.
@@ -37,12 +37,14 @@ This plugin supports the following configuration options plus the <<plugins-{typ
37
37
  | <<plugins-{type}s-{plugin}-join>> |<<hash,hash>>|No
38
38
  | <<plugins-{type}s-{plugin}-lowercase>> |<<array,array>>|No
39
39
  | <<plugins-{type}s-{plugin}-merge>> |<<hash,hash>>|No
40
+ | <<plugins-{type}s-{plugin}-coerce>> |<<hash,hash>>|No
40
41
  | <<plugins-{type}s-{plugin}-rename>> |<<hash,hash>>|No
41
42
  | <<plugins-{type}s-{plugin}-replace>> |<<hash,hash>>|No
42
43
  | <<plugins-{type}s-{plugin}-split>> |<<hash,hash>>|No
43
44
  | <<plugins-{type}s-{plugin}-strip>> |<<array,array>>|No
44
45
  | <<plugins-{type}s-{plugin}-update>> |<<hash,hash>>|No
45
46
  | <<plugins-{type}s-{plugin}-uppercase>> |<<array,array>>|No
47
+ | <<plugins-{type}s-{plugin}-capitalize>> |<<array,array>>|No
46
48
  |=======================================================================
47
49
 
48
50
  Also see <<plugins-{type}s-{plugin}-common-options>> for a list of options supported by all
@@ -51,7 +53,7 @@ filter plugins.
51
53
  &nbsp;
52
54
 
53
55
  [id="plugins-{type}s-{plugin}-convert"]
54
- ===== `convert`
56
+ ===== `convert`
55
57
 
56
58
  * Value type is <<hash,hash>>
57
59
  * There is no default value for this setting.
@@ -72,7 +74,13 @@ If the conversion type is `integer` and the value is a boolean, it will be conve
72
74
  * **True:** `1`
73
75
  * **False:** `0`
74
76
 
75
- Valid conversion targets are: integer, float, string, and boolean.
77
+ If you have numeric strings that have decimal commas (Europe and ex-colonies)
78
+ e.g. "1.234,56" or "2.340", by using conversion targets of integer_eu or float_eu
79
+ the convert function will treat "." as a group separator and "," as a decimal separator.
80
+
81
+ Conversion targets of integer or float will now correctly handle "," as a group separator.
82
+
83
+ Valid conversion targets are: integer, float, integer_eu, float_eu, string, and boolean.
76
84
 
77
85
  Example:
78
86
  [source,ruby]
@@ -83,7 +91,7 @@ Example:
83
91
  }
84
92
 
85
93
  [id="plugins-{type}s-{plugin}-copy"]
86
- ===== `copy`
94
+ ===== `copy`
87
95
 
88
96
  * Value type is <<hash,hash>>
89
97
  * There is no default value for this setting.
@@ -99,7 +107,7 @@ Example:
99
107
  }
100
108
 
101
109
  [id="plugins-{type}s-{plugin}-gsub"]
102
- ===== `gsub`
110
+ ===== `gsub`
103
111
 
104
112
  * Value type is <<array,array>>
105
113
  * There is no default value for this setting.
@@ -129,7 +137,7 @@ Example:
129
137
 
130
138
 
131
139
  [id="plugins-{type}s-{plugin}-join"]
132
- ===== `join`
140
+ ===== `join`
133
141
 
134
142
  * Value type is <<hash,hash>>
135
143
  * There is no default value for this setting.
@@ -145,7 +153,7 @@ Example:
145
153
  }
146
154
 
147
155
  [id="plugins-{type}s-{plugin}-lowercase"]
148
- ===== `lowercase`
156
+ ===== `lowercase`
149
157
 
150
158
  * Value type is <<array,array>>
151
159
  * There is no default value for this setting.
@@ -161,7 +169,7 @@ Example:
161
169
  }
162
170
 
163
171
  [id="plugins-{type}s-{plugin}-merge"]
164
- ===== `merge`
172
+ ===== `merge`
165
173
 
166
174
  * Value type is <<hash,hash>>
167
175
  * There is no default value for this setting.
@@ -181,8 +189,25 @@ Example:
181
189
  }
182
190
  }
183
191
 
192
+ [id="plugins-{type}s-{plugin}-coerce"]
193
+ ===== `coerce`
194
+
195
+ * Value type is <<hash,hash>>
196
+ * There is no default value for this setting.
197
+
198
+ Set the default value of a field that exists but is null
199
+
200
+ Example:
201
+ [source,ruby]
202
+ filter {
203
+ mutate {
204
+ # Sets the default value of the 'field1' field to 'default_value'
205
+ coerce => { "field1" => "default_value" }
206
+ }
207
+ }
208
+
184
209
  [id="plugins-{type}s-{plugin}-rename"]
185
- ===== `rename`
210
+ ===== `rename`
186
211
 
187
212
  * Value type is <<hash,hash>>
188
213
  * There is no default value for this setting.
@@ -199,7 +224,7 @@ Example:
199
224
  }
200
225
 
201
226
  [id="plugins-{type}s-{plugin}-replace"]
202
- ===== `replace`
227
+ ===== `replace`
203
228
 
204
229
  * Value type is <<hash,hash>>
205
230
  * There is no default value for this setting.
@@ -216,7 +241,7 @@ Example:
216
241
  }
217
242
 
218
243
  [id="plugins-{type}s-{plugin}-split"]
219
- ===== `split`
244
+ ===== `split`
220
245
 
221
246
  * Value type is <<hash,hash>>
222
247
  * There is no default value for this setting.
@@ -233,7 +258,7 @@ Example:
233
258
  }
234
259
 
235
260
  [id="plugins-{type}s-{plugin}-strip"]
236
- ===== `strip`
261
+ ===== `strip`
237
262
 
238
263
  * Value type is <<array,array>>
239
264
  * There is no default value for this setting.
@@ -249,7 +274,7 @@ Example:
249
274
  }
250
275
 
251
276
  [id="plugins-{type}s-{plugin}-update"]
252
- ===== `update`
277
+ ===== `update`
253
278
 
254
279
  * Value type is <<hash,hash>>
255
280
  * There is no default value for this setting.
@@ -266,7 +291,7 @@ Example:
266
291
  }
267
292
 
268
293
  [id="plugins-{type}s-{plugin}-uppercase"]
269
- ===== `uppercase`
294
+ ===== `uppercase`
270
295
 
271
296
  * Value type is <<array,array>>
272
297
  * There is no default value for this setting.
@@ -281,7 +306,21 @@ Example:
281
306
  }
282
307
  }
283
308
 
309
+ [id="plugins-{type}s-{plugin}-capitalize"]
310
+ ===== `capitalize`
311
+
312
+ * Value type is <<array,array>>
313
+ * There is no default value for this setting.
314
+
315
+ Convert a string to its capitalized equivalent.
284
316
 
317
+ Example:
318
+ [source,ruby]
319
+ filter {
320
+ mutate {
321
+ capitalize => [ "fieldname" ]
322
+ }
323
+ }
285
324
 
286
325
  [id="plugins-{type}s-{plugin}-common-options"]
287
326
  include::{include_path}/{type}.asciidoc[]
@@ -7,6 +7,18 @@ require "logstash/namespace"
7
7
  class LogStash::Filters::Mutate < LogStash::Filters::Base
8
8
  config_name "mutate"
9
9
 
10
+ # Sets a default value when the field exists but the value is null.
11
+ #
12
+ # Example:
13
+ # [source,ruby]
14
+ # filter {
15
+ # mutate {
16
+ # # Sets the default value of the 'field1' field to 'default_value'
17
+ # coerce => { "field1" => "default_value" }
18
+ # }
19
+ # }
20
+ config :coerce, :validate => :hash
21
+
10
22
  # Rename one or more fields.
11
23
  #
12
24
  # Example:
@@ -59,7 +71,13 @@ class LogStash::Filters::Mutate < LogStash::Filters::Base
59
71
  # * **True:** `1`
60
72
  # * **False:** `0`
61
73
  #
62
- # Valid conversion targets are: integer, float, string, and boolean.
74
+ # If you have numeric strings that have decimal commas (Europe and ex-colonies)
75
+ # e.g. "1.234,56" or "2.340", by using conversion targets of integer_eu or float_eu
76
+ # the convert function will treat "." as a group separator and "," as a decimal separator.
77
+ #
78
+ # Conversion targets of integer or float will now correctly handle "," as a group separator.
79
+ #
80
+ # Valid conversion targets are: integer, float, integer_eu, float_eu, string, and boolean.
63
81
  #
64
82
  # Example:
65
83
  # [source,ruby]
@@ -117,6 +135,17 @@ class LogStash::Filters::Mutate < LogStash::Filters::Base
117
135
  # }
118
136
  config :lowercase, :validate => :array
119
137
 
138
+ # Convert a string to its capitalized equivalent.
139
+ #
140
+ # Example:
141
+ # [source,ruby]
142
+ # filter {
143
+ # mutate {
144
+ # capitalize => [ "fieldname" ]
145
+ # }
146
+ # }
147
+ config :capitalize, :validate => :array
148
+
120
149
  # Split a field to an array using a separator character. Only works on string
121
150
  # fields.
122
151
  #
@@ -183,7 +212,7 @@ class LogStash::Filters::Mutate < LogStash::Filters::Base
183
212
  CONVERT_PREFIX = "convert_".freeze
184
213
 
185
214
  def register
186
- valid_conversions = %w(string integer float boolean)
215
+ valid_conversions = %w(string integer float boolean integer_eu float_eu )
187
216
  # TODO(sissel): Validate conversion requests if provided.
188
217
  @convert.nil? or @convert.each do |field, type|
189
218
  if !valid_conversions.include?(type)
@@ -216,12 +245,14 @@ class LogStash::Filters::Mutate < LogStash::Filters::Base
216
245
  end
217
246
 
218
247
  def filter(event)
248
+ coerce(event) if @coerce
219
249
  rename(event) if @rename
220
250
  update(event) if @update
221
251
  replace(event) if @replace
222
252
  convert(event) if @convert
223
253
  gsub(event) if @gsub
224
254
  uppercase(event) if @uppercase
255
+ capitalize(event) if @capitalize
225
256
  lowercase(event) if @lowercase
226
257
  strip(event) if @strip
227
258
  remove(event) if @remove
@@ -235,6 +266,13 @@ class LogStash::Filters::Mutate < LogStash::Filters::Base
235
266
 
236
267
  private
237
268
 
269
+ def coerce(event)
270
+ @coerce.each do |field, default_value|
271
+ next unless event.include?(field) && event.get(field)==nil
272
+ event.set(field, event.sprintf(default_value))
273
+ end
274
+ end
275
+
238
276
  def rename(event)
239
277
  @rename.each do |old, new|
240
278
  old = event.sprintf(old)
@@ -268,7 +306,7 @@ class LogStash::Filters::Mutate < LogStash::Filters::Base
268
306
  when Hash
269
307
  @logger.debug? && @logger.debug("I don't know how to type convert a hash, skipping", :field => field, :value => original)
270
308
  when Array
271
- event.set(field, original.map { |v| converter.call(v) })
309
+ event.set(field, original.map { |v| v.nil? ? v : converter.call(v) })
272
310
  when NilClass
273
311
  # ignore
274
312
  else
@@ -288,21 +326,37 @@ class LogStash::Filters::Mutate < LogStash::Filters::Base
288
326
  value.to_s.force_encoding(Encoding::UTF_8)
289
327
  end
290
328
 
329
+ def convert_boolean(value)
330
+ return true if value =~ TRUE_REGEX
331
+ return false if value.empty? || value =~ FALSE_REGEX
332
+ @logger.warn("Failed to convert #{value} into boolean.")
333
+ value
334
+ end
335
+
291
336
  def convert_integer(value)
292
337
  return 1 if value == true
293
338
  return 0 if value == false
294
- value.to_i
339
+ return value.to_i if !value.is_a?(String)
340
+ value.tr(",", "").to_i
295
341
  end
296
342
 
297
343
  def convert_float(value)
298
- value.to_f
344
+ value.tr(",", "").to_f
299
345
  end
300
346
 
301
- def convert_boolean(value)
302
- return true if value =~ TRUE_REGEX
303
- return false if value.empty? || value =~ FALSE_REGEX
304
- @logger.warn("Failed to convert #{value} into boolean.")
305
- value
347
+ def convert_integer_eu(value)
348
+ return 1 if value == true
349
+ return 0 if value == false
350
+ cnv_replace_eu(value).to_i
351
+ end
352
+
353
+ def convert_float_eu(value)
354
+ cnv_replace_eu(value).to_f
355
+ end
356
+
357
+ def cnv_replace_eu(value)
358
+ return value if !value.is_a?(String)
359
+ value.tr(".", "").tr(",", ".")
306
360
  end
307
361
 
308
362
  def gsub(event)
@@ -385,6 +439,26 @@ class LogStash::Filters::Mutate < LogStash::Filters::Base
385
439
  end
386
440
  end
387
441
 
442
+ def capitalize(event)
443
+ #see comments for #uppercase
444
+ @capitalize.each do |field|
445
+ original = event.get(field)
446
+ next if original.nil?
447
+ result = case original
448
+ when Array
449
+ original.map! do |elem|
450
+ (elem.is_a?(String) ? elem.capitalize : elem)
451
+ end
452
+ when String
453
+ original.capitalize! || original
454
+ else
455
+ @logger.debug? && @logger.debug("Can't capitalize something that isn't a string", :field => field, :value => original)
456
+ original
457
+ end
458
+ event.set(field, result)
459
+ end
460
+ end
461
+
388
462
  def split(event)
389
463
  @split.each do |field, separator|
390
464
  value = event.get(field)
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
 
3
3
  s.name = 'logstash-filter-mutate'
4
- s.version = '3.2.0'
4
+ s.version = '3.3.1'
5
5
  s.licenses = ['Apache License (2.0)']
6
6
  s.summary = "Performs mutations on fields"
7
7
  s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"
@@ -54,6 +54,24 @@ describe LogStash::Filters::Mutate do
54
54
  end
55
55
  end
56
56
 
57
+ context "when doing capitalize of an array" do
58
+
59
+ let(:config) do
60
+ { "capitalize" => ["array_of"] }
61
+ end
62
+
63
+ let(:attrs) { { "array_of" => ["ab", 2, "CDE"] } }
64
+
65
+ it "should capitalize not raise an error" do
66
+ expect { subject.filter(event) }.not_to raise_error
67
+ end
68
+
69
+ it "should convert only string elements" do
70
+ subject.filter(event)
71
+ expect(event.get("array_of")).to eq(["Ab", 2, "Cde"])
72
+ end
73
+ end
74
+
57
75
  context "when doing lowercase of an array" do
58
76
 
59
77
  let(:config) do
@@ -72,7 +90,7 @@ describe LogStash::Filters::Mutate do
72
90
  end
73
91
  end
74
92
 
75
- %w(lowercase uppercase).each do |operation|
93
+ %w(lowercase uppercase capitalize).each do |operation|
76
94
  context "executing #{operation} a non-existant field" do
77
95
  let(:attrs) { }
78
96
 
@@ -262,12 +280,14 @@ describe LogStash::Filters::Mutate do
262
280
  mutate {
263
281
  lowercase => ["lowerme","Lowerme", "lowerMe"]
264
282
  uppercase => ["upperme", "Upperme", "upperMe"]
283
+ capitalize => ["capitalizeme", "Capitalizeme", "capitalizeMe"]
265
284
  convert => [ "intme", "integer", "floatme", "float" ]
266
285
  rename => [ "rename1", "rename2" ]
267
286
  replace => [ "replaceme", "hello world" ]
268
287
  replace => [ "newfield", "newnew" ]
269
288
  update => [ "nosuchfield", "weee" ]
270
289
  update => [ "updateme", "updated" ]
290
+
271
291
  }
272
292
  }
273
293
  CONFIG
@@ -275,10 +295,13 @@ describe LogStash::Filters::Mutate do
275
295
  event = {
276
296
  "lowerme" => "example",
277
297
  "upperme" => "EXAMPLE",
298
+ "capitalizeme" => "Example",
278
299
  "Lowerme" => "ExAmPlE",
279
300
  "Upperme" => "ExAmPlE",
301
+ "Capitalizeme" => "ExAmPlE",
280
302
  "lowerMe" => [ "ExAmPlE", "example" ],
281
303
  "upperMe" => [ "ExAmPlE", "EXAMPLE" ],
304
+ "capitalizeMe" => [ "ExAmPlE", "Example" ],
282
305
  "intme" => [ "1234", "7890.4", "7.9" ],
283
306
  "floatme" => [ "1234.455" ],
284
307
  "rename1" => [ "hello world" ],
@@ -289,10 +312,13 @@ describe LogStash::Filters::Mutate do
289
312
  sample event do
290
313
  expect(subject.get("lowerme")).to eq 'example'
291
314
  expect(subject.get("upperme")).to eq 'EXAMPLE'
315
+ expect(subject.get("capitalizeme")).to eq 'Example'
292
316
  expect(subject.get("Lowerme")).to eq 'example'
293
317
  expect(subject.get("Upperme")).to eq 'EXAMPLE'
318
+ expect(subject.get("Capitalizeme")).to eq 'Example'
294
319
  expect(subject.get("lowerMe")).to eq ['example', 'example']
295
320
  expect(subject.get("upperMe")).to eq ['EXAMPLE', 'EXAMPLE']
321
+ expect(subject.get("capitalizeMe")).to eq ['Example', 'Example']
296
322
  expect(subject.get("intme") ).to eq [1234, 7890, 7]
297
323
  expect(subject.get("floatme")).to eq [1234.455]
298
324
  expect(subject).not_to include("rename1")
@@ -311,6 +337,7 @@ describe LogStash::Filters::Mutate do
311
337
  mutate {
312
338
  lowercase => ["lowerme"]
313
339
  uppercase => ["upperme"]
340
+ capitalize => ["capitalizeme"]
314
341
  }
315
342
  }
316
343
  CONFIG
@@ -318,12 +345,14 @@ describe LogStash::Filters::Mutate do
318
345
  event = {
319
346
  "lowerme" => [ "АБВГД\0MMM", "こにちわ", "XyZółć", "NÎcË GÛŸ"],
320
347
  "upperme" => [ "аБвгд\0mmm", "こにちわ", "xYzółć", "Nîcë gûÿ"],
348
+ "capitalizeme" => ["АБВГД\0mmm", "こにちわ", "xyzółć", "nÎcË gÛŸ"],
321
349
  }
322
350
 
323
351
  sample event do
324
352
  # ATM, only the ASCII characters will case change
325
353
  expect(subject.get("lowerme")).to eq [ "АБВГД\0mmm", "こにちわ", "xyzółć", "nÎcË gÛŸ"]
326
354
  expect(subject.get("upperme")).to eq [ "аБвгд\0MMM", "こにちわ", "XYZółć", "NîCë Gûÿ"]
355
+ expect(subject.get("capitalizeme")).to eq [ "АБВГД\u0000mmm", "こにちわ", "Xyzółć", "NÎcË gÛŸ"]
327
356
  end
328
357
  end
329
358
 
@@ -520,7 +549,7 @@ describe LogStash::Filters::Mutate do
520
549
  expect(subject.get("[foo][bar]")).to eq 1000
521
550
  expect(subject.get("[foo][bar]")).to be_a(Fixnum)
522
551
  end
523
- end
552
+ end
524
553
 
525
554
  describe "convert should work within arrays" do
526
555
  config <<-CONFIG
@@ -566,6 +595,102 @@ describe LogStash::Filters::Mutate do
566
595
  end
567
596
  end
568
597
 
598
+ describe "convert various US/UK strings" do
599
+ describe "to integer" do
600
+ config <<-CONFIG
601
+ filter {
602
+ mutate {
603
+ convert => {
604
+ "[foo][0]" => "integer"
605
+ "[foo][1]" => "integer"
606
+ "[foo][2]" => "integer"
607
+ }
608
+ }
609
+ }
610
+ CONFIG
611
+
612
+ sample({ "foo" => ["1,000", "1,234,567.8", "123.4"] }) do
613
+ expect(subject.get("[foo][0]")).to eq 1000
614
+ expect(subject.get("[foo][0]")).to be_a(Fixnum)
615
+ expect(subject.get("[foo][1]")).to eq 1234567
616
+ expect(subject.get("[foo][1]")).to be_a(Fixnum)
617
+ expect(subject.get("[foo][2]")).to eq 123
618
+ expect(subject.get("[foo][2]")).to be_a(Fixnum)
619
+ end
620
+ end
621
+
622
+ describe "to float" do
623
+ config <<-CONFIG
624
+ filter {
625
+ mutate {
626
+ convert => {
627
+ "[foo][0]" => "float"
628
+ "[foo][1]" => "float"
629
+ "[foo][2]" => "float"
630
+ }
631
+ }
632
+ }
633
+ CONFIG
634
+
635
+ sample({ "foo" => ["1,000", "1,234,567.8", "123.4"] }) do
636
+ expect(subject.get("[foo][0]")).to eq 1000.0
637
+ expect(subject.get("[foo][0]")).to be_a(Float)
638
+ expect(subject.get("[foo][1]")).to eq 1234567.8
639
+ expect(subject.get("[foo][1]")).to be_a(Float)
640
+ expect(subject.get("[foo][2]")).to eq 123.4
641
+ expect(subject.get("[foo][2]")).to be_a(Float)
642
+ end
643
+ end
644
+ end
645
+
646
+ describe "convert various EU style strings" do
647
+ describe "to integer" do
648
+ config <<-CONFIG
649
+ filter {
650
+ mutate {
651
+ convert => {
652
+ "[foo][0]" => "integer_eu"
653
+ "[foo][1]" => "integer_eu"
654
+ "[foo][2]" => "integer_eu"
655
+ }
656
+ }
657
+ }
658
+ CONFIG
659
+
660
+ sample({ "foo" => ["1.000", "1.234.567,8", "123,4"] }) do
661
+ expect(subject.get("[foo][0]")).to eq 1000
662
+ expect(subject.get("[foo][0]")).to be_a(Fixnum)
663
+ expect(subject.get("[foo][1]")).to eq 1234567
664
+ expect(subject.get("[foo][1]")).to be_a(Fixnum)
665
+ expect(subject.get("[foo][2]")).to eq 123
666
+ expect(subject.get("[foo][2]")).to be_a(Fixnum)
667
+ end
668
+ end
669
+
670
+ describe "to float" do
671
+ config <<-CONFIG
672
+ filter {
673
+ mutate {
674
+ convert => {
675
+ "[foo][0]" => "float_eu"
676
+ "[foo][1]" => "float_eu"
677
+ "[foo][2]" => "float_eu"
678
+ }
679
+ }
680
+ }
681
+ CONFIG
682
+
683
+ sample({ "foo" => ["1.000", "1.234.567,8", "123,4"] }) do
684
+ expect(subject.get("[foo][0]")).to eq 1000.0
685
+ expect(subject.get("[foo][0]")).to be_a(Float)
686
+ expect(subject.get("[foo][1]")).to eq 1234567.8
687
+ expect(subject.get("[foo][1]")).to be_a(Float)
688
+ expect(subject.get("[foo][2]")).to eq 123.4
689
+ expect(subject.get("[foo][2]")).to be_a(Float)
690
+ end
691
+ end
692
+ end
693
+
569
694
  #LOGSTASH-1529
570
695
  describe "gsub on a String with dynamic fields (%{}) in pattern" do
571
696
  config '
@@ -712,4 +837,26 @@ describe LogStash::Filters::Mutate do
712
837
  end
713
838
  end
714
839
 
840
+ describe "coerce arrays fields with default values when null" do
841
+ config '
842
+ filter {
843
+ mutate {
844
+ coerce => {
845
+ "field1" => "Hello"
846
+ "field2" => "Bye"
847
+ "field3" => 5
848
+ "field4" => false
849
+ }
850
+ }
851
+ }'
852
+
853
+
854
+ sample("field1" => nil, "field2" => nil, "field3" => nil, "field4" => true) do
855
+ expect(subject.get("field1")).to eq("Hello")
856
+ expect(subject.get("field2")).to eq("Bye")
857
+ expect(subject.get("field3")).to eq("5")
858
+ expect(subject.get("field4")).to eq(true)
859
+ end
860
+ end
861
+
715
862
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstash-filter-mutate
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.0
4
+ version: 3.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elastic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-28 00:00:00.000000000 Z
11
+ date: 2018-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement