/* Copyright 2022, Michele Santullo
* This file is part of duckticker.
*
* Wrenpp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Wrenpp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with duckticker. If not, see .
*/
#pragma once
#include
#include
#include
namespace duck {
namespace detail {
template
inline std::optional get_from_node(simdjson::ondemand::object& node, Key&& key, Keys&&... keys) {
auto item_result = node[key];
if (item_result.error())
return {};
else {
if constexpr (sizeof...(Keys) > 0) {
simdjson::ondemand::object item = item_result;
return get_from_node(item, std::forward(keys)...);
}
else {
return static_cast(item_result);
}
}
}
} //namespace detail
class BitcoinityPayload {
public:
static constexpr std::string_view JsonKeyPayload = "payload";
BitcoinityPayload(
simdjson::padded_string&& body_p,
simdjson::ondemand::parser&& parser_p
);
BitcoinityPayload(BitcoinityPayload&& other);
BitcoinityPayload& operator= (BitcoinityPayload&& other);
template
std::optional get (Keys&&... keys) const {
return detail::get_from_node(m_payload_node, std::forward(keys)...);
}
template
std::optional get_string (Keys&&... keys) const {
return this->get(std::forward(keys)...);
}
template
std::optional get_double (Keys&&... keys) const {
return this->get(std::forward(keys)...);
}
template
std::string_view get_string_or_empty (Keys&&... keys) const {
auto retval = this->get_string(std::forward(keys)...);
if (retval)
return *retval;
else
return {};
}
template
std::optional get_uint64 (Keys&&... keys) const {
return this->get(std::forward(keys)...);
}
template
bool has_key (Keys&&... keys) const {
return !!this->get(std::forward(keys)...);
}
private:
simdjson::padded_string m_body;
mutable simdjson::ondemand::parser m_parser;
mutable simdjson::ondemand::document m_document;
mutable simdjson::ondemand::object m_payload_node;
};
} //namespace duck