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

feat: dependant toggles #155

Merged
merged 6 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
- name: Install dependencies
run: bundle install
- name: Download test cases
run: git clone --depth 5 --branch v4.3.3 https://github.com/Unleash/client-specification.git client-specification
run: git clone --depth 5 --branch v4.5.1 https://github.com/Unleash/client-specification.git client-specification
- name: Run tests
run: bundle exec rake
env:
Expand Down
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Metrics/ClassLength:
Max: 135
CountAsOne:
- 'method_call'
Exclude:
- 'lib/unleash/feature_toggle.rb'
Layout/LineLength:
Max: 140
Metrics/MethodLength:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ You can also run `bin/console` for an interactive prompt that will allow you to
This SDK is also built against the Unleash Client Specification tests.
To run the Ruby SDK against this test suite, you'll need to have a copy on your machine, you can clone the repository directly using:

`git clone --depth 5 --branch v4.3.3 https://github.com/Unleash/client-specification.git client-specification`
`git clone --depth 5 --branch v4.5.1 https://github.com/Unleash/client-specification.git client-specification`

After doing this, `rake spec` will also run the client specification tests.

Expand Down
1 change: 0 additions & 1 deletion lib/unleash/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def is_enabled?(feature, context = nil, default_value_param = false, &fallback_b
end

toggle_as_hash = Unleash&.toggles&.select{ |toggle| toggle['name'] == feature }&.first

if toggle_as_hash.nil?
Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} not found"
return default_value
Expand Down
2 changes: 1 addition & 1 deletion lib/unleash/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def http_headers
{
'UNLEASH-INSTANCEID' => self.instance_id,
'UNLEASH-APPNAME' => self.app_name,
'Unleash-Client-Spec' => '4.2.2'
'Unleash-Client-Spec' => '4.5.1'
}.merge!(generate_custom_http_headers)
end

Expand Down
45 changes: 42 additions & 3 deletions lib/unleash/feature_toggle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

module Unleash
class FeatureToggle
attr_accessor :name, :enabled, :strategies, :variant_definitions
attr_accessor :name, :enabled, :dependencies, :strategies, :variant_definitions

FeatureEvaluationResult = Struct.new(:enabled?, :strategy)

Expand All @@ -16,6 +16,7 @@ def initialize(params = {}, segment_map = {})

self.name = params.fetch('name', nil)
self.enabled = params.fetch('enabled', false)
self.dependencies = params.fetch('dependencies', [])

self.strategies = initialize_strategies(params, segment_map)
self.variant_definitions = initialize_variant_definitions(params)
Expand Down Expand Up @@ -75,9 +76,48 @@ def am_enabled?(context)
evaluate(context).enabled?
end

def parent_dependencies_satisfied?(context)
gardleopard marked this conversation as resolved.
Show resolved Hide resolved
return true if dependencies.empty?

dependencies.all? do |parent|
evaluate_parenthood(parent, context)
end
end

def evaluate_parenthood(parent, context)
gardleopard marked this conversation as resolved.
Show resolved Hide resolved
parent_toggle = get_parent(parent["feature"])

return false if parent_toggle.nil?

return false unless parent_toggle.dependencies.empty?

evaluation_result = parent_toggle.is_enabled?(context)
return !evaluation_result if parent["enabled"] == false
rarruda marked this conversation as resolved.
Show resolved Hide resolved

return false unless evaluation_result

unless parent["variants"].nil? || parent["variants"].empty?
return parent["variants"].include?(parent_toggle.get_variant(context).name)
end

evaluation_result
end

def get_parent(feature)
toggle_as_hash = Unleash&.toggles&.find{ |toggle| toggle['name'] == feature }
if toggle_as_hash.nil?
Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} not found"
return nil
end

Unleash::FeatureToggle.new(toggle_as_hash, Unleash&.segment_cache)
end

def evaluate(context)
evaluation_result =
if !self.enabled
if !parent_dependencies_satisfied?(context)
FeatureEvaluationResult.new(false, nil)
elsif !self.enabled
FeatureEvaluationResult.new(false, nil)
elsif self.strategies.empty?
FeatureEvaluationResult.new(true, nil)
Expand All @@ -88,7 +128,6 @@ def evaluate(context)

Unleash.logger.debug "Unleash::FeatureToggle (enabled:#{self.enabled}) " \
"and Strategies combined with constraints returned #{evaluation_result})"

evaluation_result
end

Expand Down
6 changes: 3 additions & 3 deletions spec/unleash/client_specification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@
variant_tests = current_test_set.fetch('variantTests', [])
variant_tests.each do |test|
it "test that #{test['description']}" do
test_toggle = unleash_toggles.select{ |t| t.fetch('name', '') == test.fetch('toggleName') }.first
Unleash.toggles = unleash_toggles
Unleash.segment_cache = state_segments

toggle = Unleash::FeatureToggle.new(test_toggle, state_segments)
context = Unleash::Context.new(test['context'])

variant = toggle.get_variant(context, DEFAULT_VARIANT)
variant = unleash_client.get_variant(test.fetch('toggleName'), context)

expect(variant).to eq(Unleash::Variant.new(test['expectedResult']))
end
Expand Down
2 changes: 1 addition & 1 deletion spec/unleash/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
'X-API-KEY' => '123',
'UNLEASH-APPNAME' => 'test-app',
'UNLEASH-INSTANCEID' => config.instance_id,
'Unleash-Client-Spec' => '4.2.2'
'Unleash-Client-Spec' => '4.5.1'
}
)
expect(custom_headers_proc).to have_received(:call).exactly(1).times
Expand Down