reference_handler.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #pragma once
  2. #include <functional>
  3. #include <map>
  4. #include <set>
  5. #include <unordered_map>
  6. #include <jvalidate/detail/anchor.h>
  7. #include <jvalidate/detail/pointer.h>
  8. #include <jvalidate/detail/reference.h>
  9. #include <jvalidate/enum.h>
  10. #include <jvalidate/forward.h>
  11. #include <jvalidate/uri.h>
  12. namespace jvalidate {
  13. template <Adapter A> class ReferenceManager {
  14. public:
  15. using Keywords = std::unordered_map<std::string_view, std::set<schema::Wraps>>;
  16. private:
  17. std::map<detail::RootReference, A> roots_;
  18. std::map<detail::Reference, detail::RootReference, std::greater<>> absolute_to_canonical_;
  19. std::map<detail::RootReference, detail::Reference, std::greater<>> canonical_to_absolute_;
  20. public:
  21. ReferenceManager(A const & root, schema::Version version, Keywords const & keywords)
  22. : roots_{{{}, root}} {
  23. prime_impl(root, {}, version, keywords);
  24. }
  25. std::optional<A> root(detail::RootReference const & root) const {
  26. if (auto it = roots_.find(root); it != roots_.end()) {
  27. return it->second;
  28. }
  29. return std::nullopt;
  30. }
  31. detail::Reference canonicalize(detail::Reference const & ref, bool allow_equality = false) const {
  32. if (canonical_to_absolute_.contains(ref.root())) {
  33. return ref;
  34. }
  35. auto it = allow_equality ? absolute_to_canonical_.lower_bound(ref)
  36. : absolute_to_canonical_.upper_bound(ref);
  37. if (it == absolute_to_canonical_.end()) {
  38. return ref;
  39. }
  40. auto const & [absolute, anchor] = *it;
  41. if (not ref.pointer().starts_with(absolute.pointer())) {
  42. return ref;
  43. }
  44. return detail::Reference(anchor, ref.pointer().relative_to(absolute.pointer()));
  45. }
  46. detail::Reference canonicalize(detail::Reference const & ref,
  47. detail::Reference const & parent) const {
  48. // Relative URI, not in the HEREDOC (or we set an $id)
  49. if (ref.uri().empty() and ref.anchor().empty()) {
  50. return detail::Reference(canonicalize(parent, true).root(), ref.pointer());
  51. }
  52. // TODO(samjaffe): Clean this clause up
  53. URI uri = ref.uri().empty() ? parent.root().uri() : ref.uri();
  54. if (uri.empty()) {
  55. auto parent_uri = canonicalize(parent).uri();
  56. if (parent_uri == parent.uri() && parent.uri().empty()) {
  57. auto it = absolute_to_canonical_.find(detail::Reference());
  58. if (it != absolute_to_canonical_.end()) {
  59. uri = it->second.uri();
  60. }
  61. }
  62. } else if (uri.is_rootless()) {
  63. auto parent_uri = canonicalize(parent).uri();
  64. if (parent_uri == parent.uri() && parent.uri().empty()) {
  65. auto it = absolute_to_canonical_.find(detail::Reference());
  66. if (it != absolute_to_canonical_.end()) {
  67. parent_uri = it->second.uri();
  68. }
  69. }
  70. EXPECT_M(parent_uri.resource().rfind('/') != std::string::npos,
  71. "Unable to deduce root for relative uri " << uri << " (" << parent_uri << ")");
  72. uri = (uri.is_relative() ? parent_uri.parent() : parent_uri.root()) / uri;
  73. }
  74. detail::Reference rval(uri, ref.anchor(), ref.pointer());
  75. // Will now need to go make an external fetch...
  76. // TODO(samjaffe): Make that process internal?
  77. return rval;
  78. }
  79. void prime(Adapter auto const & json, URI const & where, schema::Version version,
  80. Keywords const & keywords) {
  81. canonical_to_absolute_.emplace(where, detail::Reference(where));
  82. prime_impl(json, detail::Reference(where), version, keywords);
  83. }
  84. private:
  85. void prime_impl(Adapter auto const & json, detail::Reference where, schema::Version version,
  86. Keywords const & keywords) {
  87. if (json.type() != adapter::Type::Object) {
  88. return;
  89. }
  90. canonicalize(where, version, json);
  91. for (auto const & [key, value] : json.as_object()) {
  92. auto vit = keywords.find(key);
  93. if (vit == keywords.end()) {
  94. continue;
  95. }
  96. if (vit->second.contains(schema::Wraps::Array) && value.type() == adapter::Type::Array) {
  97. size_t index = 0;
  98. for (auto const & elem : value.as_array()) {
  99. prime_impl(elem, where / key / index, version, keywords);
  100. ++index;
  101. }
  102. } else if (vit->second.contains(schema::Wraps::Object) &&
  103. value.type() == adapter::Type::Object) {
  104. for (auto const & [prop, elem] : value.as_object()) {
  105. prime_impl(elem, where / key / prop, version, keywords);
  106. }
  107. } else if (vit->second.contains(schema::Wraps::Schema)) {
  108. prime_impl(value, where / key, version, keywords);
  109. }
  110. }
  111. }
  112. void canonicalize(detail::Reference & where, schema::Version version, A const & json) {
  113. std::string const id = version <= schema::Version::Draft04 ? "id" : "$id";
  114. auto const schema = json.as_object();
  115. detail::RootReference root = where.root();
  116. if (schema.contains(id)) {
  117. root = detail::RootReference(schema[id].as_string());
  118. if (root.uri().empty()) {
  119. root = detail::RootReference(where.uri(), root.anchor());
  120. } else if (not root.uri().is_rootless() || where.uri().empty()) {
  121. // By definition - rootless URIs cannot be relative
  122. } else if (root.uri().is_relative()) {
  123. root = detail::RootReference(where.uri().parent() / root.uri(), root.anchor());
  124. } else {
  125. root = detail::RootReference(where.uri().root() / root.uri(), root.anchor());
  126. }
  127. cache(root, where, json);
  128. }
  129. if (not schema.contains("$anchor") || version < schema::Version::Draft2019_09) {
  130. return;
  131. }
  132. root = detail::RootReference(root.uri(), detail::Anchor(schema["$anchor"].as_string()));
  133. cache(root, where, json);
  134. }
  135. template <size_t I>
  136. bool extract_relative(detail::Reference const & where, auto const & tup,
  137. detail::Pointer & out) const {
  138. auto const & ptr = std::get<I>(tup).pointer();
  139. if (where.pointer().starts_with(ptr)) {
  140. out = where.pointer().relative_to(ptr);
  141. return true;
  142. }
  143. return false;
  144. }
  145. void recursive_cache(detail::RootReference const & root, detail::Reference const & where) {
  146. if (where.pointer().empty()) {
  147. return;
  148. }
  149. detail::Pointer relative;
  150. if (auto it = canonical_to_absolute_.lower_bound(where.root());
  151. it != canonical_to_absolute_.end() && where.root() != it->second.root() &&
  152. extract_relative<1>(where, *it, relative)) {
  153. absolute_to_canonical_.emplace(it->second / relative, root);
  154. recursive_cache(root, it->second / relative);
  155. }
  156. }
  157. void cache(detail::RootReference const & root, detail::Reference & where,
  158. Adapter auto const & json) {
  159. recursive_cache(root, where);
  160. absolute_to_canonical_.emplace(where, root);
  161. canonical_to_absolute_.emplace(root, where);
  162. roots_.emplace(root, json);
  163. where = detail::Reference(root);
  164. }
  165. };
  166. }