Skip to content

Commit

Permalink
remove variable helpers module from our configuration DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoCaso committed Sep 24, 2023
1 parent cb5fe8a commit 31ee55a
Show file tree
Hide file tree
Showing 7 changed files with 3 additions and 267 deletions.
3 changes: 0 additions & 3 deletions lib/datadog/core/configuration/base.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require_relative '../environment/variable_helpers'
require_relative 'options'

module Datadog
Expand All @@ -8,8 +7,6 @@ module Configuration
# @public_api
module Base
def self.included(base)
base.extend(Core::Environment::VariableHelpers)
base.include(Core::Environment::VariableHelpers)
base.include(Options)

base.extend(ClassMethods)
Expand Down
69 changes: 0 additions & 69 deletions lib/datadog/core/environment/variable_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,75 +29,6 @@ def env_to_bool(var, default = nil, deprecation_warning: true)
end
end

# Reads an environment variable as an Integer.
#
# @param [String] var environment variable
# @param [Array<String>] var list of environment variables
# @param [Integer] 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 [Integer] if the environment value is a valid Integer
# @return [default] if the environment value is not found
def env_to_int(var, default = nil, deprecation_warning: true)
var = decode_array(var, deprecation_warning)
var && ENV.key?(var) ? ENV[var].to_i : default
end

# Reads an environment variable as a Float.
#
# @param [String] var environment variable
# @param [Array<String>] var list of environment variables
# @param [Float] 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 [Float] if the environment value is a valid Float
# @return [default] if the environment value is not found
def env_to_float(var, default = nil, deprecation_warning: true)
var = decode_array(var, deprecation_warning)
var && ENV.key?(var) ? ENV[var].to_f : default
end

# Parses comma- or space-separated lists.
#
# If a comma is present, then the list is considered comma-separated.
# Otherwise, it is considered space-separated.
#
# After the entries are separated, commas and whitespaces that are
# either trailing or leading are trimmed.
#
# Empty entries, after trimmed, are also removed from the result.
#
# @param [String] var environment variable
# @param [Array<String>] var list of environment variables
# @param [Array<Object>] 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 [Array<Object>] if the environment value is a valid list
# @return [default] if the environment value is not found
def env_to_list(var, default = [], comma_separated_only:, deprecation_warning: true)
var = decode_array(var, deprecation_warning)
if var && ENV.key?(var)
value = ENV[var]

values = if value.include?(',') || comma_separated_only
value.split(',')
else
value.split(' ') # rubocop:disable Style/RedundantArgument
end

values.map! do |v|
v.gsub!(/\A[\s,]*|[\s,]*\Z/, '')

v.empty? ? nil : v
end

values.compact!
values
else
default
end
end

private

def decode_array(var, deprecation_warning)
Expand Down
5 changes: 3 additions & 2 deletions lib/datadog/tracing/configuration/settings.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative '../../tracing/configuration/ext'
require_relative '../environment/variable_helpers'
require_relative 'http'

module Datadog
Expand Down Expand Up @@ -425,7 +426,7 @@ def self.extended(base)
option :enabled do |o|
o.type :bool
o.default do
disabled = env_to_bool(Tracing::Configuration::Ext::ClientIp::ENV_DISABLED)
disabled = Core::Environment::VariableHelpers.env_to_bool(Tracing::Configuration::Ext::ClientIp::ENV_DISABLED)

enabled = if disabled.nil?
false
Expand All @@ -438,7 +439,7 @@ def self.extended(base)
end

# ENABLED env var takes precedence over deprecated DISABLED
env_to_bool(Tracing::Configuration::Ext::ClientIp::ENV_ENABLED, enabled)
Core::Environment::VariableHelpers.env_to_bool(Tracing::Configuration::Ext::ClientIp::ENV_ENABLED, enabled)
end
end

Expand Down
1 change: 0 additions & 1 deletion sig/datadog/appsec/configuration/settings.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module Datadog
module Configuration
# Settings
module Settings
extend Datadog::Core::Environment::VariableHelpers
extend Datadog::Core::Configuration::Base::ClassMethods
include Datadog::Core::Configuration::Base::InstanceMethods
extend Datadog::Core::Configuration::Options::ClassMethods
Expand Down
6 changes: 0 additions & 6 deletions sig/datadog/core/environment/variable_helpers.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ module Datadog

def env_to_bool: (untyped var, ?untyped? default) -> untyped

