feat: Add Hooks class#118
Conversation
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
|
Warning Review limit reached
Next review available in: 42 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Caution Failed to replace (edit) comment. This is likely due to insufficient permissions or the comment being deleted. Error details |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (2)
test/hook_test.cpp (1)
5-10: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueAdd missing
<vector>include.The test uses
std::vector(Line 222) but relies on transitive inclusion. Including it explicitly prevents unexpected compilation failures if downstream headers are refactored.♻️ Proposed fix
`#include` <any> `#include` <memory> `#include` <optional> `#include` <stdexcept> `#include` <string> +#include <vector>🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/hook_test.cpp` around lines 5 - 10, Add an explicit <vector> standard-library include alongside the existing headers in hook_test.cpp, so the std::vector usage in the test is directly supported rather than relying on transitive includes.openfeature/base_hook.h (1)
12-15: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueDelete move operations for polymorphic base class.
BaseHookdeletes copy semantics but explicitly defaults move semantics. Since derived classes likeHookexplicitly delete move operations, it is safer and more consistent to delete move semantics at the base class level to entirely prevent unintended slicing or partial moves.♻️ Proposed refactor
- BaseHook(const BaseHook&) = delete; - BaseHook(BaseHook&&) = default; - BaseHook& operator=(const BaseHook&) = delete; - BaseHook& operator=(BaseHook&&) = default; + BaseHook(const BaseHook&) = delete; + BaseHook(BaseHook&&) = delete; + BaseHook& operator=(const BaseHook&) = delete; + BaseHook& operator=(BaseHook&&) = delete;🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@openfeature/base_hook.h` around lines 12 - 15, Delete the move constructor and move assignment operator declarations in BaseHook, replacing the defaulted move operations while preserving its existing deleted copy operations. This ensures the polymorphic base class cannot be moved or partially transferred, consistent with derived classes such as Hook.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@openfeature/hook_data.h`:
- Around line 22-29: Add a const overload of HookData::GetAs matching the
existing non-const lookup and typed any_cast behavior, returning a const T
pointer from const HookData instances. Preserve the current missing-key behavior
by returning nullptr.
In `@openfeature/hook.h`:
- Around line 34-38: Make the Before hook accept HookContext<T> as a const
reference, matching the immutability contract and the existing After, Error, and
Finally signatures. Update the corresponding mocked override in
test/hook_test.cpp to use the same const-reference signature; both affected
sites require this change.
---
Nitpick comments:
In `@openfeature/base_hook.h`:
- Around line 12-15: Delete the move constructor and move assignment operator
declarations in BaseHook, replacing the defaulted move operations while
preserving its existing deleted copy operations. This ensures the polymorphic
base class cannot be moved or partially transferred, consistent with derived
classes such as Hook.
In `@test/hook_test.cpp`:
- Around line 5-10: Add an explicit <vector> standard-library include alongside
the existing headers in hook_test.cpp, so the std::vector usage in the test is
directly supported rather than relying on transitive includes.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 381c79ae-6a33-413a-ad1c-0990a4889791
📒 Files selected for processing (17)
openfeature/BUILDopenfeature/base_hook.hopenfeature/flag_evaluation_details.cppopenfeature/flag_evaluation_details.hopenfeature/flag_type_value.hopenfeature/hook.cppopenfeature/hook.hopenfeature/hook_context.cppopenfeature/hook_context.hopenfeature/hook_data.cppopenfeature/hook_data.hopenfeature/hook_hints.htest/BUILDtest/flag_evaluation_details_test.cpptest/hook_context_test.cpptest/hook_data_test.cpptest/hook_test.cpp
| template <typename T> | ||
| T* GetAs(std::string_view key) { | ||
| auto it_key = data_.find(std::string(key)); | ||
| if (it_key != data_.end()) { | ||
| return std::any_cast<T>(&it_key->second); | ||
| } | ||
| return nullptr; | ||
| } |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Add a const overload for GetAs.
The standard OpenFeature Hook lifecycle methods (After, Error, and Finally) receive a const HookContext<T>& ctx. This likely limits state retrieval to a const HookData&. Without a const overload for GetAs, hook authors will not be able to use typed retrieval during these critical stages to read the state they initialized in the Before hook.
🛠️ Proposed fix
template <typename T>
T* GetAs(std::string_view key) {
auto it_key = data_.find(std::string(key));
if (it_key != data_.end()) {
return std::any_cast<T>(&it_key->second);
}
return nullptr;
}
+
+ template <typename T>
+ const T* GetAs(std::string_view key) const {
+ auto it_key = data_.find(std::string(key));
+ if (it_key != data_.end()) {
+ return std::any_cast<T>(&it_key->second);
+ }
+ return nullptr;
+ }📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| template <typename T> | |
| T* GetAs(std::string_view key) { | |
| auto it_key = data_.find(std::string(key)); | |
| if (it_key != data_.end()) { | |
| return std::any_cast<T>(&it_key->second); | |
| } | |
| return nullptr; | |
| } | |
| template <typename T> | |
| T* GetAs(std::string_view key) { | |
| auto it_key = data_.find(std::string(key)); | |
| if (it_key != data_.end()) { | |
| return std::any_cast<T>(&it_key->second); | |
| } | |
| return nullptr; | |
| } | |
| template <typename T> | |
| const T* GetAs(std::string_view key) const { | |
| auto it_key = data_.find(std::string(key)); | |
| if (it_key != data_.end()) { | |
| return std::any_cast<T>(&it_key->second); | |
| } | |
| return nullptr; | |
| } |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@openfeature/hook_data.h` around lines 22 - 29, Add a const overload of
HookData::GetAs matching the existing non-const lookup and typed any_cast
behavior, returning a const T pointer from const HookData instances. Preserve
the current missing-key behavior by returning nullptr.
| // Runs before the flag evaluation occurs. | ||
| virtual std::optional<EvaluationContext> Before(HookContext<T>& ctx, | ||
| const HookHints& hints) { | ||
| return std::nullopt; | ||
| } |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
Enforce HookContext immutability in the Before hook.
The OpenFeature specification requires that the before hook must not alter the hook context object. Passing ctx as a non-const reference violates this contract and introduces an inconsistency with the After, Error, and Finally methods which correctly accept it as a const reference. The underlying HookData state remains cleanly modifiable since it is stored via std::shared_ptr.
openfeature/hook.h#L34-L38: Update the signature to acceptconst HookContext<T>& ctx.test/hook_test.cpp#L27-L38: Update the mocked override signature to matchconst HookContext<T>& ctx.
📍 Affects 2 files
openfeature/hook.h#L34-L38(this comment)test/hook_test.cpp#L27-L38
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@openfeature/hook.h` around lines 34 - 38, Make the Before hook accept
HookContext<T> as a const reference, matching the immutability contract and the
existing After, Error, and Finally signatures. Update the corresponding mocked
override in test/hook_test.cpp to use the same const-reference signature; both
affected sites require this change.
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <115723925+NeaguGeorgiana23@users.noreply.github.com>
This PR
Introduces the foundation for Hooks. It defines the base interfaces for hooks and the evaluation details structure passed to them during the evaluation lifecycle.
BaseHook, a non-templated base class, allowing different types of hooks to be stored together.Hook<T>template class defining the lifecycle methods:Before,After,Error, andFinally.FlagEvaluationDetails<T>, which extendsResolutionDetails<T>by adding theflag_keyto associate the evaluation results with the requested flag.BoolHook,StringHook,IntHook,DoubleHook,ObjectHookand their correspondingFlagEvaluationDetailscounterparts).FlagEvaluationDetailsand theHooklifecycle methods.Fixes #117