From 3fae228c1a97852d358a743fa45f4e1e16c49920 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sat, 12 Jan 2019 16:32:17 -0800 Subject: [PATCH] ircd::json: Simplify json::object interface: eliminate path traversing overloads. --- include/ircd/json/json.h | 1 - include/ircd/json/object.h | 101 +------------------------------------ include/ircd/json/path.h | 35 ------------- ircd/m/v1.cc | 7 ++- modules/s_keys.cc | 12 ++++- 5 files changed, 18 insertions(+), 138 deletions(-) delete mode 100644 include/ircd/json/path.h diff --git a/include/ircd/json/json.h b/include/ircd/json/json.h index 7fbc30b9f..655fbd308 100644 --- a/include/ircd/json/json.h +++ b/include/ircd/json/json.h @@ -50,7 +50,6 @@ namespace ircd::json } #include "util.h" -#include "path.h" #include "array.h" #include "object.h" #include "vector.h" diff --git a/include/ircd/json/object.h b/include/ircd/json/object.h index 32778be04..e183c6019 100644 --- a/include/ircd/json/object.h +++ b/include/ircd/json/object.h @@ -52,9 +52,7 @@ namespace ircd::json /// Recursive traversal cannot be achieved via a single key string value; so /// any string_view argument for a key will not be recursive. In other words, /// due to the fact that a JS identifier can have almost any character we have -/// to use a different *type* like a vector of strings; in our common case we -/// use an initializer_list typedef'ed as `path` and those overloads will be -/// recursive. +/// to use a different *type* like a vector of strings. /// struct ircd::json::object :string_view @@ -85,21 +83,16 @@ struct ircd::json::object size_t count() const; size_t size() const; // warns if used; use count() bool has(const string_view &key) const; - bool has(const path &) const; // returns value or default template T get(const string_view &key, const T &def = T{}) const; - template T get(const path &, const T &def = T{}) const; string_view get(const string_view &key, const string_view &def = {}) const; - string_view get(const path &, const string_view &def = {}) const; // returns value or throws not_found template T at(const string_view &key) const; - template T at(const path &) const; // returns value or empty string_view operator[](const string_view &key) const; - string_view operator[](const path &) const; // constructor. Note that you are able to construct from invalid JSON. The // parser is not invoked until other operations and that's when it errors. @@ -178,13 +171,6 @@ struct ircd::json::object::const_iterator friend bool operator>(const const_iterator &, const const_iterator &); }; -inline ircd::string_view -ircd::json::object::operator[](const path &path) -const -{ - return get(path); -} - inline ircd::string_view ircd::json::object::operator[](const string_view &key) const @@ -193,38 +179,6 @@ const return it != end()? it->second : string_view{}; } -template -T -ircd::json::object::at(const path &path) -const try -{ - object object(*this); - const auto it(std::find_if(std::begin(path), std::end(path), [&object] - (const string_view &key) - { - const auto it(object.find(key)); - if(it == std::end(object)) - throw not_found - { - "'%s'", key - }; - - object = it->second; - return false; - })); - - return lex_cast(object); -} -catch(const bad_lex_cast &e) -{ - throw type_error - { - "'%s' must cast to type %s", - ircd::string(path), - typeid(T).name() - }; -} - template T @@ -274,14 +228,6 @@ catch(const bad_lex_cast &e) }; } -inline ircd::string_view -ircd::json::object::get(const path &path, - const string_view &def) -const -{ - return get(path, def); -} - inline ircd::string_view ircd::json::object::get(const string_view &key, const string_view &def) @@ -290,31 +236,6 @@ const return get(key, def); } -template -T -ircd::json::object::get(const path &path, - const T &def) -const try -{ - object object(*this); - const auto it(std::find_if(std::begin(path), std::end(path), [&object] - (const string_view &key) - { - const auto it(object.find(key)); - if(it == std::end(object)) - return true; - - object = it->second; - return false; - })); - - return it == std::end(path)? lex_cast(object) : def; -} -catch(const bad_lex_cast &e) -{ - return def; -} - template ircd::string_view @@ -389,26 +310,6 @@ const return sv.size() <= 2; } -inline bool -ircd::json::object::has(const path &path) -const -{ - object object(*this); - const auto it(std::find_if(std::begin(path), std::end(path), [&object] - (const string_view &key) - { - const auto val(object[key]); - if(val.empty()) - return true; - - object = val; - return false; - })); - - // && path.size() ensures false for empty path. - return it == std::end(path) && path.size(); -} - inline bool ircd::json::object::has(const string_view &key) const diff --git a/include/ircd/json/path.h b/include/ircd/json/path.h deleted file mode 100644 index a57265e69..000000000 --- a/include/ircd/json/path.h +++ /dev/null @@ -1,35 +0,0 @@ -// Matrix Construct -// -// Copyright (C) Matrix Construct Developers, Authors & Contributors -// Copyright (C) 2016-2018 Jason Volk -// -// 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. - -#pragma once -#define HAVE_IRCD_JSON_PATH_H - -namespace ircd::json -{ - /// Higher order type beyond a string to cleanly delimit multiple keys. - using path = std::initializer_list; - std::ostream &operator<<(std::ostream &, const path &); -} - -inline std::ostream & -ircd::json::operator<<(std::ostream &s, const path &p) -{ - auto it(std::begin(p)); - if(it != std::end(p)) - { - s << *it; - ++it; - } - - for(; it != std::end(p); ++it) - s << '.' << *it; - - return s; -} diff --git a/ircd/m/v1.cc b/ircd/m/v1.cc index 5c3938493..63b338bc5 100644 --- a/ircd/m/v1.cc +++ b/ircd/m/v1.cc @@ -1272,9 +1272,14 @@ ircd::m::v1::fetch_head(const id::room &room_id, request.in.content }; + const json::object event + { + proto.at("event") + }; + const json::array prev_events { - proto.at({"event", "prev_events"}) + event.at("prev_events") }; const json::array prev_event diff --git a/modules/s_keys.cc b/modules/s_keys.cc index 2cb9489f4..e54c51622 100644 --- a/modules/s_keys.cc +++ b/modules/s_keys.cc @@ -123,9 +123,19 @@ init_my_tls_crt() const json::object config{}; if(!fs::exists(cert_file)) { + const json::object &certificate + { + config.get("certificate") + }; + + const json::object &self_ + { + certificate.get(m::self::origin) + }; + std::string subject { - config.get({"certificate", m::self::origin, "subject"}) + self_.get("subject") }; if(!subject)