relative_pointer.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #pragma once
  2. #include <ostream>
  3. #include <string>
  4. #include <string_view>
  5. #include <jvalidate/detail/expect.h>
  6. #include <jvalidate/detail/pointer.h>
  7. #include <jvalidate/forward.h>
  8. namespace jvalidate::detail {
  9. class RelativePointer {
  10. public:
  11. RelativePointer(std::string_view path) {
  12. if (path == "0") {
  13. return;
  14. }
  15. if (auto pos = path.find('/'); pos != path.npos) {
  16. pointer_ = Pointer(path.substr(pos));
  17. path.remove_suffix(path.size() - pos);
  18. } else {
  19. EXPECT_M(not path.empty() && path.back() == '#',
  20. "RelativePointer must end in a pointer, or a '#'");
  21. requests_key_ = true;
  22. path.remove_suffix(1);
  23. }
  24. parent_steps_ = std::stoull(std::string(path));
  25. }
  26. template <Adapter A>
  27. std::variant<std::string, A> inspect(Pointer const & where, A const & root) const {
  28. if (requests_key_) {
  29. return where.parent(parent_steps_).back();
  30. }
  31. auto rval = where.parent(parent_steps_).walk(root);
  32. return pointer_.walk(rval);
  33. }
  34. friend std::ostream & operator<<(std::ostream & os, RelativePointer const & rel) {
  35. os << rel.parent_steps_;
  36. if (rel.requests_key_) {
  37. return os << '#';
  38. }
  39. os << rel.pointer_;
  40. return os;
  41. }
  42. private:
  43. size_t parent_steps_ = 0;
  44. bool requests_key_ = false;
  45. Pointer pointer_ = {};
  46. };
  47. }