reference_handler.h 6.7 KB

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