userver: userver/formats/common/merge.hpp Source File
Loading...
Searching...
No Matches
merge.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/formats/common/merge.hpp
4/// @brief @copybrief formats::json::MergeSchemas
5/// @ingroup userver_universal
6
7#include <userver/formats/common/items.hpp>
8
9USERVER_NAMESPACE_BEGIN
10
11namespace formats::common {
12
13/// @brief Add to `original` new non-object elements from `patch` (overwriting
14/// the old ones, if any) and merge object elements recursively.
15/// @note For the missing `patch`, it does nothing with the `original`.
16/// @note Arrays are not merged.
17template <typename Value>
18void Merge(typename Value::Builder& original, const Value& patch) {
19 if (patch.IsObject() && original.IsObject() && !original.IsEmpty()) {
20 for (const auto& [elem_key, elem_value] : common::Items(patch)) {
21 auto next_origin = original[elem_key];
22 Merge(next_origin, elem_value);
23 }
24 } else if (!patch.IsMissing()) {
25 original = patch;
26 }
27}
28
29} // namespace formats::common
30
31USERVER_NAMESPACE_END