-
Notifications
You must be signed in to change notification settings - Fork 9
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: adds evaluation context merging #56
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,48 @@ | ||||||
# frozen_string_literal: true | ||||||
|
||||||
module OpenFeature | ||||||
module SDK | ||||||
module Evaluation | ||||||
# A container for arbitrary contextual data that can be used as a basis for dynamic evaluation | ||||||
class Context < SimpleDelegator | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not seeing anywhere else in the PR that is using this yet 🤔 |
||||||
|
||||||
def initialize(context = Concurrent::Hash.new({})) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is part of the concurrent-ruby and it's not a dependency. @josecolella included this dependency initially, but we ended up removing it for simplificy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I realize that development has suddendly picked up a lot of pace here :D Hmm.. not sure where exactly I'm just slightly frustrated with last year's development pace vs. last weeks pace :D |
||||||
raise ArgumentError, "context must be a Hash" unless context.is_a?(Hash) | ||||||
|
||||||
context = Concurrent::Hash[context] unless context.is_a?(Concurrent::Hash) | ||||||
super(context) | ||||||
end | ||||||
|
||||||
def freeze | ||||||
super | ||||||
deep_freeze | ||||||
end | ||||||
|
||||||
def targeting_key | ||||||
self[:targeting_key] | ||||||
end | ||||||
|
||||||
def targeting_key=(value) | ||||||
raise ArgumentError, "targeting_key must be a String" unless value.is_a?(String) | ||||||
|
||||||
self[:targeting_key] = value | ||||||
end | ||||||
Comment on lines
+21
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The targeting key is basically a well-defined property in the context, which is necessary because many providers need a user-identifier or equivalent of some kind: https://openfeature.dev/specification/sections/evaluation-context#requirement-311 |
||||||
|
||||||
private | ||||||
|
||||||
def deep_freeze | ||||||
hashes = values.select { |value| value.is_a?(Hash) } | ||||||
while hashes.empty? == false | ||||||
hash = hashes.pop | ||||||
hash.freeze unless hash.frozen? | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: freeze is perfectly happy to run on a frozen object. Under the hood
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yeah, I think what I had in my mind was more a cheap check to avoid running in circles when one of the values contains the hash itself. |
||||||
hash.each do |key, value| | ||||||
key.freeze unless key.frozen? | ||||||
value.freeze unless value.frozen? | ||||||
hashes << value if value.is_a?(Hash) | ||||||
end | ||||||
end | ||||||
end | ||||||
end | ||||||
end | ||||||
end | ||||||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../../../spec_helper" | ||
|
||
require "openfeature/sdk/evaluation/context" | ||
require "date" | ||
|
||
# https://openfeature.dev/specification/sections/evaluation-context | ||
RSpec.describe OpenFeature::SDK::Evaluation::Context do | ||
subject(:evaluation_context) { described_class.new({ targeting_key: "targeting_key", custom_field: "abc" }) } | ||
context "3.1 Fields" do | ||
context "Requirement 3.1.1" do | ||
it "MUST define an optional targeting key field of type string, identifying the subject of the flag evaluation." do | ||
expect(evaluation_context).to respond_to(:targeting_key) | ||
expect(evaluation_context).to respond_to(:targeting_key=) | ||
expect(evaluation_context.targeting_key).to be_a(String) | ||
end | ||
end | ||
|
||
context "Requirement 3.1.2" do | ||
context "MUST support the inclusion of custom fields, having keys of type string, and values of type boolean | string | number | datetime | structure." do | ||
context "boolean" do | ||
it do | ||
expect(evaluation_context[:boolean_key] = true).to eq(true) | ||
expect(evaluation_context[:boolean_key]).to be_a(TrueClass) | ||
end | ||
end | ||
context "string" do | ||
it do | ||
expect(evaluation_context[:string_key] = "string_value").to eq("string_value") | ||
expect(evaluation_context[:string_key]).to be_a(String) | ||
end | ||
end | ||
context "number" do | ||
it do | ||
expect(evaluation_context[:number_key] = 1).to eq(1) | ||
expect(evaluation_context[:number_key]).to be_a(Integer) | ||
end | ||
end | ||
context "datetime" do | ||
it do | ||
expect(evaluation_context[:datetime_key] = DateTime.now).to be_a(DateTime) | ||
expect(evaluation_context[:datetime_key]).to be_a(DateTime) | ||
end | ||
end | ||
context "structure" do | ||
it do | ||
expect(evaluation_context[:structure_key] = { key: "value" }).to eq({ key: "value" }) | ||
expect(evaluation_context[:structure_key]).to be_a(Hash) | ||
end | ||
end | ||
end | ||
end | ||
|
||
context "Requirement 3.1.3" do | ||
it "MUST support fetching the custom fields by key and also fetching all key value pairs." do | ||
expect(evaluation_context).to respond_to(:to_h) | ||
expect(evaluation_context.to_h).to eq({ targeting_key: "targeting_key", custom_field: "abc" }) | ||
end | ||
end | ||
|
||
context "Requirement 3.1.4" do | ||
it "MUST have an unique key." do | ||
evaluation_context[:key] = "value" | ||
expect(evaluation_context[:key]).to eq("value") | ||
evaluation_context[:key] = "new_value" | ||
expect(evaluation_context[:key]).to eq("new_value") | ||
expect(evaluation_context.keys).to eq(evaluation_context.keys.uniq) | ||
end | ||
end | ||
|
||
context "Requirement 3.2.2" do | ||
let(:provider) { instance_spy("NoOpProvider") } | ||
|
||
it "MUST be merged in the order: API (global; lowest precedence) -> client -> invocation -> before hooks (highest precedence), with duplicate values being overwritten" do | ||
api_context = described_class.new({ a: "api_value", b: "api_value" }) | ||
client_context = described_class.new({ c: "client_value", b: "client_value" }) | ||
invocation_context = described_class.new({ d: "invocation_value", b: "invocation_value" }) | ||
|
||
OpenFeature::SDK.configure do |config| | ||
config.context = api_context | ||
end | ||
expected_context = { a: "api_value", c: "client_value", d: "invocation_value", b: "invocation_value" } | ||
|
||
client = OpenFeature::SDK::Client.new(provider: provider, context: client_context) | ||
client.fetch_boolean_value(flag_key: "flag_key", default_value: false, evaluation_context: invocation_context) | ||
expect(provider).to have_received(:fetch_boolean_value).with(flag_key: "flag_key", default_value: false, evaluation_context: expected_context) | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a bunch of CI failures originating back to this line, ie:
It doesn't look like
OpenFeature::SDK.context
ever gets set.