userver: userver/chaotic/primitive.hpp Source File
Loading...
Searching...
No Matches
primitive.hpp
1#pragma once
2
3#include <userver/chaotic/convert/to.hpp>
4#include <userver/chaotic/validators.hpp>
5#include <userver/formats/parse/to.hpp>
6
7USERVER_NAMESPACE_BEGIN
8
9namespace chaotic {
10
11template <typename RawType, typename... Validators>
12struct Primitive final {
13 const RawType& value;
14};
15
16template <typename Value, typename RawType, typename... Validators>
17RawType Parse(const Value& value, formats::parse::To<Primitive<RawType, Validators...>>) {
18 auto result = value.template As<RawType>();
19 chaotic::Validate<Validators...>(result, value);
20 return result;
21}
22
23template <typename Value, typename RawType, typename... Validators>
24Value Serialize(const Primitive<RawType, Validators...>& ps, formats::serialize::To<Value>) {
25 return typename Value::Builder{ps.value}.ExtractValue();
26}
27
28} // namespace chaotic
29
30USERVER_NAMESPACE_END