Skip to content

Commit

Permalink
Merge pull request #1369 from fluent/csv-raise-empty-field
Browse files Browse the repository at this point in the history
raise ConfigError when fields parameter is empty array
  • Loading branch information
repeatedly authored Dec 14, 2016
2 parents c0a5cb7 + f65dad7 commit bb685e6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/fluent/plugin/formatter_csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ class CsvFormatter < Formatter
def configure(conf)
super
@fields = fields.select{|f| !f.empty? }
raise ConfigError, "empty value is specified in fields parameter" if @fields.empty?

@generate_opts = {col_sep: @delimiter, force_quotes: @force_quotes}
end

def format(tag, time, record)
row = @fields.map do |key|
record[key]
end
CSV.generate_line(row, col_sep: @delimiter,
force_quotes: @force_quotes)
CSV.generate_line(row, @generate_opts)
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/plugin/test_formatter_csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ def test_config_params
assert_equal(['a', 'b', 'c'], d.instance.fields)
end

data('empty array' => [],
'array including empty string' => ['', ''])
def test_empty_fields(param)
assert_raise Fluent::ConfigError do
create_driver('fields' => param)
end
end

data(
'tab_char' => ["\t", '\t'],
'tab_string' => ["\t", 'TAB'],
Expand Down

0 comments on commit bb685e6

Please sign in to comment.