From e52e4a25e5ef39225e9feca5d1173425d39446ee Mon Sep 17 00:00:00 2001 From: Masahiro Nakagawa Date: Thu, 3 Aug 2017 10:25:55 +0900 Subject: [PATCH] filter_parser: use record_accessor --- lib/fluent/plugin/filter_parser.rb | 5 +++-- test/plugin/test_filter_parser.rb | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/fluent/plugin/filter_parser.rb b/lib/fluent/plugin/filter_parser.rb index b05dd3ce96..f76498ce57 100644 --- a/lib/fluent/plugin/filter_parser.rb +++ b/lib/fluent/plugin/filter_parser.rb @@ -24,7 +24,7 @@ module Fluent::Plugin class ParserFilter < Filter Fluent::Plugin.register_filter('parser', self) - helpers :parser, :compat_parameters + helpers :parser, :record_accessor, :compat_parameters config_param :key_name, :string config_param :reserve_data, :bool, default: false @@ -41,6 +41,7 @@ def configure(conf) super + @accessor = record_accessor_create(@key_name) @parser = parser_create end @@ -48,7 +49,7 @@ def configure(conf) REPLACE_CHAR = '?'.freeze def filter_with_time(tag, time, record) - raw_value = record[@key_name] + raw_value = @accessor.call(record) if raw_value.nil? if @emit_invalid_record_to_error router.emit_error_event(tag, time, record, ArgumentError.new("#{@key_name} does not exist")) diff --git a/test/plugin/test_filter_parser.rb b/test/plugin/test_filter_parser.rb index df1824f129..046e02ab28 100644 --- a/test/plugin/test_filter_parser.rb +++ b/test/plugin/test_filter_parser.rb @@ -390,6 +390,29 @@ def test_filter_csv(conf) assert_equal 'value"ThreeYes!', first[1]['key3'] end + def test_filter_with_nested_record + d = create_driver(%[ + key_name $.data.log + + @type csv + keys key1,key2,key3 + + ]) + time = @default_time.to_i + d.run do + d.feed(@tag, time, {'data' => {'log' => 'value1,"value2","value""ThreeYes!"'}, 'xxx' => 'x', 'yyy' => 'y'}) + end + filtered = d.filtered + assert_equal 1, filtered.length + + first = filtered[0] + assert_equal time, first[0] + assert_nil first[1]['data'] + assert_equal 'value1', first[1]['key1'] + assert_equal 'value2', first[1]['key2'] + assert_equal 'value"ThreeYes!', first[1]['key3'] + end + CONFIG_HASH_VALUE_FIELD = %[ key_name data hash_value_field parsed