Skip to content

Commit

Permalink
Enable to set nil to parameters in config file
Browse files Browse the repository at this point in the history
Example setting:

  username: "#{ENV['USERNAME'] || raise(SetNil)}"

Signed-off-by: Takuro Ashie <ashie@clear-code.com>
  • Loading branch information
ashie committed Nov 20, 2019
1 parent 7b2e9ec commit f7b5914
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
3 changes: 3 additions & 0 deletions lib/fluent/config/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ class ConfigParseError < ConfigError

class ObsoletedParameterError < ConfigError
end

class SetNil < ConfigError
end
end
12 changes: 10 additions & 2 deletions lib/fluent/config/literal_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ def scan_double_quoted_string
string = []
while true
if skip(/\"/)
return string.join
if string.include?(nil)
return nil
else
return string.join
end
elsif check(/[^"]#{LINE_END_WITHOUT_SPACING_AND_COMMENT}/)
if s = check(/[^\\]#{LINE_END_WITHOUT_SPACING_AND_COMMENT}/)
string << s
Expand Down Expand Up @@ -168,7 +172,11 @@ def eval_embedded_code(code)
hostname = Socket.gethostname
worker_id = ENV['SERVERENGINE_WORKER_ID'] || ''
EOM
@eval_context.instance_eval(code)
begin
@eval_context.instance_eval(code)
rescue SetNil => e
nil
end
end

def eval_escape_char(c)
Expand Down
10 changes: 7 additions & 3 deletions lib/fluent/config/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ def self.regexp_value(str, opts = {}, name = nil)
end

STRING_TYPE = Proc.new { |val, opts = {}, name = nil|
v = val.to_s
v = v.frozen? ? v.dup : v # config_param can't assume incoming string is mutable
v.force_encoding(Encoding::UTF_8)
if val.nil?
nil
else
v = val.to_s
v = v.frozen? ? v.dup : v # config_param can't assume incoming string is mutable
v.force_encoding(Encoding::UTF_8)
end
}

ENUM_TYPE = Proc.new { |val, opts = {}, name = nil|
Expand Down
1 change: 1 addition & 0 deletions test/config/test_literal_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ def test_falseX
assert_text_parsed_as("foo1", '"foo#{worker_id}"')
ENV.delete('SERVERENGINE_WORKER_ID')
}
test('nil') { assert_text_parsed_as(nil, '"#{raise SetNil}"') }
end

sub_test_case 'array parsing' do
Expand Down
4 changes: 4 additions & 0 deletions test/config/test_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ class TestConfigTypes < ::Test::Unit::TestCase
assert_equal Encoding::UTF_8, Config::STRING_TYPE.call(val, {}).encoding
end

test 'string nil' do
assert_equal nil, Config::STRING_TYPE.call(nil, {})
end

data('latin' => 'Märch',
'ascii' => 'ascii',
'space' => ' ',
Expand Down

0 comments on commit f7b5914

Please sign in to comment.