-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Conor Evans <[email protected]>
- Loading branch information
1 parent
f03611d
commit a6623bc
Showing
4 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'fluent/config' | ||
|
||
module Fluent | ||
module EnvUtils | ||
def self.convert_env_var_to_type(value, type) | ||
Fluent::Config.reformatted_value(type, value) | ||
end | ||
|
||
class OjOptions | ||
OJ_OPTIONS = { | ||
'bigdecimal_load': :symbol, | ||
'max_nesting': :integer, | ||
'mode': :symbol, | ||
'use_to_json': :bool | ||
} | ||
|
||
OJ_OPTIONS_ALLOWED_VALUES = { | ||
'bigdecimal_load': %i[bigdecimal float auto], | ||
'mode': %i[strict null concat json rails object custom] | ||
} | ||
|
||
OJ_OPTIONS_DEFAULTS = { | ||
'bigdecimal_load': :float, | ||
'mode': :concat, | ||
'use_to_json': true | ||
} | ||
|
||
def initialize | ||
@options = {} | ||
OJ_OPTIONS_DEFAULTS.each { |key, value| @options[key] = value } | ||
end | ||
|
||
def get_options | ||
OJ_OPTIONS.each do |key, type| | ||
env_value = ENV["FLUENT_OJ_OPTION_#{key.upcase}"] | ||
next if env_value.nil? | ||
|
||
cast_value = Fluent::EnvUtils.convert_env_var_to_type(env_value, OJ_OPTIONS[key]) | ||
next if cast_value.nil? | ||
|
||
next if OJ_OPTIONS_ALLOWED_VALUES[key] && !OJ_OPTIONS_ALLOWED_VALUES[key].include?(cast_value) | ||
|
||
@options[key.to_sym] = cast_value | ||
end | ||
|
||
@options | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require_relative 'helper' | ||
require 'fluent/test' | ||
require 'fluent/env_utils' | ||
|
||
class EnvUtilsTest < ::Test::Unit::TestCase | ||
setup do | ||
@oj = Fluent::EnvUtils::OjOptions.new | ||
end | ||
|
||
sub_test_case "module methods" do | ||
test "convert_env_var_to_type" do | ||
assert_equal :conor, Fluent::EnvUtils.convert_env_var_to_type("conor", :symbol) | ||
assert_equal 8933, Fluent::EnvUtils.convert_env_var_to_type("8933", :integer) | ||
assert_equal nil, Fluent::EnvUtils.convert_env_var_to_type("conor", :integer) | ||
assert_equal true, Fluent::EnvUtils.convert_env_var_to_type("true", :bool) | ||
assert_equal nil, Fluent::EnvUtils.convert_env_var_to_type("conor", :bool) | ||
end | ||
end | ||
|
||
sub_test_case "OjOptions" do | ||
test "when no env vars set, returns default options" do | ||
ENV.clear | ||
assert_equal Fluent::EnvUtils::OjOptions::OJ_OPTIONS_DEFAULTS, @oj.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] | ||
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] | ||
end | ||
|
||
test "invalid env var passed, nothing done with it" do | ||
ENV["FLUENT_OJ_OPTION_CONOR"] = ":conor" | ||
assert_equal nil, @oj.get_options[:conor] | ||
end | ||
end | ||
end |