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: adds evaluation context merging #56

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions lib/openfeature/sdk/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Client

attr_accessor :hooks

def initialize(provider:, client_options: nil, context: nil)
def initialize(provider:, client_options: nil, context: {})
@provider = provider
@metadata = client_options
@context = context
Expand All @@ -25,7 +25,8 @@ def initialize(provider:, client_options: nil, context: nil)
# def fetch_boolean_details(flag_key:, default_value:, evaluation_context: nil)
# result = @provider.fetch_boolean_value(flag_key: flag_key, default_value: default_value, evaluation_context: evaluation_context)
# end
def fetch_#{result_type}_#{suffix}(flag_key:, default_value:, evaluation_context: nil)
def fetch_#{result_type}_#{suffix}(flag_key:, default_value:, evaluation_context: {})
evaluation_context = OpenFeature::SDK.context.merge(@context).merge(evaluation_context)
Copy link
Collaborator

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:

      NoMethodError:
        undefined method `merge' for nil:NilClass
      # ./lib/openfeature/sdk/client.rb:29:in `fetch_number_details'
      # ./spec/openfeature/sdk/client_spec.rb:150:in `block (6 levels) in <top (required)>'

It doesn't look like OpenFeature::SDK.context ever gets set.

result = @provider.fetch_#{result_type}_value(flag_key: flag_key, default_value: default_value, evaluation_context: evaluation_context)
#{"result.value" if suffix == :value}
end
Expand Down
48 changes: 48 additions & 0 deletions lib/openfeature/sdk/evaluation/context.rb
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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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({}))
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

@mschoenlaub mschoenlaub Sep 15, 2023

Choose a reason for hiding this comment

The 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
Given that there seem to be a lot of moving parts right now in this repo, I'll probably revisit this at a later point :)

Hmm.. not sure where exactly concurrent-ruby got removed, but not in this repo. That being said, I'm not unhappy with it being removed at all.

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is targetting key refer to exactly?

Copy link
Member

Choose a reason for hiding this comment

The 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?
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 freeze handles that check for you.

Suggested change
hash.freeze unless hash.frozen?
hash.freeze

Copy link
Contributor Author

@mschoenlaub mschoenlaub Sep 15, 2023

Choose a reason for hiding this comment

The 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.
While I think there might be no obvious reason to do that, there's nothing in the spec or the language that prohibits that.
.duping stuff could avoid that... but not sure if that's not more expensive in the end.

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
91 changes: 91 additions & 0 deletions spec/openfeature/sdk/evaluation/context_spec.rb
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
Loading