Changed UpdateFlags function to return absl::Status#114
Conversation
Signed-off-by: Marcin Olko <molko@google.com>
There was a problem hiding this comment.
Code Review
This pull request refactors the flag synchronization and validation logic in the flagd provider to return absl::Status instead of boolean or void, allowing for better error propagation. Callers and test suites have been updated to handle and assert on these statuses. The reviewer's feedback focuses on performance and safety improvements, suggesting the use of [[nodiscard]] to prevent ignoring returned statuses, passing JSON objects by value to enable move semantics and avoid expensive copies, and using auto for clearly deduced types.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
|
|
||
| protected: | ||
| void UpdateFlags(const nlohmann::json& new_json); | ||
| absl::Status UpdateFlags(const nlohmann::json& new_json); |
There was a problem hiding this comment.
Mark UpdateFlags with [[nodiscard]] to ensure that callers do not silently ignore validation or update errors. Additionally, pass new_json by value (nlohmann::json) instead of const nlohmann::json& to enable move semantics and avoid expensive deep copies of the flag configuration JSON.
| absl::Status UpdateFlags(const nlohmann::json& new_json); | |
| [[nodiscard]] absl::Status UpdateFlags(nlohmann::json new_json); |
| } | ||
|
|
||
| bool Validate(const Json& json) const { | ||
| absl::Status Validate(const Json& json) const { |
| FlagSync::~FlagSync() = default; | ||
|
|
||
| void FlagSync::UpdateFlags(const nlohmann::json& new_json) { | ||
| absl::Status FlagSync::UpdateFlags(const nlohmann::json& new_json) { |
There was a problem hiding this comment.
Update the definition of UpdateFlags to accept new_json by value and mark it [[nodiscard]].
To fully leverage move semantics and avoid deep copies of the JSON structure, you should also update the body of this function (which is outside the current diff) to use std::move when extracting "flags" and "metadata":
auto new_flags_snapshot =
std::make_shared<const nlohmann::json>(std::move(new_json["flags"]));
auto new_metadata_snapshot =
std::make_shared<const nlohmann::json>(std::move(new_json["metadata"]));| absl::Status FlagSync::UpdateFlags(const nlohmann::json& new_json) { | |
| [[nodiscard]] absl::Status FlagSync::UpdateFlags(nlohmann::json new_json) { |
References
- Avoid redundant checks for the existence of JSON fields if their presence is guaranteed by schema validation performed before the data is processed.
- In modern C++, use 'auto' for variable declarations when the type is clearly deduced from the expression on the right-hand side.
| absl::Status status = UpdateFlags(raw); | ||
| if (!status.ok()) { | ||
| LOG(ERROR) << "Failed to update flags: " << status; | ||
| continue; | ||
| } |
There was a problem hiding this comment.
Pass raw using std::move to avoid copying the entire JSON object when calling UpdateFlags. Use auto for the status variable declaration as its type is clearly deduced from the function call.
| absl::Status status = UpdateFlags(raw); | |
| if (!status.ok()) { | |
| LOG(ERROR) << "Failed to update flags: " << status; | |
| continue; | |
| } | |
| auto status = UpdateFlags(std::move(raw)); | |
| if (!status.ok()) { | |
| LOG(ERROR) << "Failed to update flags: " << status; | |
| continue; | |
| } |
References
- In modern C++, use 'auto' for variable declarations when the type is clearly deduced from the expression on the right-hand side.
| absl::Status TriggerUpdate(const nlohmann::json& new_json) { | ||
| return this->UpdateFlags(new_json); | ||
| } |
There was a problem hiding this comment.
Update TriggerUpdate to accept new_json by value and move it to UpdateFlags to align with the new signature and avoid unnecessary copies.
| absl::Status TriggerUpdate(const nlohmann::json& new_json) { | |
| return this->UpdateFlags(new_json); | |
| } | |
| absl::Status TriggerUpdate(nlohmann::json new_json) { | |
| return this->UpdateFlags(std::move(new_json)); | |
| } |
| absl::Status SetFlags(const nlohmann::json& flags) { | ||
| return this->UpdateFlags(flags); | ||
| } |
There was a problem hiding this comment.
Update SetFlags to accept flags by value and move it to UpdateFlags to align with the new signature and avoid unnecessary copies.
| absl::Status SetFlags(const nlohmann::json& flags) { | |
| return this->UpdateFlags(flags); | |
| } | |
| absl::Status SetFlags(nlohmann::json flags) { | |
| return this->UpdateFlags(std::move(flags)); | |
| } |
| absl::Status TriggerUpdate(const nlohmann::json& new_json) { | ||
| return this->UpdateFlags(new_json); | ||
| } |
There was a problem hiding this comment.
Update TriggerUpdate to accept new_json by value and move it to UpdateFlags to align with the new signature and avoid unnecessary copies.
| absl::Status TriggerUpdate(const nlohmann::json& new_json) { | |
| return this->UpdateFlags(new_json); | |
| } | |
| absl::Status TriggerUpdate(nlohmann::json new_json) { | |
| return this->UpdateFlags(std::move(new_json)); | |
| } |
| absl::Status TriggerUpdate(const nlohmann::json& new_json) { | ||
| return this->UpdateFlags(new_json); | ||
| } |
There was a problem hiding this comment.
Update TriggerUpdate to accept new_json by value and move it to UpdateFlags to align with the new signature and avoid unnecessary copies.
| absl::Status TriggerUpdate(const nlohmann::json& new_json) { | |
| return this->UpdateFlags(new_json); | |
| } | |
| absl::Status TriggerUpdate(nlohmann::json new_json) { | |
| return this->UpdateFlags(std::move(new_json)); | |
| } |
This PR
Update the
FlagSync:UpdateFlagsinterface to returnabsl::Statusand return errors on validation problems. The change also includes updating all the tests to support that.Related Issues
Fixes #113