-
Notifications
You must be signed in to change notification settings - Fork 2
Implement class Evaluation Context #56
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0aff916
Implement class Evaluation Context
NeaguGeorgiana23 82db85c
Merge branch 'main' into eval_context
NeaguGeorgiana23 319928e
Fix tests that were broken due to EvaluationContext not having a defa…
NeaguGeorgiana23 ceaca64
Resolve comments.
NeaguGeorgiana23 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| #include "evaluation_context.h" | ||
|
|
||
| namespace openfeature { | ||
|
|
||
| EvaluationContext::EvaluationContext(std::optional<std::string> targeting_key, | ||
| std::map<std::string, std::any> attributes) | ||
| : targeting_key_(std::move(targeting_key)), | ||
| attributes_(std::move(attributes)) {} | ||
|
|
||
| std::optional<std::string_view> EvaluationContext::GetTargetingKey() const { | ||
| if (!targeting_key_.has_value()) { | ||
| return std::nullopt; | ||
| } | ||
| return targeting_key_; | ||
| } | ||
|
|
||
| const std::any* EvaluationContext::GetValue(std::string_view key) const { | ||
| auto it = attributes_.find(std::string(key)); | ||
| if (it != attributes_.end()) { | ||
| return &it->second; | ||
| } | ||
| return nullptr; | ||
| } | ||
|
|
||
| const std::map<std::string, std::any>& EvaluationContext::GetAttributes() | ||
| const { | ||
| return attributes_; | ||
| } | ||
|
|
||
| EvaluationContext EvaluationContext::Merge( | ||
| std::initializer_list<const EvaluationContext*> contexts) { | ||
| Builder builder; | ||
|
|
||
| // Merge Attributes from all contexts (higher precedence overwrites lower). | ||
| for (const EvaluationContext* ctx : contexts) { | ||
| if (ctx != nullptr) { | ||
| for (const auto& pair : ctx->GetAttributes()) { | ||
| builder.WithAttribute(pair.first, pair.second); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Find the first valid targeting key from highest to lowest precedence. | ||
| // We iterate through the list backwards to check the highest precedence | ||
| // context first. | ||
| std::vector<const EvaluationContext*> reversed(contexts); | ||
| std::reverse(reversed.begin(), reversed.end()); | ||
|
|
||
| for (const EvaluationContext* ctx : reversed) { | ||
| if (ctx != nullptr) { | ||
| auto key = ctx->GetTargetingKey(); | ||
| if (key.has_value() && !key->empty()) { | ||
| builder.WithTargetingKey(std::string(key.value())); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return builder.build(); | ||
| } | ||
|
|
||
| EvaluationContext::Builder& EvaluationContext::Builder::WithTargetingKey( | ||
| std::string key) { | ||
| this->targeting_key_ = std::move(key); | ||
| return *this; | ||
| } | ||
|
|
||
| EvaluationContext::Builder& EvaluationContext::Builder::WithAttribute( | ||
| std::string key, std::any value) { | ||
| this->attributes_.insert_or_assign(std::move(key), std::move(value)); | ||
| return *this; | ||
| } | ||
|
|
||
| EvaluationContext::Builder& EvaluationContext::Builder::WithAttribute( | ||
| std::string key, const char* value) { | ||
| return this->WithAttribute(std::move(key), std::string(value)); | ||
| } | ||
|
|
||
| EvaluationContext EvaluationContext::Builder::build() const { | ||
| return EvaluationContext(targeting_key_, attributes_); | ||
| } | ||
|
|
||
| } // namespace openfeature | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.