diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/yson_pull/event.h | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/yson_pull/event.h')
-rw-r--r-- | library/cpp/yson_pull/event.h | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/library/cpp/yson_pull/event.h b/library/cpp/yson_pull/event.h new file mode 100644 index 0000000000..b41d5ea3b5 --- /dev/null +++ b/library/cpp/yson_pull/event.h @@ -0,0 +1,85 @@ +#pragma once + +#include "cyson_enums.h" +#include "scalar.h" + +#include <util/generic/strbuf.h> +#include <util/system/types.h> +#include <util/system/yassert.h> + +namespace NYsonPull { + //! A well-formed decoded YSON stream can be described by the following grammar: + //! + //! STREAM[node] ::= begin_stream VALUE end_stream + //! STREAM[list_fragment] ::= begin_stream LIST_FRAGMENT end_stream + //! STREAM[map_fragment] ::= begin_stream MAP_FRAGMENT end_stream + //! LIST_FRAGMENT ::= { VALUE; } + //! MAP_FRAGMENT ::= { KEY VALUE; } + //! KEY ::= key(String) + //! VALUE ::= VALUE_NOATTR | ATTRIBUTES VALUE_NOATTR + //! ATTRIBUTES ::= begin_attributes MAP_FRAGMENT end_attributes + //! VALUE_NOATTR ::= scalar(Scalar) | LIST | MAP + //! LIST ::= begin_list LIST_FRAGMENT end_list + //! MAP ::= begin_map MAP_FRAGMENT end_map + + //! \brief YSON event type tag. Corresponds to YSON grammar. + enum class EEventType { + BeginStream = YSON_EVENT_BEGIN_STREAM, + EndStream = YSON_EVENT_END_STREAM, + BeginList = YSON_EVENT_BEGIN_LIST, + EndList = YSON_EVENT_END_LIST, + BeginMap = YSON_EVENT_BEGIN_MAP, + EndMap = YSON_EVENT_END_MAP, + BeginAttributes = YSON_EVENT_BEGIN_ATTRIBUTES, + EndAttributes = YSON_EVENT_END_ATTRIBUTES, + Key = YSON_EVENT_KEY, + Scalar = YSON_EVENT_SCALAR, + }; + + //! \brief YSON event variant type. + class TEvent { + EEventType Type_; + TScalar Value_; + + public: + //! \brief Construct a tag-only event. + explicit constexpr TEvent(EEventType type = EEventType::BeginStream) + : Type_{type} { + } + + //! \brief Construct a tag+value event. + //! + //! Only \p EEventType::key is meaningful. + constexpr TEvent(EEventType type, const TScalar& value) + : Type_{type} + , Value_{value} { + } + + //! \brief Construct a \p EEventType::scalar event. + explicit constexpr TEvent(const TScalar& value) + : Type_{EEventType::Scalar} + , Value_{value} { + } + + EEventType Type() const { + return Type_; + } + + //! \brief Get TScalar value. + //! + //! Undefined behaviour when event type is not \p EEventType::scalar. + const TScalar& AsScalar() const { + Y_ASSERT(Type_ == EEventType::Scalar || Type_ == EEventType::Key); + return Value_; + } + + //! \brief Get string value. + //! + //! Undefined behaviour when event type is not \p EEventType::key. + TStringBuf AsString() const { + Y_ASSERT(Type_ == EEventType::Key || (Type_ == EEventType::Scalar && Value_.Type() == EScalarType::String)); + return Value_.AsString(); + } + }; + +} |