Skip to content

Commit

Permalink
fix to overwrite correctly bool configuration params with default tru…
Browse files Browse the repository at this point in the history
…e value by false value specified by configuration
  • Loading branch information
tagomoris committed Apr 21, 2014
1 parent 01889e6 commit b13eea1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/fluent/config/section.rb
Expand Up @@ -12,6 +12,10 @@ def initialize(params = {})
@params = params
end

def nil?
false
end

def to_h
@params
end
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/configurable.rb
Expand Up @@ -40,7 +40,7 @@ def configure(conf)

root.instance_eval{ @params.keys }.each do |param_name|
varname = "@#{param_name}".to_sym
if root[param_name] || instance_variable_get(varname).nil?
if (! root[param_name].nil?) || instance_variable_get(varname).nil?
instance_variable_set(varname, root[param_name])
end
end
Expand Down
24 changes: 21 additions & 3 deletions spec/config/configurable_spec.rb
Expand Up @@ -7,14 +7,16 @@ class Base1
include Fluent::Configurable

config_param :node, :string, :default => "node"
config_param :flag1, :bool, :default => false
config_param :flag2, :bool, :default => true

config_param :name1, :string
config_param :name2, :string
config_param :name3, :string, :default => "base1"
config_param :name4, :string, :default => "base1"

def get_all
[@node, @name1, @name2, @name3, @name4]
[@node, @flag1, @flag2, @name1, @name2, @name3, @name4]
end
end

Expand Down Expand Up @@ -86,6 +88,8 @@ def get_all
it 'create instance methods and default values by config_param and config_set_default' do
obj1 = ConfigurableSpec::Base1.new
expect(obj1.node).to eql("node")
expect(obj1.flag1).to eq(false)
expect(obj1.flag2).to eq(true)
expect(obj1.name1).to be_nil
expect(obj1.name2).to be_nil
expect(obj1.name3).to eql("base1")
Expand All @@ -95,6 +99,8 @@ def get_all
it 'create instance methods and default values overwritten by sub class definition' do
obj2 = ConfigurableSpec::Base2.new
expect(obj2.node).to eql("node")
expect(obj2.flag1).to eq(false)
expect(obj2.flag2).to eq(true)
expect(obj2.name1).to be_nil
expect(obj2.name2).to eql("base2")
expect(obj2.name3).to eql("base1")
Expand All @@ -112,7 +118,19 @@ def get_all
expect{ b2.configure({"name5" => "t5"}) }.to raise_error(Fluent::ConfigError)
expect{ b2.configure({"name1" => "t1", "name5" => "t5"}) }.not_to raise_error()

expect(b2.get_all).to eql(["node", "t1", "base2", "base1", "base2", "t5", "base2"])
expect(b2.get_all).to eql(["node", false, true, "t1", "base2", "base1", "base2", "t5", "base2"])
end

it 'can configure bool values' do
b2a = ConfigurableSpec::Base2.new
expect{ b2a.configure({"flag1" => "true", "flag2" => "yes", "name1" => "t1", "name5" => "t5"}) }.not_to raise_error()
expect(b2a.flag1).to eq(true)
expect(b2a.flag2).to eq(true)

b2b = ConfigurableSpec::Base2.new
expect{ b2b.configure({"flag1" => "false", "flag2" => "no", "name1" => "t1", "name5" => "t5"}) }.not_to raise_error()
expect(b2b.flag1).to eq(false)
expect(b2b.flag2).to eq(false)
end

it 'overwrites values of defaults' do
Expand All @@ -125,7 +143,7 @@ def get_all
expect(b2.name5).to eql("t5")
expect(b2.name6).to eql("base2")

expect(b2.get_all).to eql(["node", "t1", "t2", "t3", "t4", "t5", "base2"])
expect(b2.get_all).to eql(["node", false, true, "t1", "t2", "t3", "t4", "t5", "base2"])
end
end
end
Expand Down

0 comments on commit b13eea1

Please sign in to comment.