Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'type' to OptionDefinition #2493

Merged
merged 2 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/datadog/core/configuration/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ def settings(name, &block)
option(name) do |o|
o.default { settings_class.new }
o.lazy

o.resetter do |value|
value.reset! if value.respond_to?(:reset!)
value
end

o.type settings_class
end
end

Expand Down
13 changes: 11 additions & 2 deletions lib/datadog/core/configuration/option_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class OptionDefinition
:name,
:on_set,
:resetter,
:setter
:setter,
:type

def initialize(name, meta = {}, &block)
@default = meta[:default]
Expand All @@ -28,6 +29,7 @@ def initialize(name, meta = {}, &block)
@on_set = meta[:on_set]
@resetter = meta[:resetter]
@setter = meta[:setter] || block || IDENTITY
@type = meta[:type]
end

# Creates a new Option, bound to the context provided.
Expand All @@ -51,6 +53,7 @@ def initialize(name, options = {})
@on_set = nil
@resetter = nil
@setter = OptionDefinition::IDENTITY
@type = nil

# If options were supplied, apply them.
apply_options!(options)
Expand Down Expand Up @@ -91,6 +94,10 @@ def setter(&block)
@setter = block
end

def type(value = nil)
@type = value
end

# For applying options for OptionDefinition
def apply_options!(options = {})
return if options.nil? || options.empty?
Expand All @@ -102,6 +109,7 @@ def apply_options!(options = {})
on_set(&options[:on_set]) if options.key?(:on_set)
resetter(&options[:resetter]) if options.key?(:resetter)
setter(&options[:setter]) if options.key?(:setter)
type(&options[:type]) if options.key?(:type)
end

def to_definition
Expand All @@ -116,7 +124,8 @@ def meta
lazy: @lazy,
on_set: @on_set,
resetter: @resetter,
setter: @setter
setter: @setter,
type: @type
}
end
end
Expand Down
16 changes: 14 additions & 2 deletions spec/datadog/core/configuration/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,23 @@

it { is_expected.to be_a_kind_of(Datadog::Core::Configuration::OptionDefinition) }

it 'sets default properties' do
expect(definition.type).to be_a_kind_of(Class)
expect(definition.type.ancestors).to include(described_class)

is_expected.to have_attributes(
default: kind_of(Proc),
lazy: true,
resetter: kind_of(Proc)
)
end

describe 'when instantiated' do
subject(:option) { Datadog::Core::Configuration::Option.new(definition, self) }
let(:settings_object) { option.default_value }

it { expect(option.default_value).to be_a_kind_of(described_class) }
it { expect(option.default_value.option_defined?(:enabled)).to be true }
it { expect(settings_object).to be_a_kind_of(described_class) }
it { expect(settings_object.option_defined?(:enabled)).to be true }
end
end
end
Expand Down
34 changes: 32 additions & 2 deletions spec/datadog/core/configuration/option_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@
end
end

describe '#type' do
subject(:type) { definition.type }

context 'when given a value' do
let(:meta) { { type: type_value } }
let(:type_value) { double('type') }

it { is_expected.to be type_value }
end

context 'when not initialized' do
it { is_expected.to be nil }
end
end

describe '#build' do
subject(:build) { definition.build(context) }

Expand Down Expand Up @@ -184,7 +199,8 @@
name: name,
on_set: nil,
resetter: nil,
setter: Datadog::Core::Configuration::OptionDefinition::IDENTITY
setter: Datadog::Core::Configuration::OptionDefinition::IDENTITY,
type: nil
)
end
end
Expand Down Expand Up @@ -329,6 +345,19 @@
it { is_expected.to be block }
end

describe '#type' do
subject(:type) { builder.type(value) }

let(:value) { nil }

context 'given a value' do
let(:value) { String }

it { is_expected.to be value }
it { expect { type }.to change { builder.meta[:type] }.from(nil).to(value) }
end
end

describe '#apply_options!' do
subject(:apply_options!) { builder.apply_options!(options) }

Expand Down Expand Up @@ -444,7 +473,8 @@
:lazy,
:on_set,
:resetter,
:setter
:setter,
:type
)
end
end
Expand Down