userver: formats::common Namespace Reference
Loading...
Searching...
No Matches
formats::common Namespace Reference

Detailed Description

Common utilities for all the formats.

Classes

class  ConversionStack
 Used in the implementation of functions that convert between different formats, e.g. formats::yaml::Value to formats::json::Value, etc. More...
 
class  ItemsWrapper
 Wrapper for handy python-like iteration over a map. More...
 
struct  ItemsWrapperValue
 
class  Path
 
class  TransferTag
 This tag class is used to move a ValueBuilder object without copying. More...
 

Typedefs

template<typename Value , typename T >
using ParseType = decltype(Parse(std::declval<Value>(), parse::To<T>()))
 

Enumerations

enum class  IteratorDirection {
  kForward = 1 ,
  kReverse = -1
}
 Helper to distinguish forward and reverse iterators for Value. More...
 
enum class  Type {
  kNull ,
  kArray ,
  kObject
}
 Common enum of types. More...
 

Functions

template<typename ValueTo , typename ValueFrom >
ValueTo PerformMinimalFormatConversion (ValueFrom &&value)
 Performs the conversion between different formats. Only supports basic formats node types, throws on any non-standard ones.
 
template<typename Value >
ItemsWrapper< Value > Items (Value &&value)
 Wrapper for handy python-like iteration over a map.
 
template<typename Value >
void Merge (typename Value::Builder &original, const Value &patch)
 Add to original new non-object elements from patch (overwriting the old ones, if any) and merge object elements recursively.
 
std::string GetIndexString (size_t index)
 Returns string of [idx], e.g. "[0]" or "[1025]".
 
void AppendPath (std::string &path, std::string_view key)
 
void AppendPath (std::string &path, std::size_t index)
 
std::string MakeChildPath (std::string_view parent, std::string_view key)
 
std::string MakeChildPath (std::string &&parent, std::string_view key)
 
std::string MakeChildPath (std::string_view parent, std::size_t index)
 
std::string MakeChildPath (std::string &&parent, std::size_t index)
 
template<typename Value >
std::enable_if_t< common::kIsFormatValue< Value >, Value > GetAtPath (Value parent, const std::vector< std::string > &path)
 Get the Value at path in parent.
 
template<typename ValueBuilder >
std::enable_if_t<!common::kIsFormatValue< ValueBuilder >, ValueBuilder > GetAtPath (ValueBuilder &parent, std::vector< std::string > &&path)
 Get the ValueBuilder at path in parent.
 
template<typename Value >
void SetAtPath (typename Value::Builder &parent, std::vector< std::string > &&path, Value new_value)
 Set the new_value along the path in the parent.
 
template<typename ValueBuilder >
void RemoveAtPath (ValueBuilder &parent, std::vector< std::string > &&path)
 Remove the element along the path in the parent.
 
std::vector< std::string > SplitPathString (std::string_view path)
 Split path to vector<std::string> by dots.
 
template<typename ExceptionType , typename Float >
Float ValidateFloat (Float value)
 Common restrictions to floating type values serialization. Asserts or throws on NaN and Inf values.
 

Variables

template<class Value >
constexpr bool kIsFormatValue = meta::kIsDetected<impl::IsFormatValue, Value>
 
constexpr char kPathSeparator = '.'
 
constexpr char kPathRoot [] = "/"
 
constexpr char kPathPrefixSeparator [] = ":/"
 

Typedef Documentation

◆ ParseType

template<typename Value , typename T >
using formats::common::ParseType = decltype(Parse(std::declval<Value>(), parse::To<T>()))

Definition at line 59 of file meta.hpp.

Enumeration Type Documentation

◆ IteratorDirection

Helper to distinguish forward and reverse iterators for Value.

Definition at line 8 of file iterator_direction.hpp.

◆ Type

enum class formats::common::Type
strong

Common enum of types.

Enumerator
kArray 

Value or ValueBuilder holds Null value.

kObject 

Value or ValueBuilder holds an Array.

Definition at line 13 of file type.hpp.

Function Documentation

◆ GetAtPath() [1/2]

