Skip to content

Commit

Permalink
support arity 0
Browse files Browse the repository at this point in the history
  • Loading branch information
mensfeld committed Oct 25, 2023
1 parent c64535f commit d92a216
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### 2.2.4 (Unreleased)
- [Enhancement] Allow for `lazy` evaluated constructors.
- [Enhancement] Allow no-arg constructors.

### 2.2.3 (2023-10-17)
- [Change] Set minimum `karafka-rdkafka` on `0.13.6`.
Expand Down
18 changes: 16 additions & 2 deletions lib/karafka/core/configurable/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def compile
if value.constructor && value.lazy?
false
elsif value.constructor
value.constructor.call(value.default)
call_constructor(value)
else
value.default
end
Expand Down Expand Up @@ -146,11 +146,25 @@ def build_dynamic_accessor(value)

return existing if existing

built = value.constructor.call(value.default)
built = call_constructor(value)

instance_variable_set("@#{value.name}", built)
end
end

# Runs the constructor with or without the default depending on its arity and returns the
# result
#
# @param value [Leaf]
def call_constructor(value)
constructor = value.constructor

if constructor.arity.zero?
constructor.call
else
constructor.call(value.default)
end
end
end
end
end
Expand Down
47 changes: 43 additions & 4 deletions spec/lib/karafka/core/configurable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
setting(:leaf, default: 6)
setting(:with_constructor, default: false, constructor: ->(default) { default || 5 })
setting(:ov_constructor, default: true, constructor: ->(default) { default || 5 })
setting(:with_zero_constructor, constructor: -> { 7 })
end

setting(:nested1, default: 1)
Expand All @@ -36,6 +37,7 @@
it { expect(config.nested1.nested1).to eq(1) }
it { expect(config.nested1.nested2.with_constructor).to eq(5) }
it { expect(config.nested1.nested2.ov_constructor).to eq(true) }
it { expect(config.nested1.nested2.with_zero_constructor).to eq(7) }
end

context 'when we do override some settings' do
Expand Down Expand Up @@ -114,13 +116,25 @@
end

describe '#to_h' do
let(:expected_hash) do
{
with_default: 123,
nested1: {
nested1: 1,
nested2: {
leaf: 6,
ov_constructor: true,
with_constructor: 5,
with_zero_constructor: 7
}
}
}
end

before { config.configure }

it 'expect to map with correct values' do
expect(config.to_h).to eq(
with_default: 123,
nested1: { nested1: 1, nested2: { leaf: 6, ov_constructor: true, with_constructor: 5 } }
)
expect(config.to_h).to eq(expected_hash)
end
end

Expand Down Expand Up @@ -192,6 +206,31 @@
end
end

context 'when constructor changes and its zero arity' do
let(:configurable_class) do
constructor1 = constructor

Class.new do
extend Karafka::Core::Configurable

setting(
:lazy_setting,
constructor: constructor1,
lazy: true
)
end
end

let(:attempts) { [1, 10, false, false, false] }
let(:constructor) { -> { attempts.pop } }

it 'expect to retry until non-false is present and then cache it' do
3.times { expect(config.lazy_setting).to eq(false) }
expect(config.lazy_setting).to eq(10)
expect(config.lazy_setting).to eq(10)
end
end

context 'when we want to overwrite constructed state with a different one during config' do
let(:default) { false }
let(:constructor) { ->(_) { false } }
Expand Down

0 comments on commit d92a216

Please sign in to comment.