Skip to content

Commit

Permalink
Merge pull request #1477 from okkez/fix-undefined-conversion-error
Browse files Browse the repository at this point in the history
Use `String#force_encoding` instead of `String#encode`
  • Loading branch information
repeatedly committed Mar 3, 2017
1 parent 9fee2fb commit c279a3e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/fluent/config/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def self.bool_value(str)
end
end

STRING_TYPE = Proc.new { |val, opts| val }
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]
Expand All @@ -86,7 +86,7 @@ def self.bool_value(str)
value
else
case type
when :string then value.to_s
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)
Expand Down
19 changes: 19 additions & 0 deletions test/config/test_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ class TestConfigTypes < ::Test::Unit::TestCase
assert_equal ' ', Config::STRING_TYPE.call(' ', {})
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]})
Expand Down Expand Up @@ -142,6 +153,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', {}))
Expand Down

0 comments on commit c279a3e

Please sign in to comment.