22#include <fmt/format.h>
24#include <userver/compiler/demangle.hpp>
25#include <userver/formats/parse/may_parse.hpp>
26#include <userver/formats/parse/to.hpp>
28USERVER_NAMESPACE_BEGIN
30namespace formats::
json {
34namespace formats::
parse {
36template <
typename ParseException,
typename Variant,
typename TypeA>
37[[noreturn]]
void ThrowVariantAmbiguousParse(std::string_view path, std::type_index type_b) {
38 throw ParseException(
fmt::format(
39 "Value of '{}' is ambiguous, it is parseable into multiple variants of '{}', at least '{}' and '{}'",
47template <
class ParseException,
typename Variant>
48[[noreturn]]
void ThrowVariantParseException(std::string_view path, std::string_view errors) {
49 throw ParseException(
fmt::format(
50 "Value of '{}' cannot be parsed as {}. Issues during parsing follow: {}",
60std::string FormatVariantParseError(
const std::exception& e) {
61 return fmt::format(
"\n* Failed to parse as {}: {}",
compiler::GetTypeName<T>(), e.what());
64template <
class T,
class Value,
typename Result>
65void ParseVariantSingle(
const Value& value, std::optional<Result>& result, std::string& error) {
66 if (!formats::
parse::MayParseAs<T>(value)) {
71 const auto old_type = std::visit([](
const auto& v) -> std::type_index {
return typeid(v); }, *result);
73 value.
template As<T>();
74 }
catch (
const std::exception&) {
78 using ParseException =
typename Value::ParseException;
79 formats::
parse::ThrowVariantAmbiguousParse<ParseException, Result, T>(value.GetPath(), old_type);
83 result = value.
template As<T>();
84 }
catch (
const std::exception& e) {
85 error = impl::FormatVariantParseError<T>(e);
90template <
class T,
class Value>
91void ParseRejectedVariantSingle(
const Value& value, std::string& error) {
92 if (formats::
parse::MayParseAs<T>(value)) {
97 value.
template As<T>();
98 }
catch (
const std::exception& e) {
99 error = impl::FormatVariantParseError<T>(e);
103template <
typename... Types,
class Value,
typename Result, std::size_t... Indices>
105ParseVariantAlternatives(
const Value& value, std::optional<Result>& result, std::index_sequence<Indices...>) {
106 std::array<std::string,
sizeof...(Types)> errors;
107 (impl::ParseVariantSingle<Types>(value, result, errors[Indices]), ...);
112 (impl::ParseRejectedVariantSingle<Types>(value, errors[Indices]), ...);
113 return (std::string{} + ... + errors[Indices]);
118template <
class Value,
typename... Types>
119std::variant<
decltype(Parse(std::declval<Value>(),
To<Types>{}))...>
120Parse(
const Value& value, formats::
parse::
To<std::variant<Types...>>) {
121 using Result = std::variant<
decltype(Parse(std::declval<Value>(),
To<Types>{}))...>;
122 std::optional<Result> result;
126 if constexpr (std::is_same_v<Value, formats::
json::
Value>) {
128 value2.DropRootPath();
129 errors = impl::ParseVariantAlternatives<Types...>(value2, result, std::index_sequence_for<Types...>{});
131 errors = impl::ParseVariantAlternatives<Types...>(value, result, std::index_sequence_for<Types...>{});
135 formats::
parse::ThrowVariantParseException<
136 typename Value::ParseException,
137 std::variant<Types...>>(value.GetPath(), errors);
140 return std::move(*result);