reference_handler.h 6.3 KB

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