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 simplified syntax for hash and array #875

Merged
merged 4 commits into from
Apr 4, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
47 changes: 43 additions & 4 deletions lib/fluent/config/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,58 @@ def self.bool_value(str)
SIZE_TYPE = Proc.new { |val, opts| Config.size_value(val) }
BOOL_TYPE = Proc.new { |val, opts| Config.bool_value(val) }
TIME_TYPE = Proc.new { |val, opts| Config.time_value(val) }

REFORMAT_VALUES = ->(type, value) {
Copy link
Member

Choose a reason for hiding this comment

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

Why REFORMAT_VALUES? REFORMAT_VALUE?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed to REFORMAT_VALUE

if value.nil?
value
else
case type
when :string then value.to_s
when :integer then value.to_i
when :float then value.to_f
when :size then Config.size_value(value)
when :bool then Config.bool_value(value)
when :time then Config.time_value(value)
else
raise "unknown type in REFORMAT: #{type}"
end
end
}

HASH_TYPE = Proc.new { |val, opts|
param = val.is_a?(String) ? JSON.load(val) : val
param = if val.is_a?(String)
val.start_with?('{') ? JSON.load(val) : Hash[val.strip.split(/\s*,\s*/).map{|v| v.split(':', 2)}]
else
val
end
if param.class != Hash
raise ConfigError, "hash required but got #{val.inspect}"
end
param
if opts.empty?
param
else
newparam = {}
param.each_pair do |key, value|
new_key = opts[:symbolize_keys] ? key.to_sym : key
newparam[new_key] = opts[:value_type] ? REFORMAT_VALUES.call(opts[:value_type], value) : value
end
newparam
end
}
ARRAY_TYPE = Proc.new { |val, opts|
param = val.is_a?(String) ? JSON.load(val) : val
param = if val.is_a?(String)
val.start_with?('[') ? JSON.load(val) : val.strip.split(/\s*,\s*/)
else
val
end
if param.class != Array
raise ConfigError, "array required but got #{val.inspect}"
end
param
if opts[:value_type]
param.map{|v| REFORMAT_VALUES.call(opts[:value_type], v) }
else
param
end
}
end

Expand Down
28 changes: 28 additions & 0 deletions test/config/test_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,38 @@ class TestConfigTypes < ::Test::Unit::TestCase

test 'hash' do
assert_equal({"x"=>"v","k"=>1}, Config::HASH_TYPE.call('{"x":"v","k":1}', {}))
assert_equal({"x"=>"v","k"=>"1"}, Config::HASH_TYPE.call('x:v,k:1', {}))
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a test a hash with , surrounded by spaces?

Copy link
Member Author

Choose a reason for hiding this comment

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

done

assert_equal({"x"=>"v","k"=>"1"}, Config::HASH_TYPE.call('x:v, k:1', {}))
assert_equal({"x"=>"v","k"=>"1"}, Config::HASH_TYPE.call(' x:v, k:1 ', {}))
assert_equal({"x"=>"v","k"=>"1"}, Config::HASH_TYPE.call('x:v , k:1 ', {}))

assert_equal({"x"=>"v:v","k"=>"1"}, Config::HASH_TYPE.call('x:v:v, k:1', {}))

assert_equal({x: "v", k: 1}, Config::HASH_TYPE.call('{"x":"v","k":1}', {symbolize_keys: true}))
assert_equal({x: "v", k: "1"}, Config::HASH_TYPE.call('x:v,k:1', {symbolize_keys: true, value_type: :string}))
assert_equal({x: "v", k: "1"}, Config::HASH_TYPE.call('{"x":"v","k":1}', {symbolize_keys: true, value_type: :string}))
assert_equal({x: 0, k: 1}, Config::HASH_TYPE.call('x:0,k:1', {symbolize_keys: true, value_type: :integer}))

assert_equal({"x"=>1,"y"=>60,"z"=>3600}, Config::HASH_TYPE.call('{"x":"1s","y":"1m","z":"1h"}', {value_type: :time}))
assert_equal({"x"=>1,"y"=>60,"z"=>3600}, Config::HASH_TYPE.call('x:1s,y:1m,z:1h', {value_type: :time}))
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', {}))

assert_equal(["a","b","c"], Config::ARRAY_TYPE.call('["a","b","c"]', {}))
assert_equal(["a","b","c"], Config::ARRAY_TYPE.call('a,b,c', {}))
assert_equal(["a","b","c"], Config::ARRAY_TYPE.call('a, b, c', {}))
assert_equal(["a","b","c"], Config::ARRAY_TYPE.call('a , b , c', {}))

assert_equal(["a a","b,b"," c "], Config::ARRAY_TYPE.call('["a a","b,b"," c "]', {}))

assert_equal(["a a","b","c"], Config::ARRAY_TYPE.call('a a,b,c', {}))

assert_equal([1,2,1], Config::ARRAY_TYPE.call('[1,2,1]', {}))
assert_equal([1,2,1], Config::ARRAY_TYPE.call('["1","2","1"]', {value_type: :integer}))
assert_equal([1,2,1], Config::ARRAY_TYPE.call('1,2,1', {value_type: :integer}))

array_options = {
default: [],
Expand Down