reference_handler.h 5.3 KB

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