Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameter delimiter_pattern to parser_ltsv #1802

Merged
merged 2 commits into from
Dec 27, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/fluent/plugin/parser_ltsv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ class LabeledTSVParser < Parser

desc 'The delimiter character (or string) of TSV values'
config_param :delimiter, :string, default: "\t"
desc 'The delimiter pattern of TSV values'
config_param :delimiter_pattern, default: nil do |value|
Regexp.compile(value[1..-2]) if value
end
desc 'The delimiter character between field name and value'
config_param :label_delimiter, :string, default: ":"

config_set_default :time_key, 'time'

def parse(text)
r = {}
text.split(@delimiter).each do |pair|
delimiter = @delimiter_pattern || @delimiter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about moving this to configure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reviewing. Good catch.

text.split(delimiter).each do |pair|
key, value = pair.split(@label_delimiter, 2)
r[key] = value
end
Expand Down
17 changes: 17 additions & 0 deletions test/plugin/test_parser_labeled_tsv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,21 @@ def test_parse_with_null_empty_string
assert_equal record['b'], ' '
end
end

data("single space" => ["k1=v1 k2=v2", { "k1" => "v1", "k2" => "v2" }],
"multiple space" => ["k1=v1 k2=v2", { "k1" => "v1", "k2" => "v2" }],
"reverse" => ["k2=v2 k1=v1", { "k1" => "v1", "k2" => "v2" }],
"tab" => ["k2=v2\tk1=v1", { "k1" => "v1", "k2" => "v2" }],
"tab and space" => ["k2=v2\t k1=v1", { "k1" => "v1", "k2" => "v2" }])
def test_parse_with_delimiter_pattern(data)
text, expected = data
parser = Fluent::Test::Driver::Parser.new(Fluent::Plugin::LabeledTSVParser)
parser.configure(
'delimiter_pattern' => '/\s+/',
'label_delimiter' => '='
)
parser.instance.parse(text) do |_time, record|
assert_equal(expected, record)
end
end
end