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,
18 formats::parse::To<Primitive<RawType, Validators...>>) {
19 auto result = value.template As<RawType>();
20 chaotic::Validate<Validators...>(result, value);
21 return result;
22}
23
24template <typename Value, typename RawType, typename... Validators>
25Value Serialize(const Primitive<RawType, Validators...>& ps,
26 formats::serialize::To<Value>) {
27 return typename Value::Builder{ps.value}.ExtractValue();
28}
29
30} // namespace chaotic
31
32USERVER_NAMESPACE_END