diff --git a/lib/fluent/config/types.rb b/lib/fluent/config/types.rb index a8739f9d15..4f5df084a4 100644 --- a/lib/fluent/config/types.rb +++ b/lib/fluent/config/types.rb @@ -64,7 +64,7 @@ def self.bool_value(str) end end - STRING_TYPE = Proc.new { |val, opts| val.to_s.encode(Encoding::UTF_8) } + STRING_TYPE = Proc.new { |val, opts| val.to_s.force_encoding(Encoding::UTF_8) } ENUM_TYPE = Proc.new { |val, opts| s = val.to_sym list = opts[:list] @@ -85,7 +85,7 @@ def self.bool_value(str) value else case type - when :string then value.to_s.encode(Encoding::UTF_8) + when :string then value.to_s.force_encoding(Encoding::UTF_8) when :integer then value.to_i when :float then value.to_f when :size then Config.size_value(value) diff --git a/test/config/test_types.rb b/test/config/test_types.rb index 350b7b32c7..6c12418bd7 100644 --- a/test/config/test_types.rb +++ b/test/config/test_types.rb @@ -69,6 +69,17 @@ class TestConfigTypes < ::Test::Unit::TestCase assert_equal Encoding::UTF_8, Config::STRING_TYPE.call('test', {}).encoding end + data('latin' => 'Märch', + 'ascii' => 'ascii', + 'space' => ' ', + 'number' => '1', + 'Hiragana' => 'あいうえお') + test 'string w/ binary' do |str| + actual = Config::STRING_TYPE.call(str.b, {}) + assert_equal str, actual + assert_equal Encoding::UTF_8, actual.encoding + end + test 'enum' do assert_equal :val, Config::ENUM_TYPE.call('val', {list: [:val, :value, :v]}) assert_equal :v, Config::ENUM_TYPE.call('v', {list: [:val, :value, :v]}) @@ -143,6 +154,14 @@ class TestConfigTypes < ::Test::Unit::TestCase assert_raise(RuntimeError.new("unknown type in REFORMAT: foo")){ Config::HASH_TYPE.call("x:1,y:2", {value_type: :foo}) } end + data('latin' => ['3:Märch', {"3"=>"Märch"}], + 'ascii' => ['ascii:ascii', {"ascii"=>"ascii"}], + 'number' => ['number:1', {"number"=>"1"}], + 'Hiragana' => ['hiragana:あいうえお', {"hiragana"=>"あいうえお"}]) + test 'hash w/ binary' do |(target, expected)| + assert_equal(expected, Config::HASH_TYPE.call(target.b, { value_type: :string })) + end + test 'array' do assert_equal(["1","2",1], Config::ARRAY_TYPE.call('["1","2",1]', {})) assert_equal(["1","2","1"], Config::ARRAY_TYPE.call('1,2,1', {}))