template<typename Value >
std::enable_if_t< common::kIsFormatValue< Value >, Value > formats::common::GetAtPath ( Value parent,
const std::vector< std::string > & path )

Get the Value at path in parent.

Note
For empty path this function returns parent.
Exceptions
TypeMismatchExceptionif there is a non-object node in the middle of path

Definition at line 48 of file utils.hpp.

◆ GetAtPath() [2/2]

template<typename ValueBuilder >
std::enable_if_t<!common::kIsFormatValue< ValueBuilder >, ValueBuilder > formats::common::GetAtPath ( ValueBuilder & parent,
std::vector< std::string > && path )

Get the ValueBuilder at path in parent.

Note
path must not be empty.
Exceptions
TypeMismatchExceptionif there is a non-object node in the middle of path

Definition at line 62 of file utils.hpp.

◆ Items()

template<typename Value >
ItemsWrapper< Value > formats::common::Items ( Value && value)

Wrapper for handy python-like iteration over a map.

for (const auto& [name, value]: Items(map)) ...

To move out values:

for (auto [name, value]: Items(map)) {
vector.push_back(std::move(name));
// value is a const reference and can not be moved
}

Definition at line 111 of file items.hpp.

◆ Merge()

template<typename Value >
void formats::common::Merge ( typename Value::Builder & original,
const Value & patch )

Add to original new non-object elements from patch (overwriting the old ones, if any) and merge object elements recursively.

Note
For the missing patch, it does nothing with the original.
Arrays are not merged.

Definition at line 18 of file merge.hpp.

◆ PerformMinimalFormatConversion()

template<typename ValueTo , typename ValueFrom >
ValueTo formats::common::PerformMinimalFormatConversion ( ValueFrom && value)

Performs the conversion between different formats. Only supports basic formats node types, throws on any non-standard ones.

Note
This is intended as a building block for conversion functions. Prefer calling value.As<AnotherFormat>() or value.ConvertTo<AnotherFormat>().

Definition at line 152 of file conversion_stack.hpp.

◆ RemoveAtPath()

template<typename ValueBuilder >
void formats::common::RemoveAtPath ( ValueBuilder & parent,
std::vector< std::string > && path )

Remove the element along the path in the parent.

Note
If path is empty or there is no node at path, this function does nothing.
Exceptions
TypeMismatchExceptionif there is a non-object node in the middle of path

Definition at line 85 of file utils.hpp.

◆ SetAtPath()

template<typename Value >
void formats::common::SetAtPath ( typename Value::Builder & parent,
std::vector< std::string > && path,
Value new_value )

Set the new_value along the path in the parent.

Note
If path is empty it sets new_value to parent.
Exceptions
TypeMismatchExceptionif there is a non-object node in the middle of path

Definition at line 71 of file utils.hpp.

◆ SplitPathString()

std::vector< std::string > formats::common::SplitPathString ( std::string_view path)

Split path to vector<std::string> by dots.

Note
If path has a double dot or a dot at the beginning or end, the result will contain an empty string.
Returns an empty vector if path is empty

◆ ValidateFloat()

template<typename ExceptionType , typename Float >
Float formats::common::ValidateFloat ( Float value)

Common restrictions to floating type values serialization. Asserts or throws on NaN and Inf values.

Definition at line 17 of file validations.hpp.

Variable Documentation

◆ kIsFormatValue

template<class Value >
constexpr bool formats::common::kIsFormatValue = meta::kIsDetected<impl::IsFormatValue, Value>
inlineconstexpr

Used in Parse overloads that are templated on Value, avoids clashing with Parse from string

Definition at line 50 of file meta.hpp.

◆ kPathPrefixSeparator

constexpr char formats::common::kPathPrefixSeparator[] = ":/"
inlineconstexpr

Definition at line 15 of file path.hpp.

◆ kPathRoot

constexpr char formats::common::kPathRoot[] = "/"
inlineconstexpr

Definition at line 14 of file path.hpp.

◆ kPathSeparator

constexpr char formats::common::kPathSeparator = '.'
inlineconstexpr

Definition at line 13 of file path.hpp.