reference_handler.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/detail/reference_cache.h>
  10. #include <jvalidate/enum.h>
  11. #include <jvalidate/forward.h>
  12. #include <jvalidate/uri.h>
  13. namespace jvalidate {
  14. template <Adapter A> class ReferenceManager {
  15. public:
  16. using Keywords = std::unordered_map<std::string_view, std::set<schema::Wraps>>;
  17. private:
  18. std::map<detail::RootReference, A> roots_;
  19. detail::ReferenceCache references_;
  20. std::multimap<URI, detail::Anchor> dynamic_anchors_;
  21. public:
  22. ReferenceManager(A const & root, schema::Version version, Keywords const & keywords)
  23. : roots_{{{}, root}} {
  24. prime_impl(root, {}, version, keywords);
  25. }
  26. bool has_dynamic_anchor(detail::RootReference const & root) const {
  27. for (auto [it, end] = dynamic_anchors_.equal_range(root.uri()); it != end; ++it) {
  28. if (it->second == root.anchor()) {
  29. return true;
  30. }
  31. }
  32. return false;
  33. }
  34. std::optional<A> root(detail::RootReference const & root) const {
  35. if (auto it = roots_.find(root); it != roots_.end()) {
  36. return it->second;
  37. }
  38. return std::nullopt;
  39. }
  40. detail::Reference canonicalize(detail::Reference const & ref,
  41. detail::Reference const & parent) const {
  42. // Relative URI, not in the HEREDOC (or we set an $id)
  43. if (ref.uri().empty() and ref.anchor().empty()) {
  44. return detail::Reference(references_.relative_to_nearest_anchor(parent).root(),
  45. ref.pointer());
  46. }
  47. URI uri = [this, &ref, &parent]() {
  48. if (ref.uri().empty() && parent.uri().empty()) {
  49. return references_.actual_parent_uri(parent);
  50. }
  51. URI uri = ref.uri().empty() ? parent.uri() : ref.uri();
  52. if (not uri.is_rootless()) {
  53. return uri;
  54. }
  55. URI base = references_.actual_parent_uri(parent);
  56. EXPECT_M(base.resource().rfind('/') != std::string::npos,
  57. "Unable to deduce root for relative uri " << uri << " (" << base << ")");
  58. if (not uri.is_relative()) {
  59. return base.root() / uri;
  60. }
  61. if (auto br = base.resource(), ur = uri.resource();
  62. br.ends_with(ur) && br[br.size() - ur.size() - 1] == '/') {
  63. return base;
  64. }
  65. return base.parent() / uri;
  66. }();
  67. detail::Reference rval(uri, ref.anchor(), ref.pointer());
  68. // Will now need to go make an external fetch...
  69. // TODO(samjaffe): Make that process internal?
  70. return rval;
  71. }
  72. void prime(Adapter auto const & json, URI const & where, schema::Version version,
  73. Keywords const & keywords) {
  74. references_.emplace(where);
  75. prime_impl(json, detail::Reference(where), version, keywords);
  76. }
  77. private:
  78. void prime_impl(Adapter auto const & json, detail::Reference where, schema::Version version,
  79. Keywords const & keywords) {
  80. if (json.type() != adapter::Type::Object) {
  81. return;
  82. }
  83. canonicalize(where, version, json);
  84. for (auto const & [key, value] : json.as_object()) {
  85. auto vit = keywords.find(key);
  86. if (vit == keywords.end()) {
  87. continue;
  88. }
  89. if (vit->second.contains(schema::Wraps::Array) && value.type() == adapter::Type::Array) {
  90. size_t index = 0;
  91. for (auto const & elem : value.as_array()) {
  92. prime_impl(elem, where / key / index, version, keywords);
  93. ++index;
  94. }
  95. } else if (vit->second.contains(schema::Wraps::Object) &&
  96. value.type() == adapter::Type::Object) {
  97. for (auto const & [prop, elem] : value.as_object()) {
  98. prime_impl(elem, where / key / prop, version, keywords);
  99. }
  100. } else if (vit->second.contains(schema::Wraps::Schema)) {
  101. prime_impl(value, where / key, version, keywords);
  102. }
  103. }
  104. }
  105. void canonicalize(detail::Reference & where, schema::Version version, A const & json) {
  106. std::string const id = version <= schema::Version::Draft04 ? "id" : "$id";
  107. auto const schema = json.as_object();
  108. detail::RootReference root = where.root();
  109. if (schema.contains(id)) {
  110. root = detail::RootReference(schema[id].as_string());
  111. if (root.uri().empty()) {
  112. root = detail::RootReference(where.uri(), root.anchor());
  113. } else if (not root.uri().is_rootless() || where.uri().empty()) {
  114. // By definition - rootless URIs cannot be relative
  115. } else if (root.uri().is_relative()) {
  116. root = detail::RootReference(where.uri().parent() / root.uri(), root.anchor());
  117. } else {
  118. root = detail::RootReference(where.uri().root() / root.uri(), root.anchor());
  119. }
  120. roots_.emplace(root, json);
  121. where = references_.emplace(where, root);
  122. }
  123. // $anchor and its related keywords were introduced in Draft 2019-09
  124. if (version < schema::Version::Draft2019_09) {
  125. return;
  126. }
  127. if (schema.contains("$anchor")) {
  128. root = detail::RootReference(root.uri(), detail::Anchor(schema["$anchor"].as_string()));
  129. roots_.emplace(root, json);
  130. where = references_.emplace(where, root);
  131. }
  132. if (version == schema::Version::Draft2019_09 && schema.contains("$recursiveAnchor") &&
  133. schema["$recursiveAnchor"].as_boolean()) {
  134. detail::Anchor anchor;
  135. root = detail::RootReference(root.uri(), anchor);
  136. roots_.emplace(root, json);
  137. where = references_.emplace(where, root);
  138. dynamic_anchors_.emplace(root.uri(), anchor);
  139. }
  140. if (schema.contains("$dynamicAnchor") && version > schema::Version::Draft2019_09) {
  141. detail::Anchor anchor(schema["$dynamicAnchor"].as_string());
  142. root = detail::RootReference(root.uri(), anchor);
  143. roots_.emplace(root, json);
  144. where = references_.emplace(where, root);
  145. dynamic_anchors_.emplace(root.uri(), anchor);
  146. }
  147. }
  148. };
  149. }