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

Change serialization syntax #867

Merged
merged 7 commits into from
Mar 29, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
42 changes: 37 additions & 5 deletions lib/fluent/config/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,7 @@ def to_s(nest = 0)
out << "#{indent}<#{@name} #{@arg}>\n"
end
each_pair { |k, v|
if secret_param?(k)
out << "#{nindent}#{k} xxxxxx\n"
else
out << "#{nindent}#{k} #{v}\n"
end
out << dump_value(k, v, indent, nindent)
}
@elements.each { |e|
out << e.to_s(nest + 1)
Expand All @@ -124,6 +120,8 @@ def to_s(nest = 0)
def to_masked_element
new_elems = @elements.map { |e| e.to_masked_element }
new_elem = Element.new(@name, @arg, {}, new_elems, @unused)
new_elem.v1_config = @v1_config
new_elem.corresponding_proxies = @corresponding_proxies
each_pair { |k, v|
new_elem[k] = secret_param?(k) ? 'xxxxxx' : v
}
Expand All @@ -144,6 +142,40 @@ def secret_param?(key)
false
end

def param_type(key)
return nil if @corresponding_proxies.empty?

param_key = key.to_sym
proxy = @corresponding_proxies.detect do |_proxy|
_proxy.params.has_key?(param_key)
end
return nil unless proxy
_block, opts = proxy.params[param_key]
opts[:type]
end

def dump_value(k, v, indent, nindent)
Copy link
Member

Choose a reason for hiding this comment

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

Is there any reason to assign out = "" at once and then do out << "..."?
It looks good enough just to return string value by if expression.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exactly.
I will fix this.

if secret_param?(k)
"#{nindent}#{k} xxxxxx\n"
else
if @v1_config
case param_type(k)
when :string
"#{nindent}#{k} \"#{self.class.unescape_parameter(v)}\"\n"
when :enum, :integer, :float, :size, :bool, :time
"#{nindent}#{k} #{v}\n"
when :hash, :array
"#{nindent}#{k} #{v}\n"
else
# Unknown type
"#{nindent}#{k} #{v}\n"
end
else
"#{nindent}#{k} #{v}\n"
end
end
end

def self.unescape_parameter(v)
result = ''
v.each_char { |c| result << LiteralParser.unescape_char(c) }
Expand Down
82 changes: 82 additions & 0 deletions test/config/test_config_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ def e(name, arg='', attrs={}, elements=[])
end
end

class AllTypes
include Fluent::Configurable

config_param :param_string, :string
config_param :param_enum, :enum, list: [:foo, :bar, :baz]
config_param :param_integer, :integer
config_param :param_float, :float
config_param :param_size, :size
config_param :param_bool, :bool
config_param :param_time, :time
config_param :param_hash, :hash
config_param :param_array, :array
end

class TestV1Parser < ::Test::Unit::TestCase
def read_config(path)
path = File.expand_path(path)
Expand Down Expand Up @@ -384,6 +398,74 @@ def prepare_config
conf2 = parse_text(conf.to_s) # use dumpped configuration to check unescape
assert_equal(expected, conf2.elements.first['k1'])
end

test 'all types' do
conf = parse_text(%[
param_string "value"
param_enum foo
param_integer 999
param_float 55.55
param_size 4k
param_bool true
param_time 10m
param_hash { "key1": "value1", "key2": 2 }
param_array ["value1", "value2", 100]
])
target = AllTypes.new.configure(conf)
assert_equal(conf.to_s, target.config.to_s)
expected = <<DUMP
<ROOT>
param_string "value"
param_enum foo
param_integer 999
param_float 55.55
param_size 4k
param_bool true
param_time 10m
param_hash {"key1":"value1","key2":2}
param_array ["value1","value2",100]
</ROOT>
DUMP
assert_equal(expected, conf.to_s)
end
end
end

class TestV0Parser < ::Test::Unit::TestCase
def parse_text(text)
basepath = File.expand_path(File.dirname(__FILE__) + '/../../')
Fluent::Config::Parser.parse(StringIO.new(text), '(test)', basepath)
end

sub_test_case "Fluent::Config::Element#to_s" do
test 'all types' do
conf = parse_text(%[
param_string value
param_enum foo
param_integer 999
param_float 55.55
param_size 4k
param_bool true
param_time 10m
param_hash { "key1": "value1", "key2": 2 }
param_array ["value1", "value2", 100]
])
target = AllTypes.new.configure(conf)
assert_equal(conf.to_s, target.config.to_s)
expected = <<DUMP
<ROOT>
param_string value
param_enum foo
param_integer 999
param_float 55.55
param_size 4k
param_bool true
param_time 10m
param_hash { "key1": "value1", "key2": 2 }
param_array ["value1", "value2", 100]
</ROOT>
DUMP
end
end
end
end