2018-01-22 10:07:09 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-08-23 23:32:28 +02:00
|
|
|
|
|
|
|
#pragma once
|
2017-08-26 06:39:36 +02:00
|
|
|
#define HAVE_IRCD_JSON_TUPLE_H
|
2017-08-23 23:32:28 +02:00
|
|
|
|
2018-02-01 04:06:31 +01:00
|
|
|
#include "property.h"
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
namespace ircd {
|
|
|
|
namespace json {
|
|
|
|
|
2018-05-20 02:32:31 +02:00
|
|
|
//TODO: sort
|
|
|
|
template<class tuple> struct keys;
|
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// All tuple templates inherit from this non-template type for tagging.
|
2017-09-08 21:29:21 +02:00
|
|
|
struct tuple_base
|
|
|
|
{
|
|
|
|
// EBO tag
|
|
|
|
};
|
2017-09-08 12:15:58 +02:00
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// A compile-time construct to describe a JSON object's members and types.
|
|
|
|
///
|
2017-09-14 20:30:06 +02:00
|
|
|
/// Member access by name is O(1) because of recursive constexpr function
|
|
|
|
/// inlining when translating a name to the index number which is then used
|
|
|
|
/// as the template argument to std::get() for the value.
|
|
|
|
///
|
2017-09-12 18:37:44 +02:00
|
|
|
/// Here we represent a JSON object with a named tuple, allowing the programmer
|
|
|
|
/// to create a structure specifying all of the potentially valid members of the
|
|
|
|
/// object. Thus at runtime, the tuple only carries around its values like a
|
|
|
|
/// `struct`. Unlike a `struct`, the tuple is abstractly iterable and we have
|
|
|
|
/// implemented logic operating on all JSON tuples regardless of their makeup
|
2017-09-14 20:30:06 +02:00
|
|
|
/// without any effort from a developer when creating a new tuple.
|
2017-09-12 18:37:44 +02:00
|
|
|
///
|
|
|
|
/// The member structure for the tuple is called `property` because json::member
|
2017-09-14 20:30:06 +02:00
|
|
|
/// is already used to pair together runtime oriented json::values.
|
2017-09-12 18:37:44 +02:00
|
|
|
///
|
|
|
|
/// Create and use a tuple to efficiently extract members from a json::object.
|
|
|
|
/// The tuple will populate its own members during a single-pass iteration of
|
2017-09-19 11:09:19 +02:00
|
|
|
/// the JSON input.
|
|
|
|
///
|
|
|
|
/// But remember, the tuple carries very little information for you at runtime
|
|
|
|
/// which may make it difficult to represent all JS phenomena like "undefined"
|
|
|
|
/// and "null".
|
2017-09-12 18:37:44 +02:00
|
|
|
///
|
2017-08-23 23:32:28 +02:00
|
|
|
template<class... T>
|
2017-08-26 06:39:36 +02:00
|
|
|
struct tuple
|
2017-09-08 12:15:58 +02:00
|
|
|
:std::tuple<T...>
|
2017-09-08 21:29:21 +02:00
|
|
|
,tuple_base
|
2017-08-23 23:32:28 +02:00
|
|
|
{
|
|
|
|
using tuple_type = std::tuple<T...>;
|
2017-08-27 01:30:49 +02:00
|
|
|
using super_type = tuple<T...>;
|
2017-08-23 23:32:28 +02:00
|
|
|
|
2017-12-12 21:23:20 +01:00
|
|
|
static constexpr size_t size();
|
|
|
|
|
2017-09-19 11:09:19 +02:00
|
|
|
operator json::value() const;
|
2017-10-16 06:22:52 +02:00
|
|
|
operator crh::sha256::buf() const;
|
2017-09-19 11:09:19 +02:00
|
|
|
|
2019-01-17 02:22:09 +01:00
|
|
|
template<class... U> explicit tuple(const tuple<U...> &);
|
|
|
|
template<class U> explicit tuple(const json::object &, const json::keys<U> &);
|
2019-01-17 03:35:30 +01:00
|
|
|
template<class U> explicit tuple(const tuple &, const json::keys<U> &);
|
2017-08-27 01:30:49 +02:00
|
|
|
tuple(const json::object &);
|
2017-09-09 21:20:00 +02:00
|
|
|
tuple(const json::iov &);
|
2019-03-29 04:15:22 +01:00
|
|
|
tuple(const json::members &);
|
2017-08-27 01:30:49 +02:00
|
|
|
tuple() = default;
|
2017-08-23 23:32:28 +02:00
|
|
|
};
|
|
|
|
|
2017-09-08 21:29:21 +02:00
|
|
|
template<class tuple>
|
|
|
|
constexpr bool
|
|
|
|
is_tuple()
|
|
|
|
{
|
|
|
|
return std::is_base_of<tuple_base, tuple>::value;
|
|
|
|
}
|
|
|
|
|
2017-11-25 22:07:10 +01:00
|
|
|
template<class tuple,
|
|
|
|
class R>
|
2017-09-08 21:29:21 +02:00
|
|
|
using enable_if_tuple = typename std::enable_if<is_tuple<tuple>(), R>::type;
|
|
|
|
|
2017-11-25 22:07:10 +01:00
|
|
|
template<class tuple,
|
|
|
|
class test,
|
|
|
|
class R>
|
|
|
|
using enable_if_tuple_and = typename std::enable_if<is_tuple<tuple>() && test(), R>::type;
|
|
|
|
|
2017-08-26 06:39:36 +02:00
|
|
|
template<class tuple>
|
|
|
|
using tuple_type = typename tuple::tuple_type;
|
2017-08-23 23:32:28 +02:00
|
|
|
|
2017-08-26 06:39:36 +02:00
|
|
|
template<class tuple>
|
|
|
|
using tuple_size = std::tuple_size<tuple_type<tuple>>;
|
2017-08-23 23:32:28 +02:00
|
|
|
|
2017-08-27 01:30:49 +02:00
|
|
|
template<class tuple,
|
|
|
|
size_t i>
|
2017-08-26 06:39:36 +02:00
|
|
|
using tuple_element = typename std::tuple_element<i, tuple_type<tuple>>::type;
|
2017-08-23 23:32:28 +02:00
|
|
|
|
2017-08-27 01:30:49 +02:00
|
|
|
template<class tuple,
|
|
|
|
size_t i>
|
|
|
|
using tuple_value_type = typename tuple_element<tuple, i>::value_type;
|
|
|
|
|
2017-08-26 06:39:36 +02:00
|
|
|
template<class tuple>
|
2017-08-27 01:30:49 +02:00
|
|
|
auto &
|
2017-08-26 06:39:36 +02:00
|
|
|
stdcast(const tuple &o)
|
2017-08-23 23:32:28 +02:00
|
|
|
{
|
2017-08-26 06:39:36 +02:00
|
|
|
return static_cast<const typename tuple::tuple_type &>(o);
|
2017-08-23 23:32:28 +02:00
|
|
|
}
|
|
|
|
|
2017-08-26 06:39:36 +02:00
|
|
|
template<class tuple>
|
2017-08-27 01:30:49 +02:00
|
|
|
auto &
|
2017-08-26 06:39:36 +02:00
|
|
|
stdcast(tuple &o)
|
2017-08-23 23:32:28 +02:00
|
|
|
{
|
2017-08-26 06:39:36 +02:00
|
|
|
return static_cast<typename tuple::tuple_type &>(o);
|
2017-08-23 23:32:28 +02:00
|
|
|
}
|
|
|
|
|
2017-08-27 01:30:49 +02:00
|
|
|
template<class tuple>
|
2017-09-08 21:29:21 +02:00
|
|
|
constexpr enable_if_tuple<tuple, size_t>
|
2017-08-27 01:30:49 +02:00
|
|
|
size()
|
2017-08-23 23:32:28 +02:00
|
|
|
{
|
2017-08-27 01:30:49 +02:00
|
|
|
return tuple_size<tuple>::value;
|
|
|
|
}
|
|
|
|
|
2018-05-07 06:31:32 +02:00
|
|
|
} // namespace json
|
|
|
|
} // namespace ircd
|
2017-10-05 01:25:15 +02:00
|
|
|
|
2018-05-20 02:33:28 +02:00
|
|
|
#include "key.h"
|
2018-05-07 06:31:32 +02:00
|
|
|
#include "indexof.h"
|
2017-10-05 01:25:15 +02:00
|
|
|
|
2018-05-07 06:31:32 +02:00
|
|
|
namespace ircd {
|
|
|
|
namespace json {
|
2017-09-19 11:09:19 +02:00
|
|
|
|
2017-08-27 01:30:49 +02:00
|
|
|
template<size_t i,
|
2017-09-08 21:29:21 +02:00
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, tuple_value_type<tuple, i> &>
|
2017-10-05 01:25:15 +02:00
|
|
|
val(tuple &t)
|
2017-08-23 23:32:28 +02:00
|
|
|
{
|
2017-10-05 01:25:15 +02:00
|
|
|
return static_cast<tuple_value_type<tuple, i> &>(std::get<i>(t));
|
2017-08-23 23:32:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t i,
|
2017-09-08 21:29:21 +02:00
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, const tuple_value_type<tuple, i> &>
|
2017-10-05 01:25:15 +02:00
|
|
|
val(const tuple &t)
|
2017-08-23 23:32:28 +02:00
|
|
|
{
|
2017-10-05 01:25:15 +02:00
|
|
|
return static_cast<const tuple_value_type<tuple, i> &>(std::get<i>(t));
|
2017-08-23 23:32:28 +02:00
|
|
|
}
|
|
|
|
|
2018-05-07 06:31:32 +02:00
|
|
|
template<class tuple>
|
|
|
|
constexpr bool
|
|
|
|
key_exists(const string_view &key)
|
2017-09-21 04:33:50 +02:00
|
|
|
{
|
2018-05-07 06:31:32 +02:00
|
|
|
return indexof<tuple>(key) < size<tuple>();
|
2017-09-21 04:33:50 +02:00
|
|
|
}
|
|
|
|
|
2018-05-07 06:31:32 +02:00
|
|
|
} // namespace json
|
|
|
|
} // namespace ircd
|
2017-09-21 04:33:50 +02:00
|
|
|
|
2018-05-07 06:31:32 +02:00
|
|
|
#include "get.h"
|
|
|
|
#include "at.h"
|
|
|
|
#include "for_each.h"
|
|
|
|
#include "until.h"
|
|
|
|
#include "set.h"
|
2017-09-21 04:33:50 +02:00
|
|
|
|
2018-05-07 06:31:32 +02:00
|
|
|
namespace ircd {
|
|
|
|
namespace json {
|
2017-08-23 23:32:28 +02:00
|
|
|
|
2019-01-16 23:49:07 +01:00
|
|
|
template<class... T>
|
|
|
|
template<class U>
|
|
|
|
tuple<T...>::tuple(const json::object &object,
|
|
|
|
const json::keys<U> &keys)
|
|
|
|
{
|
|
|
|
std::for_each(std::begin(object), std::end(object), [this, &keys]
|
|
|
|
(const auto &member)
|
|
|
|
{
|
|
|
|
if(keys.has(member.first))
|
|
|
|
set(*this, member.first, member.second);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-27 01:30:49 +02:00
|
|
|
template<class... T>
|
|
|
|
tuple<T...>::tuple(const json::object &object)
|
2017-08-26 06:39:36 +02:00
|
|
|
{
|
2017-08-27 01:30:49 +02:00
|
|
|
std::for_each(std::begin(object), std::end(object), [this]
|
|
|
|
(const auto &member)
|
2017-08-26 06:39:36 +02:00
|
|
|
{
|
2017-09-21 04:33:50 +02:00
|
|
|
set(*this, member.first, member.second);
|
2017-08-26 06:39:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-08 21:29:21 +02:00
|
|
|
template<class... T>
|
2017-09-09 21:20:00 +02:00
|
|
|
tuple<T...>::tuple(const json::iov &iov)
|
2017-09-08 21:29:21 +02:00
|
|
|
{
|
2017-09-12 19:03:06 +02:00
|
|
|
std::for_each(std::begin(iov), std::end(iov), [this]
|
2017-09-08 21:29:21 +02:00
|
|
|
(const auto &member)
|
|
|
|
{
|
2017-09-21 04:33:50 +02:00
|
|
|
set(*this, member.first, member.second);
|
2017-09-08 21:29:21 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
template<class... T>
|
2019-03-29 04:15:22 +01:00
|
|
|
tuple<T...>::tuple(const json::members &members)
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
|
|
|
std::for_each(std::begin(members), std::end(members), [this]
|
|
|
|
(const auto &member)
|
|
|
|
{
|
2017-09-21 04:33:50 +02:00
|
|
|
set(*this, member.first, member.second);
|
2017-09-08 10:41:02 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-17 03:35:30 +01:00
|
|
|
template<class... T>
|
|
|
|
template<class U>
|
|
|
|
tuple<T...>::tuple(const tuple &t,
|
|
|
|
const keys<U> &keys)
|
|
|
|
{
|
|
|
|
for_each(t, [this, &keys]
|
|
|
|
(const auto &key, const auto &val)
|
|
|
|
{
|
|
|
|
if(keys.has(key))
|
|
|
|
set(*this, key, val);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-27 21:25:39 +02:00
|
|
|
template<class... T>
|
|
|
|
template<class... U>
|
|
|
|
tuple<T...>::tuple(const tuple<U...> &t)
|
|
|
|
{
|
|
|
|
for_each(t, [this]
|
|
|
|
(const auto &key, const auto &val)
|
|
|
|
{
|
|
|
|
set(*this, key, val);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-13 22:32:35 +01:00
|
|
|
template<class... T>
|
|
|
|
constexpr size_t
|
|
|
|
tuple<T...>::size()
|
|
|
|
{
|
|
|
|
return std::tuple_size<tuple_type>();
|
|
|
|
}
|
|
|
|
|
2018-05-07 06:31:32 +02:00
|
|
|
} // namespace json
|
|
|
|
} // namespace ircd
|
2017-12-12 21:22:41 +01:00
|
|
|
|
2018-11-14 09:58:19 +01:00
|
|
|
#include "_key_transform.h"
|
2018-05-07 06:31:32 +02:00
|
|
|
#include "keys.h"
|
2018-11-14 09:58:19 +01:00
|
|
|
#include "_member_transform.h"
|
2017-12-12 21:22:41 +01:00
|
|
|
|
2018-05-07 06:31:32 +02:00
|
|
|
namespace ircd {
|
|
|
|
namespace json {
|
2017-12-12 21:22:41 +01:00
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
template<class... T>
|
|
|
|
size_t
|
2017-09-08 16:47:07 +02:00
|
|
|
serialized(const tuple<T...> &t)
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
constexpr const size_t member_count
|
|
|
|
{
|
|
|
|
tuple<T...>::size()
|
|
|
|
};
|
|
|
|
|
2017-10-12 03:15:09 +02:00
|
|
|
std::array<size_t, member_count> sizes {0};
|
2017-09-28 03:27:25 +02:00
|
|
|
const auto e{_member_transform_if(t, begin(sizes), end(sizes), []
|
2017-10-12 03:15:09 +02:00
|
|
|
(auto &ret, const string_view &key, auto&& val)
|
2017-09-08 16:47:07 +02:00
|
|
|
{
|
2018-04-12 07:57:10 +02:00
|
|
|
const json::value value(val);
|
|
|
|
if(!defined(value))
|
2017-09-28 03:27:25 +02:00
|
|
|
return false;
|
|
|
|
|
2018-04-12 07:57:10 +02:00
|
|
|
ret = 1 + key.size() + 1 + 1 + serialized(value) + 1;
|
2017-09-28 03:27:25 +02:00
|
|
|
return true;
|
|
|
|
})};
|
2017-09-08 16:47:07 +02:00
|
|
|
|
2017-09-28 03:27:25 +02:00
|
|
|
// Subtract one to get the final size when an extra comma is
|
|
|
|
// accumulated on non-empty objects.
|
2017-10-12 03:15:09 +02:00
|
|
|
const auto overhead
|
|
|
|
{
|
|
|
|
1 + std::all_of(begin(sizes), e, is_zero{})
|
|
|
|
};
|
|
|
|
|
|
|
|
return std::accumulate(begin(sizes), e, size_t(overhead));
|
2017-09-08 10:41:02 +02:00
|
|
|
}
|
|
|
|
|
2018-02-28 11:23:32 +01:00
|
|
|
template<class... T>
|
|
|
|
size_t
|
|
|
|
serialized(const tuple<T...> *const &b,
|
|
|
|
const tuple<T...> *const &e)
|
|
|
|
{
|
|
|
|
size_t ret(1 + (b == e));
|
|
|
|
return std::accumulate(b, e, ret, []
|
|
|
|
(size_t ret, const tuple<T...> &t)
|
|
|
|
{
|
|
|
|
return ret += serialized(t) + 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
template<class... T>
|
2017-09-08 16:47:07 +02:00
|
|
|
string_view
|
|
|
|
stringify(mutable_buffer &buf,
|
|
|
|
const tuple<T...> &tuple)
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
2017-09-08 12:08:29 +02:00
|
|
|
std::array<member, tuple.size()> members;
|
2017-09-28 03:27:25 +02:00
|
|
|
const auto e{_member_transform_if(tuple, begin(members), end(members), []
|
2017-10-12 03:15:09 +02:00
|
|
|
(auto &ret, const string_view &key, auto&& val)
|
2017-09-28 03:27:25 +02:00
|
|
|
{
|
2018-04-12 07:57:10 +02:00
|
|
|
json::value value(val);
|
|
|
|
if(!defined(value))
|
2017-09-28 03:27:25 +02:00
|
|
|
return false;
|
|
|
|
|
2018-04-12 07:57:10 +02:00
|
|
|
ret = member { key, std::move(value) };
|
2017-09-28 03:27:25 +02:00
|
|
|
return true;
|
|
|
|
})};
|
|
|
|
|
|
|
|
return stringify(buf, begin(members), e);
|
2017-09-08 10:41:02 +02:00
|
|
|
}
|
|
|
|
|
2018-02-28 11:23:32 +01:00
|
|
|
template<class... T>
|
|
|
|
string_view
|
|
|
|
stringify(mutable_buffer &buf,
|
|
|
|
const tuple<T...> *b,
|
|
|
|
const tuple<T...> *e)
|
|
|
|
{
|
|
|
|
const auto start(begin(buf));
|
|
|
|
consume(buf, copy(buf, "["_sv));
|
|
|
|
if(b != e)
|
|
|
|
{
|
|
|
|
stringify(buf, *b);
|
|
|
|
for(++b; b != e; ++b)
|
|
|
|
{
|
|
|
|
consume(buf, copy(buf, ","_sv));
|
|
|
|
stringify(buf, *b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
consume(buf, copy(buf, "]"_sv));
|
|
|
|
return { start, begin(buf) };
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
template<class... T>
|
|
|
|
std::ostream &
|
|
|
|
operator<<(std::ostream &s, const tuple<T...> &t)
|
|
|
|
{
|
2017-10-16 06:18:42 +02:00
|
|
|
s << json::strung(t);
|
2017-09-08 10:41:02 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-10-16 06:22:52 +02:00
|
|
|
template<class... T>
|
|
|
|
tuple<T...>::operator
|
|
|
|
crh::sha256::buf()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
//TODO: XXX
|
|
|
|
const auto preimage
|
|
|
|
{
|
|
|
|
json::strung(*this)
|
|
|
|
};
|
|
|
|
|
|
|
|
return crh::sha256::buf
|
|
|
|
{
|
|
|
|
[&preimage](auto &buf)
|
|
|
|
{
|
2018-02-03 08:20:26 +01:00
|
|
|
sha256{buf, const_buffer{preimage}};
|
2017-10-16 06:22:52 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-09-19 11:09:19 +02:00
|
|
|
template<class... T>
|
|
|
|
tuple<T...>::operator
|
|
|
|
json::value()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
json::value ret;
|
|
|
|
ret.type = OBJECT;
|
|
|
|
ret.create_string(serialized(*this), [this]
|
|
|
|
(mutable_buffer buffer)
|
|
|
|
{
|
|
|
|
stringify(buffer, *this);
|
|
|
|
});
|
|
|
|
|
2018-11-14 09:58:19 +01:00
|
|
|
return ret;
|
2017-09-19 11:09:19 +02:00
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
} // namespace json
|
|
|
|
} // namespace ircd
|