Skip to content

Commit

Permalink
Merge pull request #2710 from DataDog/bool-flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
marcotc authored Mar 24, 2023
2 parents e4a146b + bf40243 commit 9880f39
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
12 changes: 10 additions & 2 deletions lib/datadog/core/environment/variable_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Datadog
module Core
# Namespace for handling application environment
Expand All @@ -14,11 +16,17 @@ module VariableHelpers
# @param [Boolean] default the default value if the keys in `var` are not present in the environment
# @param [Boolean] deprecation_warning when `var` is a list, record a deprecation log when
# the first key in `var` is not used.
# @return [Boolean] if the environment value is the string `true`
# @return [Boolean] if the environment value is the string `true` or `1`
# @return [default] if the environment value is not found
def env_to_bool(var, default = nil, deprecation_warning: true)
var = decode_array(var, deprecation_warning)
var && ENV.key?(var) ? ENV[var].to_s.strip.downcase == 'true' : default
if var && ENV.key?(var)
value = ENV[var].to_s.strip
value.downcase!
value == 'true' || value == '1' # rubocop:disable Style/MultipleComparison
else
default
end
end

# Reads an environment variable as an Integer.
Expand Down
20 changes: 11 additions & 9 deletions spec/datadog/core/environment/variable_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@
include_context 'env var'

# True values
%w[
true
TRUE
[
'true',
'TRUE',
'1',
' 1 ',
].each do |value|
context value.to_s do
let(:env_value) { value.to_s }
context "'#{value}'" do
let(:env_value) { value }

it { is_expected.to be true }
end
Expand All @@ -85,11 +87,11 @@
'',
'false',
'FALSE',
0,
1
'0',
'arbitrary string',
].each do |value|
context value.to_s do
let(:env_value) { value.to_s }
context "'#{value}'" do
let(:env_value) { value }

it { is_expected.to be false }
end
Expand Down

0 comments on commit 9880f39

Please sign in to comment.