Kouhei Sutou
null+****@clear*****
Mon Oct 20 18:03:17 JST 2014
Kouhei Sutou 2014-10-20 18:03:17 +0900 (Mon, 20 Oct 2014) New Revision: ac6cdadbfa4f991b03cfcac54982e9c685ec02e8 https://github.com/groonga/fluent-plugin-groonga/commit/ac6cdadbfa4f991b03cfcac54982e9c685ec02e8 Message: Support guessing value type from string data Modified files: lib/fluent/plugin/out_groonga.rb test/output/test_type_guesser.rb Modified: lib/fluent/plugin/out_groonga.rb (+36 -5) =================================================================== --- lib/fluent/plugin/out_groonga.rb 2014-10-20 18:03:05 +0900 (ee61ada) +++ lib/fluent/plugin/out_groonga.rb 2014-10-20 18:03:17 +0900 (9428cee) @@ -194,26 +194,57 @@ module Fluent end end + def integer_value?(value) + case value + when String + begin + Integer(value) + true + rescue ArgumentError + false + end + when Integer + true + else + false + end + end + def int32_values? int32_min = -(2 ** 31) int32_max = 2 ** 31 - 1 range = int32_min..int32_max @sample_values.all? do |sample_value| - sample_value.is_a?(Integer) and - range.cover?(sample_value) + integer_value?(sample_value) and + range.cover?(Integer(sample_value)) end end def int64_values? @sample_values.all? do |sample_value| - sample_value.is_a?(Integer) + integer_value?(sample_value) + end + end + + def float_value?(value) + case value + when String + begin + Float(value) + true + rescue ArgumentError + false + end + when Float + true + else + false end end def float_values? @sample_values.all? do |sample_value| - sample_value.is_a?(Float) or - sample_value.is_a?(Integer) + float_value?(sample_value) end end Modified: test/output/test_type_guesser.rb (+26 -0) =================================================================== --- test/output/test_type_guesser.rb 2014-10-20 18:03:05 +0900 (1f86ffe) +++ test/output/test_type_guesser.rb 2014-10-20 18:03:17 +0900 (183b87e) @@ -65,6 +65,10 @@ class OutputTypeGuesserTest < Test::Unit::TestCase test "zero" do assert_equal("Int32", guess([0])) end + + test "string" do + assert_equal("Int32", guess(["0"])) + end end sub_test_case "Int64" do @@ -77,6 +81,28 @@ class OutputTypeGuesserTest < Test::Unit::TestCase int32_max = 2 ** 31 - 1 assert_equal("Int64", guess([int32_max + 1])) end + + test "string" do + assert_equal("Int64", guess([(2 ** 32).to_s])) + end + end + + sub_test_case "Float" do + test "positive" do + assert_equal("Float", guess([1.0])) + end + + test "negative" do + assert_equal("Float", guess([-1.0])) + end + + test "zero" do + assert_equal("Float", guess([0.0])) + end + + test "string" do + assert_equal("Float", guess(["1.1"])) + end end sub_test_case "WGS84GeoPoint" do -------------- next part -------------- HTML����������������������������... Descargar