Skip to content

Changed UpdateFlags function to return absl::Status#114

Open
m-olko wants to merge 1 commit into
mainfrom
updateflags_returns_status
Open

Changed UpdateFlags function to return absl::Status#114
m-olko wants to merge 1 commit into
mainfrom
updateflags_returns_status

Conversation

@m-olko

@m-olko m-olko commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

This PR

Update the FlagSync:UpdateFlags interface to return absl::Status and return errors on validation problems. The change also includes updating all the tests to support that.

Related Issues

Fixes #113

Signed-off-by: Marcin Olko <molko@google.com>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Mark Validate with [[nodiscard]] to ensure that callers do not silently ignore validation errors.

Suggested change
absl::Status Validate(const Json& json) const {
[[nodiscard]] 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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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"]));
Suggested change
absl::Status FlagSync::UpdateFlags(const nlohmann::json& new_json) {
[[nodiscard]] absl::Status FlagSync::UpdateFlags(nlohmann::json new_json) {
References
  1. Avoid redundant checks for the existence of JSON fields if their presence is guaranteed by schema validation performed before the data is processed.
  2. In modern C++, use 'auto' for variable declarations when the type is clearly deduced from the expression on the right-hand side.

Comment on lines +239 to +243
absl::Status status = UpdateFlags(raw);
if (!status.ok()) {
LOG(ERROR) << "Failed to update flags: " << status;
continue;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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
  1. In modern C++, use 'auto' for variable declarations when the type is clearly deduced from the expression on the right-hand side.

Comment on lines +19 to 21
absl::Status TriggerUpdate(const nlohmann::json& new_json) {
return this->UpdateFlags(new_json);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Update TriggerUpdate to accept new_json by value and move it to UpdateFlags to align with the new signature and avoid unnecessary copies.

Suggested change
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));
}

Comment on lines +19 to +21
absl::Status SetFlags(const nlohmann::json& flags) {
return this->UpdateFlags(flags);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Update SetFlags to accept flags by value and move it to UpdateFlags to align with the new signature and avoid unnecessary copies.

Suggested change
absl::Status SetFlags(const nlohmann::json& flags) {
return this->UpdateFlags(flags);
}
absl::Status SetFlags(nlohmann::json flags) {
return this->UpdateFlags(std::move(flags));
}

Comment on lines +21 to 23
absl::Status TriggerUpdate(const nlohmann::json& new_json) {
return this->UpdateFlags(new_json);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Update TriggerUpdate to accept new_json by value and move it to UpdateFlags to align with the new signature and avoid unnecessary copies.

Suggested change
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));
}

Comment on lines +18 to 20
absl::Status TriggerUpdate(const nlohmann::json& new_json) {
return this->UpdateFlags(new_json);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Update TriggerUpdate to accept new_json by value and move it to UpdateFlags to align with the new signature and avoid unnecessary copies.

Suggested change
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));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[flagd] The flagUpdate in FlagSync should return absl::Status

1 participant