-
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.
Merge pull request #3315 from conorevans/conorevans/allow-for-configu…
…ration-of-oj-variables Allow for configuration of OJ options
- Loading branch information
Showing
8 changed files
with
154 additions
and
13 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,62 @@ | ||
require 'fluent/config/types' | ||
|
||
module Fluent | ||
class OjOptions | ||
OPTIONS = { | ||
'bigdecimal_load': :symbol, | ||
'max_nesting': :integer, | ||
'mode': :symbol, | ||
'use_to_json': :bool | ||
} | ||
|
||
ALLOWED_VALUES = { | ||
'bigdecimal_load': %i[bigdecimal float auto], | ||
'mode': %i[strict null compat json rails object custom] | ||
} | ||
|
||
DEFAULTS = { | ||
'bigdecimal_load': :float, | ||
'mode': :compat, | ||
'use_to_json': true | ||
} | ||
|
||
@@available = false | ||
|
||
def self.available? | ||
@@available | ||
end | ||
|
||
def self.load_env | ||
options = self.get_options | ||
begin | ||
require 'oj' | ||
Oj.default_options = options | ||
@@available = true | ||
rescue LoadError | ||
@@available = false | ||
end | ||
options | ||
end | ||
|
||
private | ||
|
||
def self.get_options | ||
options = {} | ||
DEFAULTS.each { |key, value| options[key] = value } | ||
|
||
OPTIONS.each do |key, type| | ||
env_value = ENV["FLUENT_OJ_OPTION_#{key.upcase}"] | ||
next if env_value.nil? | ||
|
||
cast_value = Fluent::Config.reformatted_value(OPTIONS[key], env_value, { strict: true }) | ||
next if cast_value.nil? | ||
|
||
next if ALLOWED_VALUES[key] && !ALLOWED_VALUES[key].include?(cast_value) | ||
|
||
options[key.to_sym] = cast_value | ||
end | ||
|
||
options | ||
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
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
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,55 @@ | ||
require_relative 'helper' | ||
require 'fluent/test' | ||
require 'fluent/oj_options' | ||
|
||
class OjOptionsTest < ::Test::Unit::TestCase | ||
begin | ||
require 'oj' | ||
@@oj_is_avaibale = true | ||
rescue LoadError | ||
@@oj_is_avaibale = false | ||
end | ||
|
||
setup do | ||
@orig_env = {} | ||
ENV.each do |key, value| | ||
@orig_env[key] = value if key.start_with?("FLUENT_OJ_OPTION_") | ||
end | ||
end | ||
|
||
teardown do | ||
ENV.delete_if { |key| key.start_with?("FLUENT_OJ_OPTION_") } | ||
@orig_env.each { |key, value| ENV[key] = value } | ||
end | ||
|
||
test "available?" do | ||
assert_equal(@@oj_is_avaibale, Fluent::OjOptions.available?) | ||
end | ||
|
||
sub_test_case "set by environment variable" do | ||
test "when no env vars set, returns default options" do | ||
ENV.delete_if { |key| key.start_with?("FLUENT_OJ_OPTION_") } | ||
defaults = Fluent::OjOptions::DEFAULTS | ||
assert_equal(defaults, Fluent::OjOptions.load_env) | ||
assert_equal(defaults, Oj.default_options.slice(*defaults.keys)) if @@oj_is_avaibale | ||
end | ||
|
||
test "valid env var passed with valid value, default is overridden" do | ||
ENV["FLUENT_OJ_OPTION_BIGDECIMAL_LOAD"] = ":bigdecimal" | ||
assert_equal(:bigdecimal, Fluent::OjOptions.load_env[:bigdecimal_load]) | ||
assert_equal(:bigdecimal, Oj.default_options[:bigdecimal_load]) if @@oj_is_avaibale | ||
end | ||
|
||
test "valid env var passed with invalid value, default is not overriden" do | ||
ENV["FLUENT_OJ_OPTION_BIGDECIMAL_LOAD"] = ":conor" | ||
assert_equal(:float, Fluent::OjOptions.load_env[:bigdecimal_load]) | ||
assert_equal(:float, Oj.default_options[:bigdecimal_load]) if @@oj_is_avaibale | ||
end | ||
|
||
test "invalid env var passed, nothing done with it" do | ||
ENV["FLUENT_OJ_OPTION_CONOR"] = ":conor" | ||
assert_equal(nil, Fluent::OjOptions.load_env[:conor]) | ||
assert_equal(nil, Oj.default_options[:conor]) if @@oj_is_avaibale | ||
end | ||
end | ||
end |