-
Notifications
You must be signed in to change notification settings - Fork 13
feat(Engine): Evaluate from EvaluationContext #108
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
base: main
Are you sure you want to change the base?
Conversation
8a8c07a
to
3f2eaf4
Compare
3f2eaf4
to
7295ca8
Compare
$any = false; | ||
foreach ($rule->conditions as $condition) { | ||
$conditionMatches = self::_contextMatchesCondition( | ||
$context, | ||
$condition, | ||
$segmentKey, | ||
); | ||
|
||
switch ($rule->type) { | ||
case SegmentRuleType::ALL: | ||
if (!$conditionMatches) { | ||
return false; | ||
} | ||
break; | ||
case SegmentRuleType::NONE: | ||
if ($conditionMatches) { | ||
return false; | ||
} | ||
break; | ||
case SegmentRuleType::ANY: | ||
if ($conditionMatches) { | ||
$any = true; | ||
break 2; | ||
} | ||
break; | ||
} | ||
} | ||
|
||
if ($rule->type === SegmentRuleType::ANY && !$any) { | ||
return false; | ||
} | ||
|
||
foreach ($rule->rules as $subRule) { | ||
$ruleMatches = self::_contextMatchesRule( | ||
$context, | ||
$subRule, | ||
$segmentKey, | ||
); | ||
if (!$ruleMatches) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; |
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.
Note
Compared to the Python SDK's, this is improved with short-circuiting in both conditions and sub-rule testing.
// Hack to allow comparing flags as associative arrays (<feature_name>: <flag>) | ||
$wanted = array_column($expectedEvaluationResult->flags, null, 'name'); | ||
$expectedEvaluationResult->flags = $wanted; | ||
$actual = array_column($evaluationResult->flags, null, 'name'); | ||
$evaluationResult->flags = $actual; |
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.
Note
I would prefer EvaluationResult->flags
to be an associative array <feature_name> => <flag>
, rather than an indexed array. This would improve data consistency and avoid this hack. cc @khvn26
/** @return array<string, mixed> */ | ||
public function jsonSerialize(): array | ||
{ | ||
$json = [ | ||
'key' => $this->key, | ||
'feature_key' => $this->feature_key, | ||
'name' => $this->name, | ||
'enabled' => $this->enabled, | ||
'value' => $this->value, | ||
]; | ||
|
||
if ($this->priority !== null) { | ||
$json['priority'] = $this->priority; | ||
} | ||
|
||
if ($this->variants) { | ||
$json['variants'] = $this->variants; | ||
} | ||
|
||
return $json; | ||
} |
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.
Note
This only exists because the keys variants
and priority
are nullable in the compiled context. It can be avoided if both values are always present in the context but with insignificant values, such as rules
and conditions
are empty arrays by default.
38c5cef
to
fe68922
Compare
Contributes to #98
The engine work is largely inspired by flagsmith-engine.
Note
Notes are added to call for discussion or follow up.