logstash-plugins / logstash-filter-mutate

Apache License 2.0
16 stars 75 forks source link

converting from non-string values to boolean isn't supported #73

Closed ferrency closed 6 years ago

ferrency commented 8 years ago

See also related issue #47.

Summary

Using the mutate convert feature to convert non-string values to boolean does not work correctly.

Should it?

If it should, I will provide a patch to make it work. If it should not, I can provide a documentation patch to reflect that conversion to boolean only works on string values.

Details of why it fails

See convert_boolean, here: https://github.com/logstash-plugins/logstash-filter-mutate/blob/master/lib/logstash/filters/mutate.rb#L303

Method convert_boolean converts a value to boolean using regular expressions. Regular expressions won't match on an integer or float, so conversions from non-string field values do not work correctly. Also, the empty? method is used to convert empty strings to false boolean values. Attempting to convert a non-string value to boolean results in an exception being thrown because the empty? method doesn't exist in class FixNum.

Proposed patch

Alternately: Document that boolean conversions only work on strings, and that converting from a non-boolean value to boolean requires multiple convert steps (with string as an intermediate convertsion type).

Thanks! Alan Ferrency

BigYellowHammer commented 6 years ago

For anybody looking to solve it. You can use multiple filters to solve it.

filter {
  mutate {
    convert => {
      "fieldname" => "string"
    }
  }
}
filter {
  mutate {
    convert => {
      "fieldname" => "boolean"
    }
  }
}

First the field is converted to string and then string can be easily converted to boolean.

Generally in case of fixing it - how about doing value.to_s?