Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarJsonPatch.cpp
blob: 88f9377ebd5f187d2d55dc48801f332f182e3a66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "StarJsonPatch.hpp"
#include "StarJsonPath.hpp"
#include "StarLexicalCast.hpp"

namespace Star {

Json jsonPatch(Json const& base, JsonArray const& patch) {
  auto res = base;
  try {
    for (auto const& operation : patch) {
      res = JsonPatching::applyOperation(res, operation);
    }
    return res;
  } catch (JsonException const& e) {
    throw JsonPatchException(strf("Could not apply patch to base. {}", e.what()), false);
  }
}


// Returns 0 if not found, index + 1 if found.
size_t findJsonMatch(Json const& searchable, Json const& value, JsonPath::Pointer& pointer) {
  if (searchable.isType(Json::Type::Array)) {
    auto array = searchable.toArray();
    for (size_t i = 0; i != array.size(); ++i) {
      if (jsonPartialMatch(array[i], value))
        return i + 1;
    }
  } else {
    throw JsonPatchException(strf("Search operation failure, value at '{}' is not an array.", pointer.path()), false);
  }
  return 0;
}


namespace JsonPatching {

  static const StringMap<std::function<Json(Json, Json)>> functionMap = StringMap<std::function<Json(Json, Json)>>{
      {"test", std::bind(applyTestOperation, _1, _2)},
      {"remove", std::bind(applyRemoveOperation, _1, _2)},
      {"add", std::bind(applyAddOperation, _1, _2)},
      {"replace", std::bind(applyReplaceOperation, _1, _2)},
      {"move", std::bind(applyMoveOperation, _1, _2)},
      {"copy", std::bind(applyCopyOperation, _1, _2)},
      {"merge", std::bind(applyMergeOperation, _1, _2)},
  };

  Json applyOperation(Json const& base, Json const& op, Maybe<Json> const& external) {
    try {
      auto operation = op.getString("op");
      return JsonPatching::functionMap.get(operation)(base, op);
    } catch (JsonException const& e) {
      throw JsonPatchException(strf("Could not apply operation to base. {}", e.what()), false);
    } catch (MapException const&) {
      throw JsonPatchException(strf("Invalid operation: {}", op.getString("op")), false);
    }
  }

  Json applyTestOperation(Json const& base, Json const& op) {
    String path = op.getString("path");
    auto pointer = JsonPath::Pointer(path);
    auto inverseTest = op.getBool("inverse", false);

    try {
      if (op.contains("search")) {
        auto searchable = pointer.get(base);
        auto searchValue = op.get("search");
        bool found = findJsonMatch(searchable, searchValue, pointer);
        if (found && inverseTest)
          throw JsonPatchTestFail(strf("Test operation failure, expected {} to be missing.", searchValue), false);
        else if (!found && !inverseTest)
          throw JsonPatchTestFail(strf("Test operation failure, could not find {}.", searchValue), false);
        return base;
      } else {
        auto value = op.opt("value");
        auto testValue = pointer.get(base);
        if (!value) {
          if (inverseTest)
            throw JsonPatchTestFail(strf("Test operation failure, expected {} to be missing.", path), false);
          return base;
        }

        if ((value && (testValue == *value)) ^ inverseTest)
          return base;
        else
          throw JsonPatchTestFail(strf("Test operation failure, expected {} found {}.", value, testValue), false);
      }
    } catch (JsonPath::TraversalException& e) {
      if (inverseTest)
        return base;
      throw JsonPatchTestFail(strf("Test operation failure: {}", e.what()), false);
    }
  }

  Json applyRemoveOperation(Json const& base, Json const& op) {
    String path = op.getString("path");
    auto pointer = JsonPath::Pointer(path);

    if (op.contains("search")) {
      auto searchable = pointer.get(base);
      auto searchValue = op.get("search");
      if (size_t index = findJsonMatch(searchable, searchValue, pointer))
        return pointer.add(pointer.remove(base), searchable.eraseIndex(index - 1));
      else
        return base;
    } else {
      return pointer.remove(base);
    }
  }

  Json applyAddOperation(Json const& base, Json const& op) {
    String path = op.getString("path");
    auto value = op.get("value");
    auto pointer = JsonPath::Pointer(path);

    if (op.contains("search")) {
      auto searchable = pointer.get(base);
      auto searchValue = op.get("search");
      if (size_t index = findJsonMatch(searchable, searchValue, pointer))
        return pointer.add(pointer.remove(base), searchable.insert(index - 1, value));
      else
        return base;
    } else {
      return pointer.add(base, value);
    }
  }

  Json applyReplaceOperation(Json const& base, Json const& op) {
    String path = op.getString("path");
    auto value = op.get("value");
    auto pointer = JsonPath::Pointer(op.getString("path"));

    if (op.contains("search")) {
      auto searchable = pointer.get(base);
      auto searchValue = op.get("search");
      if (size_t index = findJsonMatch(searchable, searchValue, pointer))
        return pointer.add(pointer.remove(base), searchable.set(index - 1, value));
      else
        return base;
    } else {
      return pointer.add(pointer.remove(base), value);
    }
  }

  Json applyMoveOperation(Json const& base, Json const& op) {
    String path = op.getString("path");
    auto toPointer = JsonPath::Pointer(path);
    auto fromPointer = JsonPath::Pointer(op.getString("from"));

    if (op.contains("search")) {
      auto searchable = fromPointer.get(base);
      auto searchValue = op.get("search");
      if (size_t index = findJsonMatch(searchable, searchValue, fromPointer)) {
        auto result = toPointer.add(base, searchable.get(index - 1));
        return fromPointer.add(result, searchable.eraseIndex(index - 1));
      }
      else
        return base;
    } else {
      Json value = fromPointer.get(base);
      return toPointer.add(fromPointer.remove(base), value);
    }
  }

  Json applyCopyOperation(Json const& base, Json const& op) {
    String path = op.getString("path");
    auto toPointer = JsonPath::Pointer(path);
    auto fromPointer = JsonPath::Pointer(op.getString("from"));

    if (op.contains("search")) {
      auto searchable = fromPointer.get(base);
      auto searchValue = op.get("search");
      if (size_t index = findJsonMatch(searchable, searchValue, fromPointer))
        return toPointer.add(base, searchable.get(index - 1));
      else
        return base;
    } else {
      Json value = fromPointer.get(base);
      return toPointer.add(base, value);
    }
  }

  Json applyMergeOperation(Json const& base, Json const& op) {
    String path = op.getString("path");
    auto pointer = JsonPath::Pointer(path);

    if (op.contains("search")) {
      auto searchable = pointer.get(base);
      auto searchValue = op.get("search");
      if (size_t index = findJsonMatch(searchable, searchValue, pointer))
        return pointer.add(pointer.remove(base), searchable.set(index - 1, jsonMerge(searchable.get(index - 1), op.get("value"))));
      else
        return base;
    } else {
      return pointer.add(pointer.remove(base), jsonMerge(pointer.get(base), op.get("value")));
    }
  }
}

}