-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Add
EvaluationContext
helpers and context merging to flag ev…
…aluation (#119) Signed-off-by: Max VelDink <[email protected]>
- Loading branch information
1 parent
6895a0d
commit 34e4795
Showing
10 changed files
with
203 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module OpenFeature | ||
module SDK | ||
# Used to combine evaluation contexts from different sources | ||
class EvaluationContextBuilder | ||
def call(api_context:, client_context:, invocation_context:) | ||
available_contexts = [api_context, client_context, invocation_context].compact | ||
|
||
return nil if available_contexts.empty? | ||
|
||
available_contexts.reduce(EvaluationContext.new) do |built_context, context| | ||
built_context.merge(context) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe OpenFeature::SDK::EvaluationContextBuilder do | ||
let(:builder) { described_class.new } | ||
let(:api_context) { OpenFeature::SDK::EvaluationContext.new("targeting_key" => "api", "api" => "key") } | ||
let(:client_context) { OpenFeature::SDK::EvaluationContext.new("targeting_key" => "client", "client" => "key") } | ||
let(:invocation_context) { OpenFeature::SDK::EvaluationContext.new("targeting_key" => "invocation", "invocation" => "key") } | ||
|
||
describe "#call" do | ||
context "when no available contexts" do | ||
it "returns nil" do | ||
result = builder.call(api_context: nil, client_context: nil, invocation_context: nil) | ||
|
||
expect(result).to be_nil | ||
end | ||
end | ||
|
||
context "when only api context" do | ||
it "returns api context" do | ||
result = builder.call(api_context:, client_context: nil, invocation_context: nil) | ||
|
||
expect(result).to eq(OpenFeature::SDK::EvaluationContext.new("targeting_key" => "api", "api" => "key")) | ||
end | ||
end | ||
|
||
context "when only client context" do | ||
it "returns client context" do | ||
result = builder.call(api_context: nil, client_context:, invocation_context: nil) | ||
|
||
expect(result).to eq(OpenFeature::SDK::EvaluationContext.new("targeting_key" => "client", "client" => "key")) | ||
end | ||
end | ||
|
||
context "when only invocation context" do | ||
it "returns invocation context" do | ||
result = builder.call(api_context: nil, client_context: nil, invocation_context:) | ||
|
||
expect(result).to eq(OpenFeature::SDK::EvaluationContext.new("targeting_key" => "invocation", "invocation" => "key")) | ||
end | ||
end | ||
|
||
context "when api and client contexts" do | ||
it "returns merged context" do | ||
result = builder.call(api_context:, client_context:, invocation_context: nil) | ||
|
||
expect(result).to eq(OpenFeature::SDK::EvaluationContext.new("targeting_key" => "client", "api" => "key", "client" => "key")) | ||
end | ||
end | ||
|
||
context "when client and invocation contexts" do | ||
it "returns merged context" do | ||
result = builder.call(api_context: nil, client_context:, invocation_context:) | ||
|
||
expect(result).to eq(OpenFeature::SDK::EvaluationContext.new("targeting_key" => "invocation", "client" => "key", "invocation" => "key")) | ||
end | ||
end | ||
|
||
context "when global and invocation contexts" do | ||
it "returns merged context" do | ||
result = builder.call(api_context:, client_context: nil, invocation_context:) | ||
|
||
expect(result).to eq(OpenFeature::SDK::EvaluationContext.new("targeting_key" => "invocation", "api" => "key", "invocation" => "key")) | ||
end | ||
end | ||
|
||
context "when all contexts" do | ||
it "returns merged context" do | ||
result = builder.call(api_context:, client_context:, invocation_context:) | ||
|
||
expect(result).to eq(OpenFeature::SDK::EvaluationContext.new("targeting_key" => "invocation", "api" => "key", "client" => "key", "invocation" => "key")) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe OpenFeature::SDK::EvaluationContext do | ||
let(:evaluation_context) { described_class.new("targeting_key" => "base", "favorite_fruit" => "apple") } | ||
|
||
describe "#merge" do | ||
context "when key exists in overriding context" do | ||
it "overrides" do | ||
overriding_context = described_class.new("targeting_key" => "new", "favorite_fruit" => "banana", "favorite_day" => "Monday") | ||
|
||
new_context = evaluation_context.merge(overriding_context) | ||
|
||
expect(new_context).to eq(described_class.new("targeting_key" => "new", "favorite_fruit" => "banana", "favorite_day" => "Monday")) | ||
end | ||
end | ||
|
||
context "when new keys exist in overwriting context" do | ||
it "merges" do | ||
overriding_context = described_class.new("favorite_day" => "Monday") | ||
|
||
new_context = evaluation_context.merge(overriding_context) | ||
|
||
expect(new_context).to eq(described_class.new("targeting_key" => "base", "favorite_fruit" => "apple", "favorite_day" => "Monday")) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.