Apply minor change to iterator, add handling of invalid header

This commit is contained in:
ado 2023-08-06 15:55:23 +02:00
parent a378998e34
commit a152a8cb4a
2 changed files with 31 additions and 6 deletions

View File

@ -89,7 +89,6 @@ public:
return valid() ? reader_.line_number_ - 1 : 0; return valid() ? reader_.line_number_ - 1 : 0;
} }
// TODO append file_name and line number to exception
template <typename T, typename... Ts> template <typename T, typename... Ts>
no_void_validator_tup_t<T, Ts...> get_next() { no_void_validator_tup_t<T, Ts...> get_next() {
std::optional<std::string> error; std::optional<std::string> error;
@ -209,12 +208,15 @@ public:
using value = std::conditional_t<get_object, T, using value = std::conditional_t<get_object, T,
no_void_validator_tup_t<T, Ts...>>; no_void_validator_tup_t<T, Ts...>>;
iterator() : parser_{nullptr} { iterator() : parser_{nullptr}, value_{} {
} }
iterator(parser<Options...>* parser) : parser_{parser} { iterator(parser<Options...>* parser) : parser_{parser}, value_{} {
} }
iterator(const iterator& other) = default;
iterator(iterator&& other) = default;
value& operator*() { value& operator*() {
return value_; return value_;
} }
@ -224,7 +226,7 @@ public:
} }
iterator& operator++() { iterator& operator++() {
if (parser_->eof()) { if (!parser_ || parser_->eof()) {
parser_ = nullptr; parser_ = nullptr;
} else { } else {
if constexpr (get_object) { if constexpr (get_object) {
@ -253,8 +255,8 @@ public:
} }
private: private:
value value_;
parser<Options...>* parser_; parser<Options...>* parser_;
value value_;
}; };
iterable(parser<Options...>* parser) : parser_{parser} { iterable(parser<Options...>* parser) : parser_{parser} {
@ -465,7 +467,14 @@ private:
std::string raw_header_copy = raw_header_; std::string raw_header_copy = raw_header_;
splitter.split(raw_header_copy.data(), reader_.delim_); splitter.split(raw_header_copy.data(), reader_.delim_);
for (const auto& [begin, end] : splitter.split_data_) { for (const auto& [begin, end] : splitter.split_data_) {
header_.emplace_back(begin, end); std::string field{begin, end};
if (std::find(header_.begin(), header_.end(), field) !=
header_.end()) {
handle_error_invalid_header(field);
header_.clear();
return;
}
header_.push_back(std::move(field));
} }
} }
@ -597,6 +606,19 @@ private:
} }
} }
void handle_error_invalid_header(const std::string& field) {
constexpr static auto error_msg = "header contains duplicates: ";
if constexpr (string_error) {
error_.clear();
error_.append(error_msg).append(error_msg);
} else if constexpr (throw_on_error) {
throw ss::exception{error_msg + field};
} else {
error_ = true;
}
}
void decorate_rethrow(const ss::exception& e) const { void decorate_rethrow(const ss::exception& e) const {
static_assert(throw_on_error, static_assert(throw_on_error,
"throw_on_error needs to be enabled to use this method"); "throw_on_error needs to be enabled to use this method");

View File

@ -1286,6 +1286,9 @@ void test_invalid_fields(const std::vector<std::string>& lines,
TEST_CASE("parser test invalid header fields usage") { TEST_CASE("parser test invalid header fields usage") {
test_invalid_fields({"Int,String,Double", "1,hi,2.34"}, test_invalid_fields({"Int,String,Double", "1,hi,2.34"},
{"Int", "String", "Double"}); {"Int", "String", "Double"});
test_invalid_fields({"Int,Int,Int", "1,2,3"},
{"Int", "Int", "Int"});
} }
template <typename... Ts> template <typename... Ts>