Skip to content
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

feat:variable-length arguments in configuration(closes #146) #164

Open
wants to merge 4 commits into
base: unstable
Choose a base branch
from

Conversation

wxnzb
Copy link
Collaborator

@wxnzb wxnzb commented Jan 11, 2025

在配置中添加对不定长参数的支持

Summary by CodeRabbit

Summary by CodeRabbit

fix #146

  • New Features
    • Introduced an updated method for setting values in the configuration, allowing for more flexible handling with an optional check parameter.
  • Bug Fixes
    • Improved error handling for parameter validation in configuration settings, providing more precise error messaging when insufficient parameters are supplied.
  • Enhancements
    • Expanded the interface of the StringValueArray class with a new method for setting values, enhancing its functionality.

Copy link

coderabbitai bot commented Jan 11, 2025

Warning

Rate limit exceeded

@wxnzb has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 2 minutes and 24 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 2ab8978 and e3862a6.

📒 Files selected for processing (1)
  • src/config.cc (1 hunks)

Walkthrough

The pull request modifies the SetValue method in the StringValueArray class within the src/config.cc file. The original method is replaced with a new implementation that includes an additional boolean parameter, check, set to false. This change allows the method to conditionally check the size of the values vector against the values_ vector, enhancing flexibility in how values are set. Additionally, a new method signature is introduced in the header file src/config.h to support this change.

Changes

File Change Summary
src/config.cc Updated SetValue method to include a boolean parameter check, modifying its logic for setting values.
src/config.h Added method signature for SetValue(const std::string& value, bool check) in StringValueArray.

Assessment against linked issues

Objective Addressed Explanation
Support for variable-length parameters (#146)

Poem

🐰 In the config's heart, a new path is laid,
With a boolean check, our values cascade.
A rabbit's delight in the code's gentle flow,
Expanding the bounds of what we can know!
Hopping through logic, with joy we embrace,
Each parameter's dance finds its rightful place! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/config.cc (1)

77-78: LGTM with suggestions for improvement.

The change correctly implements variable-length argument support by allowing more parameters than required while still catching cases with insufficient parameters. However, consider these improvements:

  1. Add a warning when extra parameters are provided to help users identify potential configuration mistakes.
  2. Consider adding bounds checking in the subsequent loop to prevent potential buffer overflows.

Here's a suggested improvement:

   if (values.size() < values_.size()) {
     return Status::InvalidArgument("The number of parameters is less than required.");
+  } else if (values.size() > values_.size()) {
+    // Log a warning about extra parameters being ignored
   }
-  } else {  // if the value_ is empty, resize the value_ to the size of the values
+  } else {
     values_.resize(values.size());
   }

-  for (int i = 0; i < values.size(); i++) {
+  // Only process up to values_.size() elements to prevent buffer overflows
+  for (int i = 0; i < values_.size(); i++) {
     values_[i] = std::move(values[i]);
   }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7ac9b9a and ffd1c68.

📒 Files selected for processing (1)
  • src/config.cc (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: build_on_macos
  • GitHub Check: build_on_ubuntu
🔇 Additional comments (1)
src/config.cc (1)

77-78: Verify the impact of variable-length arguments support.

Let's analyze the usage patterns and test coverage for this change.

✅ Verification successful

Parameter validation is correctly implemented and necessary

The validation check prevents incomplete parameter sets from being applied through the configuration system, which is essential because:

  • The configuration can be modified at runtime through admin commands
  • The system expects complete parameter sets for proper functionality
  • It maintains configuration integrity across all supported options (boolean, string, and array values)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Analyze the usage of StringValueArray and configuration parsing

# Find all StringValueArray usages
echo "Finding StringValueArray usages:"
rg "StringValueArray" -A 5

# Find configuration parsing patterns
echo -e "\nAnalyzing configuration parsing patterns:"
rg "SplitString.*delimiter_" -A 5

# Look for related tests
echo -e "\nLooking for related tests:"
fd "test.*\.cc" | xargs rg "StringValueArray|SetValue.*values"

Length of output: 2303


Script:

#!/bin/bash
# Search for test files and configuration examples
echo "Looking for test files:"
fd -e cc -e cpp -e h "test" | xargs rg -l "Config|config"

echo -e "\nLooking for configuration examples:"
fd "config" -e conf -e ini -e yaml -e json

echo -e "\nSearching for documentation:"
fd "README|CONTRIBUTING" -e md | xargs rg -l "config|configuration" -i

echo -e "\nAnalyzing Config class usage:"
rg "Config::" -A 3

Length of output: 2509

src/config.cc Outdated
@@ -74,8 +74,8 @@ Status StringValue::SetValue(const std::string& value) {
Status StringValueArray::SetValue(const std::string& value) {
auto values = SplitString(value, delimiter_);
if (!values_.empty()) { // if the value_ is not empty, check the number of parameters
if (values.size() != values_.size()) {
return Status::InvalidArgument("The number of parameters does not match.");
if (values.size() < values_.size()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

需要支持两种情况

  1. 需要校验默认参数和实际参数长度是否一致
  2. 不需要校验默认参数长度

可以在SetValue(const std::string& value) 加一个默认参数, 是否需要校验默认参数

比如

SetValue(const std::string& value, bool check = false)

大多数情况下,应该都不需要检查默认参数长度,所以默认参数用 false 即可

有一种情况需要注意, 配置的默认参数填了2个, 但 配置文件中填了一个,需要把默认的两个都覆盖了,只用配置文件中的一个

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ffd1c68 and 3e2f46a.

📒 Files selected for processing (2)
  • src/config.cc (1 hunks)
  • src/config.h (1 hunks)
🧰 Additional context used
🪛 GitHub Actions: kiwi
src/config.cc

[error] 81-81: Clang format style issue: Extra whitespace at the end of line after values.size()

🔇 Additional comments (2)
src/config.h (1)

87-88: LGTM! Well-designed method signature.

The new SetValue overload with the check parameter provides a clean way to support both validation scenarios for variable-length arguments.

src/config.cc (1)

74-75: LGTM! Good default behavior.

The implementation correctly delegates to the new method with check=false, maintaining backward compatibility while defaulting to the more flexible behavior.

src/config.cc Outdated
Comment on lines 76 to 89
Status StringValueArray::SetValue(const std::string& value, bool check) {
auto values = SplitString(value, delimiter_);
if (!values_.empty()) { // if the value_ is not empty, check the number of parameters
if (check) {
if (values.size() != values_.size()) {
return Status::InvalidArgument("The number of parameters does not match.");
}
} else { // if the value_ is empty, resize the value_ to the size of the values
values_.resize(values.size());
}

for (int i = 0; i < values.size(); i++) {
values_[i] = std::move(values[i]);
} else {
if (values_.empty()) {
values_.resize(values.size());
}
for (size_t i = 0; i < values.size(); i++) {
values_[i] = std::move(values[i]);
}
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix formatting and improve error handling.

The implementation has a few issues that need attention:

  1. There's an extra whitespace at the end of line 81 (as reported by the pipeline).
  2. The logic in the else branch could be improved to handle the case where values_.size() is less than values.size().

Apply this diff to fix the issues:

 Status StringValueArray::SetValue(const std::string& value, bool check) {
   auto values = SplitString(value, delimiter_);
   if (check) {
-    if (values.size() != values_.size()) {
+    if (values.size() != values_.size()) { 
       return Status::InvalidArgument("The number of parameters does not match.");
     }
   } else {
-    if (values_.empty()) {
-      values_.resize(values.size());  
-    }
+    values_.resize(values.size());
     for (size_t i = 0; i < values.size(); i++) {
       values_[i] = std::move(values[i]);
     }
   }
   return Status::OK();
 }

The changes:

  1. Remove extra whitespace after values.size()
  2. Always resize values_ in the non-check branch to ensure it can accommodate all values
  3. Use std::move to avoid unnecessary copies (already implemented correctly)
📝 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.

Suggested change
Status StringValueArray::SetValue(const std::string& value, bool check) {
auto values = SplitString(value, delimiter_);
if (!values_.empty()) { // if the value_ is not empty, check the number of parameters
if (check) {
if (values.size() != values_.size()) {
return Status::InvalidArgument("The number of parameters does not match.");
}
} else { // if the value_ is empty, resize the value_ to the size of the values
values_.resize(values.size());
}
for (int i = 0; i < values.size(); i++) {
values_[i] = std::move(values[i]);
} else {
if (values_.empty()) {
values_.resize(values.size());
}
for (size_t i = 0; i < values.size(); i++) {
values_[i] = std::move(values[i]);
}
}
Status StringValueArray::SetValue(const std::string& value, bool check) {
auto values = SplitString(value, delimiter_);
if (check) {
if (values.size() != values_.size()) {
return Status::InvalidArgument("The number of parameters does not match.");
}
} else {
values_.resize(values.size());
for (size_t i = 0; i < values.size(); i++) {
values_[i] = std::move(values[i]);
}
}
return Status::OK();
}
🧰 Tools
🪛 GitHub Actions: kiwi

[error] 81-81: Clang format style issue: Extra whitespace at the end of line after values.size()

if (values_.empty()) {
values_.resize(values.size());
}
for (size_t i = 0; i < values.size(); i++) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里应该在 value和value_ 长度不相等就需要调用 values_.resize(values.size());

比如有一种情况, value_的默认值是 ["config1", "config2", "config3"], 但是在配置文件中的配置是

["config_1", "config_2"] 如果这时不调用 resize 函数, 那么读取配置后, 内存中的实际配置是
["config_1", "config_2", "config3"] 不符合预期的

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

好滴,我在改一下

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

Successfully merging this pull request may close these issues.

conf 不定长参数支持
2 participants