-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
oj_options.rb
62 lines (49 loc) · 1.26 KB
/
oj_options.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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 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