reference_handler.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. return (uri.is_relative() ? base.parent() : base.root()) / uri;
  59. }();
  60. detail::Reference rval(uri, ref.anchor(), ref.pointer());
  61. // Will now need to go make an external fetch...
  62. // TODO(samjaffe): Make that process internal?
  63. return rval;
  64. }
  65. void prime(Adapter auto const & json, URI const & where, schema::Version version,
  66. Keywords const & keywords) {
  67. references_.emplace(where);
  68. prime_impl(json, detail::Reference(where), version, keywords);
  69. }
  70. private:
  71. void prime_impl(Adapter auto const & json, detail::Reference where, schema::Version version,
  72. Keywords const & keywords) {
  73. if (json.type() != adapter::Type::Object) {
  74. return;
  75. }
  76. canonicalize(where, version, json);
  77. for (auto const & [key, value] : json.as_object()) {
  78. auto vit = keywords.find(key);
  79. if (vit == keywords.end()) {
  80. continue;
  81. }
  82. if (vit->second.contains(schema::Wraps::Array) && value.type() == adapter::Type::Array) {
  83. size_t index = 0;
  84. for (auto const & elem : value.as_array()) {
  85. prime_impl(elem, where / key / index, version, keywords);
  86. ++index;
  87. }
  88. } else if (vit->second.contains(schema::Wraps::Object) &&
  89. value.type() == adapter::Type::Object) {
  90. for (auto const & [prop, elem] : value.as_object()) {
  91. prime_impl(elem, where / key / prop, version, keywords);
  92. }
  93. } else if (vit->second.contains(schema::Wraps::Schema)) {
  94. prime_impl(value, where / key, version, keywords);
  95. }
  96. }
  97. }
  98. void canonicalize(detail::Reference & where, schema::Version version, A const & json) {
  99. std::string const id = version <= schema::Version::Draft04 ? "id" : "$id";
  100. auto const schema = json.as_object();
  101. detail::RootReference root = where.root();
  102. if (schema.contains(id)) {
  103. root = detail::RootReference(schema[id].as_string());
  104. if (root.uri().empty()) {
  105. root = detail::RootReference(where.uri(), root.anchor());
  106. } else if (not root.uri().is_rootless() || where.uri().empty()) {
  107. // By definition - rootless URIs cannot be relative
  108. } else if (root.uri().is_relative()) {
  109. root = detail::RootReference(where.uri().parent() / root.uri(), root.anchor());
  110. } else {
  111. root = detail::RootReference(where.uri().root() / root.uri(), root.anchor());
  112. }
  113. roots_.emplace(root, json);
  114. where = references_.emplace(where, root);
  115. }
  116. // $anchor and its related keywords were introduced in Draft 2019-09
  117. if (version < schema::Version::Draft2019_09) {
  118. return;
  119. }
  120. if (schema.contains("$anchor")) {
  121. root = detail::RootReference(root.uri(), detail::Anchor(schema["$anchor"].as_string()));
  122. roots_.emplace(root, json);
  123. where = references_.emplace(where, root);
  124. }
  125. if (version == schema::Version::Draft2019_09 && schema.contains("$recursiveAnchor") &&
  126. schema["$recursiveAnchor"].as_boolean()) {
  127. detail::Anchor anchor;
  128. root = detail::RootReference(root.uri(), anchor);
  129. roots_.emplace(root, json);
  130. where = references_.emplace(where, root);
  131. dynamic_anchors_.emplace(root.uri(), anchor);
  132. }
  133. if (schema.contains("$dynamicAnchor") && version > schema::Version::Draft2019_09) {
  134. detail::Anchor anchor(schema["$dynamicAnchor"].as_string());
  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. }
  141. };
  142. }