| 12345678910111213141516171819202122232425262728293031323334353637 |
- #pragma once
- #include <string>
- #include <string_view>
- #include <jvalidate/detail/expect.h>
- #include <jvalidate/detail/pointer.h>
- #include <jvalidate/forward.h>
- namespace jvalidate::detail {
- class RelativePointer {
- public:
- RelativePointer(std::string_view path) {
- if (auto pos = path.find('/'); pos != path.npos) {
- pointer_ = Pointer(path.substr(pos));
- path.remove_suffix(path.size() - pos);
- } else {
- EXPECT_M(not path.empty() && path.back() == '#',
- "RelativePointer must end in a pointer, or a '#'")
- path.remove_suffix(1);
- }
- parent_steps_ = std::stoull(std::string(path));
- }
- template <Adapter A>
- std::variant<std::string, A> inspect(Pointer const & where, A const & root) const {
- if (pointer_) {
- return pointer_->walk(where.parent(parent_steps_).walk(root));
- }
- return where.parent(parent_steps_).back();
- }
- private:
- size_t parent_steps_;
- std::optional<Pointer> pointer_;
- };
- }
|