Skip to content

Commit

Permalink
variable-length-configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
wxnzb committed Jan 18, 2025
1 parent ffd1c68 commit 3e2f46a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
23 changes: 13 additions & 10 deletions src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,21 @@ Status StringValue::SetValue(const std::string& value) {
return Status::OK();
}

Status StringValueArray::SetValue(const std::string& value) {
Status StringValueArray::SetValue(const std::string& value) { return SetValue(value, false); }

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 (values.size() < values_.size()) {
return Status::InvalidArgument("The number of parameters is less than required.");
if (check) {
if (values.size() != values_.size()) {
return Status::InvalidArgument("The number of parameters does not match.");
}
} else {
if (values_.empty()) {
values_.resize(values.size());
}
for (size_t i = 0; i < values.size(); i++) {
values_[i] = std::move(values[i]);
}
} 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]);
}
return Status::OK();
}
Expand Down
3 changes: 2 additions & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ class StringValueArray : public BaseValue {

std::string Value() const override { return pstd::StringConcat(values_, delimiter_); };

Status SetValue(const std::string& value, bool check);

private:
Status SetValue(const std::string& value) override;

std::vector<std::string> values_;
char delimiter_ = 0;
};
Expand Down

0 comments on commit 3e2f46a

Please sign in to comment.