Skip to content

Commit

Permalink
- json: classes' key default changed to 'simple'
Browse files Browse the repository at this point in the history
- json: simple-key => trivial-key
  • Loading branch information
oknenavin committed Jun 30, 2024
1 parent 85142e6 commit a00326c
Show file tree
Hide file tree
Showing 15 changed files with 30 additions and 106 deletions.
30 changes: 15 additions & 15 deletions src/cxon/lang/common/cio/class.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ namespace cxon { namespace cio { namespace cls { // traits
template <typename T>
struct is_bare_class;

// simple-key-class support types
struct cxon_simple_key_class_tag {};
// trivial-key-class support types
struct cxon_nontrivial_key_class_tag {};
template <typename T>
struct is_simple_key_class;
struct is_trivial_key_class;
template <typename X, typename ...Fs>
constexpr bool are_simple_key_fields(Fs&&... fs);
constexpr bool are_trivial_key_fields(Fs&&... fs);

// ordered-keys-class support types
struct cxon_ordered_keys_class_tag {};
Expand Down Expand Up @@ -78,34 +78,34 @@ namespace cxon { namespace cio { namespace cls {
template <typename T>
struct is_bare_class : imp::is_bare_class_<T> {};

// simple-key-class support types
// trivial-key-class support types

namespace imp {
template <typename T, typename E = void_t<>>
struct is_simple_key_class_ : std::false_type {};
struct is_trivial_key_class_ : std::true_type {};
template <typename T>
struct is_simple_key_class_<T, void_t<typename T::cxon_simple_key_class_tag>> : std::true_type {};
struct is_trivial_key_class_<T, void_t<typename T::cxon_nontrivial_key_class_tag>> : std::false_type {};
}
template <typename T>
struct is_simple_key_class : imp::is_simple_key_class_<T> {};
struct is_trivial_key_class : imp::is_trivial_key_class_<T> {};

namespace imp {
template <typename X>
constexpr bool is_simple_key_(const char* s, std::size_t n, unsigned i = 0) {
return i == n || (!chr::imp::should_escape_<X>(s[i]) && is_simple_key_<X>(s, n, i + 1));
constexpr bool is_trivial_key_(const char* s, std::size_t n, unsigned i = 0) {
return i == n || (!chr::imp::should_escape_<X>(s[i]) && is_trivial_key_<X>(s, n, i + 1));
}
template <typename X>
constexpr bool are_simple_key_fields_() {
constexpr bool are_trivial_key_fields_() {
return true;
}
template <typename X, typename F, typename ...Fs>
constexpr bool are_simple_key_fields_(F&& f, Fs&&... fs) {
return is_simple_key_<X>(f.name, f.nale) && are_simple_key_fields_<X>(std::forward<Fs>(fs)...);
constexpr bool are_trivial_key_fields_(F&& f, Fs&&... fs) {
return is_trivial_key_<X>(f.name, f.nale) && are_trivial_key_fields_<X>(std::forward<Fs>(fs)...);
}
}
template <typename X, typename ...Fs>
constexpr bool are_simple_key_fields(Fs&&... fs) {
return imp::are_simple_key_fields_<X>(std::forward<Fs>(fs)...);
constexpr bool are_trivial_key_fields(Fs&&... fs) {
return imp::are_trivial_key_fields_<X>(std::forward<Fs>(fs)...);
}

// ordered-keys-class support types
Expand Down
20 changes: 12 additions & 8 deletions src/cxon/lang/json/class.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ namespace cxon { // cio::val::sink read
#define CXON_JSON_CLS_FIELD_ASIS_DFLT(F, ...) CXON_JSON_CLS_FIELD_DFLT(CXON_T_, #F, F, __VA_ARGS__)
#define CXON_JSON_CLS_FIELD_SKIP(N) cxon::json::cls::make_field<CXON_T_>(N, {})

#define CXON_JSON_CLS_SIMPLE_KEY(T)\
#define CXON_JSON_CLS_NONTRIVIAL_KEY(T)\
namespace cxon { namespace cio { namespace cls { \
template <> struct is_simple_key_class<T> : std::true_type {}; \
template <> struct is_trivial_key_class<T> : std::false_type {}; \
}}}

#define CXON_JSON_CLS_READ(Type, ...)\
Expand All @@ -65,7 +65,8 @@ namespace cxon { // cio::val::sink read
inline auto read_value(Type& t, II& i, II e, Cx& cx) -> enable_for_t<X, JSON> { \
using CXON_T_ = Type; \
static CXON_CXX17_CONSTEXPR auto fs = json::cls::make_fields(__VA_ARGS__); \
CXON_IF_CONSTEXPR (cio::cls::is_simple_key_class<Type>::value || cio::cls::are_simple_key_fields<X>(__VA_ARGS__)) { \
CXON_IF_CONSTEXPR (cio::cls::is_trivial_key_class<Type>::value) { \
/*static_assert(cio::cls::are_trivial_key_fields<X>(__VA_ARGS__), "unexpected, use nontrivial key(s)");*/ \
using Y = bind_traits_t<X, cio::key::simple_traits>; \
/*CXON_IF_CONSTEXPR (cio::cls::is_ordered_keys_class<Type>::value || cio::cls::are_fields_ordered(__VA_ARGS__)) { \
using Y = bind_traits_t<Y, cio::cls::ordered_keys_traits>; \
Expand All @@ -86,7 +87,8 @@ namespace cxon { // cio::val::sink read
inline auto write_value(O& o, const Type& t, Cx& cx) -> enable_for_t<X, JSON> { \
using CXON_T_ = Type; \
static CXON_CXX17_CONSTEXPR auto fs = json::cls::make_fields(__VA_ARGS__); \
CXON_IF_CONSTEXPR (cio::cls::is_simple_key_class<Type>::value || cio::cls::are_simple_key_fields<X>(__VA_ARGS__)) { \
CXON_IF_CONSTEXPR (cio::cls::is_trivial_key_class<Type>::value) { \
/*static_assert(cio::cls::are_trivial_key_fields<X>(__VA_ARGS__), "unexpected, use nontrivial key(s)");*/ \
using Y = bind_traits_t<X, cio::key::simple_traits>; \
return json::cls::write_fields<Y>(o, t, fs, cx); \
} \
Expand All @@ -97,15 +99,16 @@ namespace cxon { // cio::val::sink read
CXON_JSON_CLS_READ(Type, __VA_ARGS__) \
CXON_JSON_CLS_WRITE(Type, __VA_ARGS__)

#define CXON_JSON_CLS_SIMPLE_KEY_MEMBER()\
using cxon_simple_key_class_tag = cxon::cio::cls::cxon_simple_key_class_tag;
#define CXON_JSON_CLS_NONTRIVIAL_KEY_MEMBER()\
using cxon_nontrivial_key_class_tag = cxon::cio::cls::cxon_nontrivial_key_class_tag;

#define CXON_JSON_CLS_READ_MEMBER(Type, ...) \
template <typename X, typename II, typename Cx> \
static auto read_value(Type& t, II& i, II e, Cx& cx) -> cxon::enable_for_t<X, cxon::JSON> { \
using CXON_T_ = Type; ((void)((CXON_T_*)0)); \
static CXON_CXX17_CONSTEXPR auto fs = cxon::json::cls::make_fields(__VA_ARGS__); \
CXON_IF_CONSTEXPR (cxon::cio::cls::is_simple_key_class<Type>::value || cxon::cio::cls::are_simple_key_fields<X>(__VA_ARGS__)) { \
CXON_IF_CONSTEXPR (cxon::cio::cls::is_trivial_key_class<Type>::value) { \
/*static_assert(cxon::cio::cls::are_trivial_key_fields<X>(__VA_ARGS__), "unexpected, use nontrivial key(s)");*/ \
using Y = cxon::bind_traits_t<X, cxon::cio::key::simple_traits>; \
/*CXON_IF_CONSTEXPR (cxon::cio::cls::is_ordered_keys_class<Type>::value || cxon::cio::cls::are_fields_ordered(__VA_ARGS__)) { \
using Y = cxon::bind_traits_t<Y, cxon::cio::cls::ordered_keys_traits>; \
Expand All @@ -124,7 +127,8 @@ namespace cxon { // cio::val::sink read
static auto write_value(O& o, const Type& t, Cx& cx) -> cxon::enable_for_t<X, cxon::JSON> { \
using CXON_T_ = Type; ((void)((CXON_T_*)0)); \
static CXON_CXX17_CONSTEXPR auto fs = cxon::json::cls::make_fields(__VA_ARGS__); \
CXON_IF_CONSTEXPR (cxon::cio::cls::is_simple_key_class<Type>::value || cxon::cio::cls::are_simple_key_fields<X>(__VA_ARGS__)) { \
CXON_IF_CONSTEXPR (cxon::cio::cls::is_trivial_key_class<Type>::value) { \
/*static_assert(cxon::cio::cls::are_trivial_key_fields<X>(__VA_ARGS__), "unexpected, use nontrivial key(s)");*/ \
using Y = cxon::bind_traits_t<X, cxon::cio::key::simple_traits>; \
return cxon::json::cls::write_fields<Y>(o, t, fs, cx); \
} \
Expand Down
3 changes: 3 additions & 0 deletions test/src/json/json.core-compound.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,7 @@ namespace {
bool operator ==(const Struct10& t) const { return a == t.a && b == t.b; }
};
}
CXON_JSON_CLS_NONTRIVIAL_KEY(Struct10)
CXON_JSON_CLS_READ(Struct10,
CXON_JSON_CLS_FIELD_ASIS(a),
CXON_JSON_CLS_FIELD_SKIP("skip1"),
Expand Down Expand Up @@ -1446,6 +1447,7 @@ namespace {
struct Struct15 {
std::string a, b, c, d;
bool operator ==(const Struct15& t) const { return a == t.a && b == t.b && c == t.c && d == t.d; }
CXON_JSON_CLS_NONTRIVIAL_KEY_MEMBER()
};
}
CXON_JSON_CLS(Struct15,
Expand Down Expand Up @@ -1561,6 +1563,7 @@ namespace {
Struct18(int a = 0) : a(a) {}
bool operator ==(const Struct18& o) const { return a == o.a; }
CXON_JSON_CLS_MEMBER(Struct18, CXON_JSON_CLS_FIELD_NAME("\t", a))
CXON_JSON_CLS_NONTRIVIAL_KEY_MEMBER()
};
struct Struct19 {
Struct18 a;
Expand Down
3 changes: 0 additions & 3 deletions test/src/json/json.node-time.native-apache_builds.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ namespace test { namespace apache_builds {
std::string color;

CXON_JSON_CLS_MEMBER(job, CXON_JSON_CLS_FIELD_ASIS(name), CXON_JSON_CLS_FIELD_ASIS(url), CXON_JSON_CLS_FIELD_ASIS(color))
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct view {
std::string name;
std::string url;

CXON_JSON_CLS_MEMBER(view, CXON_JSON_CLS_FIELD_ASIS(name), CXON_JSON_CLS_FIELD_ASIS(url))
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct object {
Expand Down Expand Up @@ -65,7 +63,6 @@ namespace test { namespace apache_builds {
CXON_JSON_CLS_FIELD_ASIS(useSecurity),
CXON_JSON_CLS_FIELD_ASIS(views)
)
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

}}
Expand Down
3 changes: 0 additions & 3 deletions test/src/json/json.node-time.native-canada.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace test { namespace canada {
std::vector<std::vector<std::pair<double, double>>> coordinates;

CXON_JSON_CLS_MEMBER(geometry, CXON_JSON_CLS_FIELD_ASIS(type), CXON_JSON_CLS_FIELD_ASIS(coordinates))
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct feature {
Expand All @@ -29,15 +28,13 @@ namespace test { namespace canada {
struct geometry geometry;

CXON_JSON_CLS_MEMBER(feature, CXON_JSON_CLS_FIELD_ASIS(type), CXON_JSON_CLS_FIELD_ASIS(properties), CXON_JSON_CLS_FIELD_ASIS(geometry))
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct object {
std::string type;
std::vector<feature> features;

CXON_JSON_CLS_MEMBER(object, CXON_JSON_CLS_FIELD_ASIS(type), CXON_JSON_CLS_FIELD_ASIS(features))
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

}}
Expand Down
5 changes: 0 additions & 5 deletions test/src/json/json.node-time.native-citm_catalog.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ namespace test { namespace citm_catalog {
CXON_JSON_CLS_FIELD_ASIS(subtitle),
CXON_JSON_CLS_FIELD_ASIS(topicIds)
)
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct price {
Expand All @@ -46,7 +45,6 @@ namespace test { namespace citm_catalog {
unsigned long long seatCategoryId;

CXON_JSON_CLS_MEMBER(price, CXON_JSON_CLS_FIELD_ASIS(amount), CXON_JSON_CLS_FIELD_ASIS(audienceSubCategoryId), CXON_JSON_CLS_FIELD_ASIS(seatCategoryId))
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct area {
Expand All @@ -61,7 +59,6 @@ namespace test { namespace citm_catalog {
unsigned long long seatCategoryId;

CXON_JSON_CLS_MEMBER(seat, CXON_JSON_CLS_FIELD_ASIS(areas), CXON_JSON_CLS_FIELD_ASIS(seatCategoryId))
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct performance {
Expand All @@ -86,7 +83,6 @@ namespace test { namespace citm_catalog {
CXON_JSON_CLS_FIELD_ASIS(start),
CXON_JSON_CLS_FIELD_ASIS(venueCode)
)
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct object {
Expand Down Expand Up @@ -115,7 +111,6 @@ namespace test { namespace citm_catalog {
CXON_JSON_CLS_FIELD_ASIS(topicSubTopics),
CXON_JSON_CLS_FIELD_ASIS(venueNames)
)
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

}}
Expand Down
1 change: 0 additions & 1 deletion test/src/json/json.node-time.native-emoji.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ namespace test { namespace emoji {
CXON_JSON_CLS_FIELD_ASIS_DFLT(ios_version , self.ios_version.empty()),
CXON_JSON_CLS_FIELD_ASIS_DFLT(skin_tones , !self.skin_tones)
)
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};
using object = std::vector<entry>;

Expand Down
19 changes: 0 additions & 19 deletions test/src/json/json.node-time.native-github_events.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ namespace test { namespace github_events {
CXON_JSON_CLS_FIELD_ASIS(url),
CXON_JSON_CLS_FIELD_ASIS(id)
)
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct repo_type {
Expand All @@ -40,15 +39,13 @@ namespace test { namespace github_events {
std::string name;

CXON_JSON_CLS_MEMBER(repo_type, CXON_JSON_CLS_FIELD_ASIS(url), CXON_JSON_CLS_FIELD_ASIS(id), CXON_JSON_CLS_FIELD_ASIS(name))
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct author_type {
std::string email;
std::string name;

CXON_JSON_CLS_MEMBER(author_type, CXON_JSON_CLS_FIELD_ASIS(email), CXON_JSON_CLS_FIELD_ASIS(name))
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct commit_type {
Expand All @@ -65,7 +62,6 @@ namespace test { namespace github_events {
CXON_JSON_CLS_FIELD_ASIS(sha),
CXON_JSON_CLS_FIELD_ASIS(author)
)
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct user_type {
Expand Down Expand Up @@ -111,7 +107,6 @@ namespace test { namespace github_events {
CXON_JSON_CLS_FIELD_ASIS(followers_url),
CXON_JSON_CLS_FIELD_ASIS(following_url)
)
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct forkee_type {
Expand Down Expand Up @@ -246,7 +241,6 @@ namespace test { namespace github_events {
CXON_JSON_CLS_FIELD_ASIS(size),
CXON_JSON_CLS_FIELD_ASIS(watchers)
)
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct pull_request_type {
Expand All @@ -255,7 +249,6 @@ namespace test { namespace github_events {
std::optional<std::string> diff_url;

CXON_JSON_CLS_MEMBER(pull_request_type, CXON_JSON_CLS_FIELD_ASIS(html_url), CXON_JSON_CLS_FIELD_ASIS(patch_url), CXON_JSON_CLS_FIELD_ASIS(diff_url))
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct issue_type {
Expand Down Expand Up @@ -300,7 +293,6 @@ namespace test { namespace github_events {
CXON_JSON_CLS_FIELD_ASIS(comments_url),
CXON_JSON_CLS_FIELD_ASIS(comments)
)
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct comment_type {
Expand Down Expand Up @@ -328,7 +320,6 @@ namespace test { namespace github_events {
CXON_JSON_CLS_FIELD_ASIS(updated_at),
CXON_JSON_CLS_FIELD_ASIS(id)
)
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct page_type {
Expand All @@ -347,7 +338,6 @@ namespace test { namespace github_events {
CXON_JSON_CLS_FIELD_ASIS(summary),
CXON_JSON_CLS_FIELD_ASIS(action)
)
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct payload {
Expand Down Expand Up @@ -394,7 +384,6 @@ namespace test { namespace github_events {
CXON_JSON_CLS_FIELD_ASIS(head),
CXON_JSON_CLS_FIELD_ASIS(size)
)
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct payload_create_event : payload {
Expand All @@ -414,7 +403,6 @@ namespace test { namespace github_events {
CXON_JSON_CLS_FIELD_ASIS(ref),
CXON_JSON_CLS_FIELD_ASIS(ref_type)
)
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct payload_fork_event : payload {
Expand All @@ -423,7 +411,6 @@ namespace test { namespace github_events {
payload_fork_event(forkee_type&& forkee) : payload(payload::fork_event), forkee(std::move(forkee)) {}

CXON_JSON_CLS_WRITE_MEMBER(payload_fork_event, CXON_JSON_CLS_FIELD_ASIS(forkee))
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct payload_watch_event : payload {
Expand All @@ -432,7 +419,6 @@ namespace test { namespace github_events {
payload_watch_event(std::string&& action) : payload(payload::watch_event), action(std::move(action)) {}

CXON_JSON_CLS_WRITE_MEMBER(payload_watch_event, CXON_JSON_CLS_FIELD_ASIS(action))
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct payload_issue_comment_event : payload {
Expand All @@ -450,7 +436,6 @@ namespace test { namespace github_events {
CXON_JSON_CLS_FIELD_ASIS(issue),
CXON_JSON_CLS_FIELD_ASIS_DFLT(comment, self.comment == comment_type {})
)
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct payload_gollum_event : payload {
Expand All @@ -459,7 +444,6 @@ namespace test { namespace github_events {
payload_gollum_event(std::vector<page_type>&& pages) : payload(payload::gollum_event), pages(std::move(pages)) {}

CXON_JSON_CLS_WRITE_MEMBER(payload_gollum_event, CXON_JSON_CLS_FIELD_ASIS(pages))
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct payload_proxy {
Expand Down Expand Up @@ -496,7 +480,6 @@ namespace test { namespace github_events {
CXON_JSON_CLS_FIELD_ASIS_DFLT(comment, self.comment == comment_type {}),
CXON_JSON_CLS_FIELD_ASIS(pages)
)
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct org_type {
Expand All @@ -513,7 +496,6 @@ namespace test { namespace github_events {
CXON_JSON_CLS_FIELD_ASIS(url),
CXON_JSON_CLS_FIELD_ASIS(id)
)
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

struct event_type {
Expand All @@ -536,7 +518,6 @@ namespace test { namespace github_events {
CXON_JSON_CLS_FIELD_ASIS(payload),
CXON_JSON_CLS_FIELD_ASIS(id)
)
CXON_JSON_CLS_SIMPLE_KEY_MEMBER()
};

using object = std::vector<event_type>;
Expand Down
Loading

0 comments on commit a00326c

Please sign in to comment.