Skip to content

Commit

Permalink
Adds method for get_treatment_with_config, see comment: #83 (comment)
Browse files Browse the repository at this point in the history
- There are multiple use cases in Hyperion of SplitIO's method where it returns both the treatment and it's configuration. This commit adds methods to retrieve these values
- Not exactly related, but I see in SplitIO's docs there is a `get_treatments_with_config` (plural) method. We're not using this in Hyperion yet, that I can see, but I wonder if it would be useful down the line. I don't typically suggest to add functionality that won't be used yet, but it;s clear that for a year or so, the ThisFeature gem was not yet capable to handle our needs, which is why the antipattern use of ThisFeature (`ThisFeature.adapter_for(:split)&.send(:client)`) has been committed many times into Hyperion. I'm unsure of why a change to ThisFeature wasn't done first.
  • Loading branch information
veeweeherman committed Oct 10, 2023
1 parent c50010a commit 23a7536
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
13 changes: 11 additions & 2 deletions lib/this_feature/adapters/split_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ def off?(flag_name, context: EMPTY_CONTEXT, data: {}, record: nil)
treatment(flag_name, context: context, data: data, record: record).eql?('off')
end

def get_treatment(flag_name, context: EMPTY_CONTEXT, data: {}, record: nil)
treatment(flag_name, context: context, data: data, record: record)
def treatment_value(flag_name, context: EMPTY_CONTEXT, data: {}, record: nil)
treatment_with_config(flag_name, context: context, data: data, record: record)[:treatment]
end

def treatment_config(flag_name, context: EMPTY_CONTEXT, data: {}, record: nil)
treatment_with_config(flag_name, context: context, data: data, record: record)[:config]
end

private
Expand All @@ -43,6 +47,11 @@ def treatment(flag_name, context: EMPTY_CONTEXT, data: {}, record: nil)
client.get_treatment(context_key(context), flag_name, base_data.merge(data))
end

def treatment_with_config(flag_name, context: EMPTY_CONTEXT, data: {}, record: nil)
base_data = record ? ThisFeature.base_data_lambda.call(record) : {}
client.get_treatment_with_config(context_key(context), flag_name, base_data.merge(data))
end

def context_key(context)
return EMPTY_CONTEXT if context.nil? || context.eql?(EMPTY_CONTEXT)
return context.send(context_key_method) unless context_key_method.nil?
Expand Down
8 changes: 6 additions & 2 deletions lib/this_feature/flag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ def control?
adapter.control?(flag_name, context: context, data: data, record: record)
end

def get_treatment
adapter.get_treatment(flag_name, context: context, data: data, record: record)
def treatment_value
adapter.treatment_value(flag_name, context: context, data: data, record: record)
end

def treatment_config
adapter.treatment_config(flag_name, context: context, data: data, record: record)
end
end
end
10 changes: 6 additions & 4 deletions spec/support/files/splits.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
- treatment_feature:
treatment: 'v1'
keys: '1'
config: {'desc': 'this applies only to when there are many treatments'}
config:
plan_id: 1

- treatment_feature:
treatment: 'v2'
keys: '2'
config: {'desc': 'this applies only to when there are many treatments'}
treatment: 'v2'
keys: '2'
config:
plan_id: 2

20 changes: 14 additions & 6 deletions spec/this_feature/adapters/split_io_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,27 @@

context 'when org id is 1' do
it 'returns the right treatment' do
expect(ThisFeature.flag(flag_name).get_treatment).to eq 'control_treatment'
expect(ThisFeature.flag(flag_name, context: org_id).get_treatment).to eq 'v1'
expect(ThisFeature.flag(flag_name, context: org_id, data: { a: :b }).get_treatment).to eq 'v1'
expect(ThisFeature.flag(flag_name).treatment_value).to eq 'control_treatment'
expect(ThisFeature.flag(flag_name, context: org_id).treatment_value).to eq 'v1'

this_feature = ThisFeature.flag(flag_name, context: org_id, data: { a: :b })

expect(this_feature.treatment_value).to eq 'v1'
expect(JSON.parse(this_feature.treatment_config)).to eq({ 'plan_id' => 1 })
end
end

context 'when org id is 2' do
let(:org_id) { 2 }

it 'returns the right treatment' do
expect(ThisFeature.flag(flag_name).get_treatment).to eq 'control_treatment'
expect(ThisFeature.flag(flag_name, context: org_id).get_treatment).to eq 'v2'
expect(ThisFeature.flag(flag_name, context: org_id, data: { a: :b }).get_treatment).to eq 'v2'
expect(ThisFeature.flag(flag_name).treatment_value).to eq 'control_treatment'
expect(ThisFeature.flag(flag_name, context: org_id).treatment_value).to eq 'v2'

this_feature = ThisFeature.flag(flag_name, context: org_id, data: { a: :b })

expect(this_feature.treatment_value).to eq 'v2'
expect(JSON.parse(this_feature.treatment_config)).to eq({ 'plan_id' => 2 })
end
end
end
Expand Down

0 comments on commit 23a7536

Please sign in to comment.