userver: userver/utils/encoding/tskv_parser_read.hpp Source File
Loading...
Searching...
No Matches
tskv_parser_read.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/encoding/tskv_parser_read.hpp
4/// @brief @copybrief utils::encoding::TskvReadRecord
5
6#include <string>
7#include <string_view>
8#include <utility>
9
10#include <userver/compiler/thread_local.hpp>
11#include <userver/utils/encoding/tskv_parser.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace utils::encoding {
16
17namespace impl {
18
19struct TskvParserKvStorage {
20 std::string key{};
21 std::string value{};
22};
23
24inline compiler::ThreadLocal tskv_parser_kv_storage = [] { return TskvParserKvStorage{}; };
25
26} // namespace impl
27
28/// @brief Reads selected values of one TSKV record, borrowing unescaped values.
29/// @param parser parser that has found the record's start using
30/// @ref utils::encoding::TskvParser::SkipToRecordBegin
31/// @param select predicate `(std::string_view key) -> bool`, called for every
32/// decoded key; unselected values are skipped without decoding or copying
33/// @param consumer callback `(std::string_view key, std::string_view value) -> bool`;
34/// false skips the remaining record
35/// @note Neither callback may retain the views beyond its invocation.
36/// Keys and escaped values use reusable scratch buffers. Unescaped values
37/// borrow the parser's input. Copy any values that must survive the callback.
38/// @note Calling this function or @ref utils::encoding::TskvReadRecord
39/// from either function's callbacks on the same thread is undefined behavior,
40/// even with a different parser.
41///
42/// Usage example:
43/// @snippet universal/src/utils/encoding/tskv_parser_test.cpp sample_selected
44template <typename KeyPredicate, typename TagConsumer>
45TskvParser::RecordStatus TskvReadRecordSelected(TskvParser& parser, KeyPredicate select, TagConsumer consumer) {
46 using RecordStatus = TskvParser::RecordStatus;
47
48 auto kv_storage = impl::tskv_parser_kv_storage.Use();
49
50 while (true) {
51 if (const auto key_status = parser.ReadKey(kv_storage->key)) {
52 return *key_status;
53 }
54
55 const bool selected = select(std::string_view{kv_storage->key});
56 std::string_view value;
57 const auto value_status = selected ? parser.ReadValueView(value, kv_storage->value) : parser.SkipValue();
58 if (value_status == RecordStatus::kIncomplete) {
59 return RecordStatus::kIncomplete;
60 }
61
62 const bool record_ok = !selected || consumer(std::string_view{kv_storage->key}, value);
63 if (value_status == RecordStatus::kReachedEnd) {
64 return RecordStatus::kReachedEnd;
65 }
66 if (!record_ok) {
67 return parser.SkipToRecordEnd();
68 }
69 }
70}
71
72/// @brief Read all keys-values for 1 TSKV record.
73///
74/// @param parser parser that should have already found the start of the TSKV
75/// record using @ref utils::encoding::TskvParser::SkipToRecordBegin
76/// @param consumer callback `(std::string_view key, std::string_view value) -> bool`;
77/// false skips the remaining record
78/// @note Views are valid only for the duration of the callback.
79/// Copy any keys or values that must survive it.
80/// @note Calling this function or @ref utils::encoding::TskvReadRecordSelected
81/// from either function's callbacks on the same thread is undefined behavior,
82/// even with a different parser.
83///
84/// Usage example:
85/// @snippet universal/src/utils/encoding/tskv_parser_test.cpp sample
86template <typename TagConsumer>
87TskvParser::RecordStatus TskvReadRecord(TskvParser& parser, TagConsumer consumer) {
88 return TskvReadRecordSelected(parser, [](std::string_view) { return true; }, std::move(consumer));
89}
90
91} // namespace utils::encoding
92
93USERVER_NAMESPACE_END