def env_to_int: (untyped var, ?untyped? default) -> untyped

def env_to_float: (untyped var, ?untyped? default) -> untyped

def env_to_list: (untyped var, ?untyped default) -> untyped

private

def decode_array: (untyped var) -> untyped
Expand Down
2 changes: 0 additions & 2 deletions spec/datadog/core/configuration/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
describe 'instance behavior' do
subject(:base_object) { base_class.new }

it { is_expected.to be_a_kind_of(Datadog::Core::Environment::VariableHelpers) }

describe '#initialize' do
subject(:base_object) { base_class.new(options) }

Expand Down
184 changes: 0 additions & 184 deletions spec/datadog/core/environment/variable_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,188 +100,4 @@
include_context 'with deprecated options'
end
end

describe '::env_to_int' do
subject(:env_to_int) { variable_helpers.env_to_int(var, **options) }

context 'when env var is not defined' do
context 'and default is not defined' do
it { is_expected.to be nil }
end

context 'and default is defined' do
subject(:env_to_int) { variable_helpers.env_to_int(var, default) }

let(:default) { double }

it { is_expected.to be default }
end
end

context 'when env var is set as' do
include_context 'env var'

context '0' do
let(:env_value) { '0' }

it { is_expected.to eq 0 }
end

context '1' do
let(:env_value) { '1' }

it { is_expected.to eq 1 }
end

context '1.5' do
let(:env_value) { '1.5' }

it { is_expected.to eq 1 }
end

context 'test' do
let(:env_value) { 'test' }

it { is_expected.to eq 0 }
end

include_context 'with deprecated options'
end
end

describe '::env_to_float' do
subject(:env_to_float) { variable_helpers.env_to_float(var, **options) }

context 'when env var is not defined' do
context 'and default is not defined' do
it { is_expected.to be nil }
end

context 'and default is defined' do
subject(:env_to_float) { variable_helpers.env_to_float(var, default) }

let(:default) { double }

it { is_expected.to be default }
end
end

context 'when env var is set as' do
include_context 'env var'

context '0' do
let(:env_value) { '0' }

it { is_expected.to eq 0.0 }
end

context '1' do
let(:env_value) { '1' }

it { is_expected.to eq 1.0 }
end

context '1.5' do
let(:env_value) { '1.5' }

it { is_expected.to eq 1.5 }
end

context 'test' do
let(:env_value) { 'test' }

it { is_expected.to eq 0.0 }
end

include_context 'with deprecated options'
end
end

describe '::env_to_list' do
subject(:env_to_list) { variable_helpers.env_to_list(var, comma_separated_only: false, **options) }

context 'when env var is not defined' do
context 'and default is not defined' do
it { is_expected.to eq([]) }
end

context 'and default is defined' do
subject(:env_to_list) { variable_helpers.env_to_list(var, default, comma_separated_only: false) }

let(:default) { double }

it { is_expected.to be default }
end
end

context 'when env var is set' do
include_context 'env var'

context '\'\'' do
let(:env_value) { '' }

it { is_expected.to eq([]) }
end

context ',' do
let(:env_value) { ',' }

it { is_expected.to eq([]) }
end

context '1' do
let(:env_value) { '1' }

it { is_expected.to eq(['1']) }
end

context '1,2' do
let(:env_value) { '1,2' }

it { is_expected.to eq(%w[1 2]) }
end

context ' 1 , 2 , 3 ' do
let(:env_value) { ' 1 , 2 , 3 ' }

it { is_expected.to eq(%w[1 2 3]) }
end

context '1 2 3' do
let(:env_value) { '1 2 3' }

it { is_expected.to eq(%w[1 2 3]) }
end

context '1,2 3' do
let(:env_value) { '1,2 3' }

it { is_expected.to eq(['1', '2 3']) }
end

context ' 1 2 3 ' do
let(:env_value) { ' 1 2 3 ' }

it { is_expected.to eq(%w[1 2 3]) }
end

context '1,, ,2,3,' do
let(:env_value) { '1,, ,2,3,' }

it { is_expected.to eq(%w[1 2 3]) }
end

context 'and comma_separated_only is set' do
subject(:env_to_list) { variable_helpers.env_to_list(var, comma_separated_only: true) }

context 'value with space' do
let(:env_value) { 'value with space' }

it { is_expected.to eq(['value with space']) }
end
end

include_context 'with deprecated options'
end
end
end

0 comments on commit 31ee55a

Please sign in to comment.