Skip to content

Commit

Permalink
OjOptions: Remove needless instantiation
Browse files Browse the repository at this point in the history
Signed-off-by: Takuro Ashie <[email protected]>
  • Loading branch information
ashie committed Jul 8, 2021
1 parent 54e0cc2 commit beae9f7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/fluent/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Fluent
DEFAULT_PLUGIN_DIR = ENV['FLUENT_PLUGIN'] || '/etc/fluent/plugin'
DEFAULT_SOCKET_PATH = ENV['FLUENT_SOCKET'] || '/var/run/fluent/fluent.sock'
DEFAULT_BACKUP_DIR = ENV['FLUENT_BACKUP_DIR'] || '/tmp/fluent'
DEFAULT_OJ_OPTIONS = Fluent::OjOptions.new.get_options
DEFAULT_OJ_OPTIONS = Fluent::OjOptions.get_options
DEFAULT_DIR_PERMISSION = 0755
DEFAULT_FILE_PERMISSION = 0644

Expand Down
12 changes: 5 additions & 7 deletions lib/fluent/oj_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ class OjOptions
'use_to_json': true
}

def initialize
@options = {}
DEFAULTS.each { |key, value| @options[key] = value }
end
def self.get_options
options = {}
DEFAULTS.each { |key, value| options[key] = value }

def get_options
OPTIONS.each do |key, type|
env_value = ENV["FLUENT_OJ_OPTION_#{key.upcase}"]
next if env_value.nil?
Expand All @@ -35,10 +33,10 @@ def get_options

next if ALLOWED_VALUES[key] && !ALLOWED_VALUES[key].include?(cast_value)

@options[key.to_sym] = cast_value
options[key.to_sym] = cast_value
end

@options
options
end
end
end
9 changes: 4 additions & 5 deletions test/test_oj_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class OjOptionsTest < ::Test::Unit::TestCase
setup do
@oj = Fluent::OjOptions.new
@orig_env = {}
ENV.each do |key, value|
@orig_env[key] = value if key.start_with?("FLUENT_OJ_OPTION_")
Expand All @@ -19,22 +18,22 @@ class OjOptionsTest < ::Test::Unit::TestCase
sub_test_case "OjOptions" do
test "when no env vars set, returns default options" do
ENV.delete_if { |key| key.start_with?("FLUENT_OJ_OPTION_") }
assert_equal Fluent::OjOptions::DEFAULTS, @oj.get_options
assert_equal Fluent::OjOptions::DEFAULTS, Fluent::OjOptions.get_options
end

test "valid env var passed with valid value, default is overridden" do
ENV["FLUENT_OJ_OPTION_BIGDECIMAL_LOAD"] = ":bigdecimal"
assert_equal :bigdecimal, @oj.get_options[:bigdecimal_load]
assert_equal :bigdecimal, Fluent::OjOptions.get_options[:bigdecimal_load]
end

test "valid env var passed with invalid value, default is not overriden" do
ENV["FLUENT_OJ_OPTION_BIGDECIMAL_LOAD"] = ":conor"
assert_equal :float, @oj.get_options[:bigdecimal_load]
assert_equal :float, Fluent::OjOptions.get_options[:bigdecimal_load]
end

test "invalid env var passed, nothing done with it" do
ENV["FLUENT_OJ_OPTION_CONOR"] = ":conor"
assert_equal nil, @oj.get_options[:conor]
assert_equal nil, Fluent::OjOptions.get_options[:conor]
end
end
end

0 comments on commit beae9f7

Please sign in to comment.