reference_handler.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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, Keywords const & keywords) : roots_{{{}, root}} {
  22. prime_impl(root, {}, keywords);
  23. }
  24. std::optional<A> root(detail::RootReference const & root) const {
  25. if (auto it = roots_.find(root); it != roots_.end()) {
  26. return it->second;
  27. }
  28. return std::nullopt;
  29. }
  30. detail::Reference canonicalize(detail::Reference const & ref) const {
  31. if (canonical_to_absolute_.contains(ref.root())) {
  32. return ref;
  33. }
  34. auto it = absolute_to_canonical_.upper_bound(ref);
  35. if (it == absolute_to_canonical_.end()) {
  36. return ref;
  37. }
  38. auto const & [absolute, anchor] = *it;
  39. if (not ref.pointer().starts_with(absolute.pointer())) {
  40. return ref;
  41. }
  42. return detail::Reference(anchor, ref.pointer().relative_to(absolute.pointer()));
  43. }
  44. detail::Reference canonicalize(detail::Reference const & ref,
  45. detail::Reference const & parent) const {
  46. // Relative URI, not in the HEREDOC (or we set an $id)
  47. if (ref.uri().empty() and ref.anchor().empty()) {
  48. return detail::Reference(canonicalize(parent).root(), ref.pointer());
  49. }
  50. // TODO(samjaffe): Clean this clause up
  51. URI uri = ref.uri().empty() ? parent.root().uri() : ref.uri();
  52. if (uri.is_rootless() && not uri.empty()) {
  53. auto parent_uri = canonicalize(parent).uri();
  54. if (parent_uri == parent.uri() && parent.uri().empty()) {
  55. auto it = absolute_to_canonical_.find(detail::Reference());
  56. if (it != absolute_to_canonical_.end()) {
  57. parent_uri = it->second.uri();
  58. }
  59. }
  60. EXPECT_M(parent_uri.resource().rfind('/') != std::string::npos,
  61. "Unable to deduce root for relative uri " << uri << " (" << parent_uri << ")");
  62. uri = (uri.is_relative() ? parent_uri.parent() : parent_uri.root()) / uri;
  63. }
  64. detail::Reference rval(detail::RootReference(uri, ref.anchor()), ref.pointer());
  65. // Will now need to go make an external fetch...
  66. // TODO(samjaffe): Make that process internal?
  67. return rval;
  68. }
  69. void prime(Adapter auto const & json, detail::RootReference const & where,
  70. Keywords const & keywords) {
  71. canonical_to_absolute_.emplace(where, detail::Reference(where));
  72. prime_impl(json, detail::Reference(where), keywords);
  73. }
  74. private:
  75. void prime_impl(Adapter auto const & json, detail::Reference where, Keywords const & keywords) {
  76. if (json.type() != adapter::Type::Object) {
  77. return;
  78. }
  79. canonicalize(where, json);
  80. for (auto const & [key, value] : json.as_object()) {
  81. auto vit = keywords.find(key);
  82. if (vit == keywords.end()) {
  83. continue;
  84. }
  85. if (vit->second.contains(schema::Wraps::Array) && value.type() == adapter::Type::Array) {
  86. size_t index = 0;
  87. for (auto const & elem : value.as_array()) {
  88. prime_impl(elem, where / key / index, keywords);
  89. ++index;
  90. }
  91. } else if (vit->second.contains(schema::Wraps::Object) &&
  92. value.type() == adapter::Type::Object) {
  93. for (auto const & [prop, elem] : value.as_object()) {
  94. prime_impl(elem, where / key / prop, keywords);
  95. }
  96. } else if (vit->second.contains(schema::Wraps::Schema)) {
  97. prime_impl(value, where / key, keywords);
  98. }
  99. }
  100. }
  101. void canonicalize(detail::Reference & where, A const & json) {
  102. auto const schema = json.as_object();
  103. detail::RootReference root = where.root();
  104. if (schema.contains("$id")) {
  105. root = detail::RootReference(schema["$id"].as_string());
  106. if (root.uri().empty()) {
  107. root = detail::RootReference(where.uri(), root.anchor());
  108. } else if (root.uri().is_rootless() && not where.uri().empty()) {
  109. // TODO(samjaffe): Should there also be something for is_relative?
  110. root = detail::RootReference(where.uri().parent() / root.uri(), root.anchor());
  111. }
  112. cache(root, where, json);
  113. }
  114. if (not schema.contains("$anchor")) {
  115. return;
  116. }
  117. EXPECT_M(root.anchor().empty(), "Cannot have $id with anchor and $anchor tags at same time");
  118. root = detail::RootReference(root.uri(), detail::Anchor(schema["$anchor"].as_string()));
  119. cache(root, where, json);
  120. }
  121. template <size_t I>
  122. bool extract_relative(detail::Reference const & where, auto const & tup,
  123. detail::Pointer & out) const {
  124. auto const & ptr = std::get<I>(tup).pointer();
  125. if (where.pointer().starts_with(ptr)) {
  126. out = where.pointer().relative_to(ptr);
  127. return true;
  128. }
  129. return false;
  130. }
  131. void recursive_cache(detail::RootReference const & root, detail::Reference const & where) {
  132. if (where.pointer().empty()) {
  133. return;
  134. }
  135. detail::Pointer relative;
  136. if (auto it = canonical_to_absolute_.lower_bound(where.root());
  137. it != canonical_to_absolute_.end() && where.root() != it->second.root() &&
  138. extract_relative<1>(where, *it, relative)) {
  139. absolute_to_canonical_.emplace(it->second / relative, root);
  140. recursive_cache(root, it->second / relative);
  141. }
  142. }
  143. void cache(detail::RootReference const & root, detail::Reference & where,
  144. Adapter auto const & json) {
  145. recursive_cache(root, where);
  146. absolute_to_canonical_.emplace(where, root);
  147. canonical_to_absolute_.emplace(root, where);
  148. roots_.emplace(root, json);
  149. where = detail::Reference(root);
  150. }
  151. };
  152. }