mirror of
https://github.com/matrix-construct/construct
synced 2024-09-29 12:18:54 +02:00
ircd::json: Add 'defaults' construct to iov.
This commit is contained in:
parent
bc3e4e3fce
commit
6562efb11e
2 changed files with 50 additions and 0 deletions
|
@ -54,6 +54,8 @@ struct ircd::json::iov
|
|||
struct add_if;
|
||||
struct set;
|
||||
struct set_if;
|
||||
struct defaults;
|
||||
struct defaults_if;
|
||||
|
||||
public:
|
||||
bool has(const string_view &key) const;
|
||||
|
@ -103,3 +105,19 @@ struct ircd::json::iov::set_if
|
|||
set_if(iov &, const bool &, member);
|
||||
set_if() = default;
|
||||
};
|
||||
|
||||
/// Add member to the object vector if doesn't exist; otherwise ignored
|
||||
struct ircd::json::iov::defaults
|
||||
:protected ircd::json::iov::node
|
||||
{
|
||||
defaults(iov &, member);
|
||||
defaults() = default;
|
||||
};
|
||||
|
||||
/// iov::defaults only if the bool argument is true for your condition
|
||||
struct ircd::json::iov::defaults_if
|
||||
:protected ircd::json::iov::node
|
||||
{
|
||||
defaults_if(iov &, const bool &, member);
|
||||
defaults_if() = default;
|
||||
};
|
||||
|
|
32
ircd/json.cc
32
ircd/json.cc
|
@ -434,6 +434,38 @@ ircd::json::iov::set_if::set_if(iov &iov,
|
|||
iov.pop_front();
|
||||
}
|
||||
|
||||
ircd::json::iov::defaults::defaults(iov &iov,
|
||||
member member)
|
||||
:node
|
||||
{
|
||||
iov, std::move(member)
|
||||
}
|
||||
{
|
||||
const auto count
|
||||
{
|
||||
std::count_if(std::begin(iov), std::end(iov), [&member]
|
||||
(const auto &existing)
|
||||
{
|
||||
return string_view{existing.first} == string_view{member.first};
|
||||
})
|
||||
};
|
||||
|
||||
if(count > 1)
|
||||
iov.pop_front();
|
||||
}
|
||||
|
||||
ircd::json::iov::defaults_if::defaults_if(iov &iov,
|
||||
const bool &b,
|
||||
member member)
|
||||
:node
|
||||
{
|
||||
iov, std::move(member)
|
||||
}
|
||||
{
|
||||
if(!b)
|
||||
iov.pop_front();
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// json/member.h
|
||||
|
|
Loading…
Reference in a new issue