-
Notifications
You must be signed in to change notification settings - Fork 6
feat: sdk consumes context engine #89
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
Open
Zaimwa9
wants to merge
4
commits into
feat/evaluation-get-result
Choose a base branch
from
feat/sdk-consumes-context-engine
base: feat/evaluation-get-result
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'json' | ||
| require 'jsonpath' | ||
|
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 thought JSONPath part was moved to #88? Should this PR get rebased? |
||
| require_relative 'constants' | ||
| require_relative 'models' | ||
| require_relative '../utils/hash_func' | ||
|
|
@@ -18,7 +20,7 @@ module Evaluator | |
| # | ||
| # @param context [Hash] Evaluation context containing identity and segment definitions | ||
| # @return [Array<Hash>] Array of segments that the identity matches | ||
| def get_identity_segments_from_context(context) | ||
| def get_identity_segments(context) | ||
| return [] unless context[:identity] && context[:segments] | ||
|
|
||
| context[:segments].values.select do |segment| | ||
|
|
@@ -29,67 +31,7 @@ def get_identity_segments_from_context(context) | |
| end | ||
| end | ||
|
|
||
| # Model-based segment evaluation (existing approach) | ||
| def get_identity_segments(environment, identity, override_traits = nil) | ||
| environment.project.segments.select do |s| | ||
| evaluate_identity_in_segment(identity, s, override_traits) | ||
| end | ||
| end | ||
|
|
||
| # Evaluates whether a given identity is in the provided segment. | ||
| # | ||
| # :param identity: identity model object to evaluate | ||
| # :param segment: segment model object to evaluate | ||
| # :param override_traits: pass in a list of traits to use instead of those on the | ||
| # identity model itself | ||
| # :return: True if the identity is in the segment, False otherwise | ||
| def evaluate_identity_in_segment(identity, segment, override_traits = nil) | ||
| segment.rules&.length&.positive? && | ||
| segment.rules.all? do |rule| | ||
| traits_match_segment_rule( | ||
| override_traits || identity.identity_traits, | ||
| rule, | ||
| segment.id, | ||
| identity.django_id || identity.composite_key | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| # rubocop:disable Metrics/MethodLength | ||
| def traits_match_segment_rule(identity_traits, rule, segment_id, identity_id) | ||
| matching_block = lambda { |condition| | ||
| traits_match_segment_condition(identity_traits, condition, segment_id, identity_id) | ||
| } | ||
|
|
||
| matches_conditions = | ||
| if rule.conditions&.length&.positive? | ||
| rule.conditions.send(rule.matching_function, &matching_block) | ||
| else | ||
| true | ||
| end | ||
|
|
||
| matches_conditions && | ||
| rule.rules.all? { |r| traits_match_segment_rule(identity_traits, r, segment_id, identity_id) } | ||
| end | ||
| # rubocop:enable Metrics/MethodLength | ||
|
|
||
| def traits_match_segment_condition(identity_traits, condition, segment_id, identity_id) | ||
| if condition.operator == PERCENTAGE_SPLIT | ||
| return hashed_percentage_for_object_ids([segment_id, | ||
| identity_id]) <= condition.value.to_f | ||
| end | ||
|
|
||
| trait = identity_traits.find { |t| t.key.to_s == condition.property } | ||
|
|
||
| return handle_trait_existence_conditions(trait, condition.operator) if [IS_SET, | ||
| IS_NOT_SET].include?(condition.operator) | ||
|
|
||
| return condition.match_trait_value?(trait.trait_value) if trait | ||
|
|
||
| false | ||
| end | ||
|
|
||
| # Context-based helper functions (new approach) | ||
| # Context-based helper functions | ||
|
|
||
| # Evaluates whether a segment rule matches using context | ||
| # | ||
|
|
@@ -148,9 +90,7 @@ def traits_match_segment_condition_from_context(condition, segment_key, context) | |
| end | ||
|
|
||
| return false if condition[:property].nil? | ||
|
|
||
| trait_value = get_trait_value(condition[:property], context) | ||
|
|
||
| return !trait_value.nil? if condition[:operator] == IS_SET | ||
| return trait_value.nil? if condition[:operator] == IS_NOT_SET | ||
|
|
||
|
|
@@ -200,25 +140,15 @@ def get_trait_value(property, context) | |
| traits[property] || traits[property.to_sym] | ||
| end | ||
|
|
||
| # Get value from context using JSONPath-like syntax | ||
| # Get value from context using JSONPath syntax | ||
| # | ||
| # @param json_path [String] JSONPath expression (e.g., '$.identity.identifier') | ||
| # @param context [Hash] The evaluation context | ||
| # @return [Object, nil] The value at the path or nil | ||
| def get_context_value(json_path, context) | ||
| return nil unless context && json_path&.start_with?('$.') | ||
|
|
||
| # Simple JSONPath implementation - handle basic cases | ||
| path_parts = json_path.sub('$.', '').split('.') | ||
| current = context | ||
|
|
||
| path_parts.each do |part| | ||
| return nil unless current.is_a?(Hash) | ||
|
|
||
| current = current[part.to_sym] | ||
| end | ||
|
|
||
| current | ||
| results = JsonPath.new(json_path, use_symbols: true).on(context) | ||
| results.first | ||
| rescue StandardError | ||
| nil | ||
| end | ||
|
|
@@ -243,14 +173,6 @@ def non_primitive?(value) | |
|
|
||
| value.is_a?(Hash) || value.is_a?(Array) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def handle_trait_existence_conditions(matching_trait, operator) | ||
| return operator == IS_NOT_SET if matching_trait.nil? | ||
|
|
||
| operator == IS_SET | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
This file contains hidden or 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 | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -103,7 +103,12 @@ def match_modulo_value(trait_value) | |||||||||||||||||||||||||
| def match_in_value(trait_value) | ||||||||||||||||||||||||||
| return false if trait_value.nil? || trait_value.is_a?(TrueClass) || trait_value.is_a?(FalseClass) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| return @value.include?(trait_value.to_s) if @value.is_a?(Array) | ||||||||||||||||||||||||||
| # Floats/doubles are not supported by the engine due to ambiguous serialization across supported platforms. (segments/models_spec.rb) | ||||||||||||||||||||||||||
| return false unless trait_value.is_a?(String) || trait_value.is_a?(Integer) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if @value.is_a?(Array) | ||||||||||||||||||||||||||
| return @value.include?(trait_value.to_s) | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
|
Comment on lines
+106
to
+111
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. According to Flagsmith/engine-test-data#38, we only skip booleans now:
Suggested change
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if @value.is_a?(String) | ||||||||||||||||||||||||||
| begin | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Since we're major-versioning anyway, can we either remove the class altogether, add
Flagsmith::Engine::get_evaluation_resultinterface? I don't see how an emptyEngineclass helps to maintain backwards compatibility here?