-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_patch_writer.cpp
310 lines (260 loc) · 14.1 KB
/
json_patch_writer.cpp
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "json_patch_writer.h"
#include "utilities.h"
using json = nlohmann::json;
JsonPatchWriter::JsonPatchWriter(MasterSettings masterSettings) {
baselinePatchStyle = masterSettings.baselinePatchStyle;
useInverseTestOps = masterSettings.getUseInverseTestOps();
useOperationSets = masterSettings.getUseOperationSets();
}
int JsonPatchWriter::writePatchFile(std::stringstream & patchText, FileSettings & fileSettings, const json & sourceJson, const json & intermediaryJson) {
indentModifier = baselinePatchStyle.getIndentationInOuterBrackets() ? 1 : 0;
currentOps = 0;
currentOpSets = 0;
//Add patch makers comment if configured.
if (fileSettings.getAddPatchMakersComment() && intermediaryJson.contains("patchMakerComment")) {
std::string comment = intermediaryJson["patchMakerComment"];
if (comment != "") {
patchText << "//" << comment << std::endl;
}
}
//Opening outer bracket
patchText << '[';
if (baselinePatchStyle.getNewLineAfterOuterOpenerBracket()) patchText << std::endl;
//TODO: if only one op set in a patch skip set brackets (configurable)
//Write operation sets
for (const PointerSettings & pointerSettings : fileSettings.getAllPointerSettings()) {
if (!pointerSettings.reference) {
writeRecursiveOperationSet(patchText, pointerSettings, sourceJson, intermediaryJson);
}
}
//New line after the last operation set closer.
if (useOperationSets && (baselinePatchStyle.getNewLineAfterOperationSetCloserBracket() || baselinePatchStyle.getNewLineAfterOperationSetCloserBracketComma())) patchText << std::endl;
//Closing outer bracket
patchText << ']';
if (baselinePatchStyle.getNewLineAfterOuterCloserBracket()) patchText << std::endl;
return currentOpSets;
}
bool JsonPatchWriter::writeOperationSet(std::stringstream & patchText, const PointerSettings & pointerSettings, const json & sourceJson, const json & intermediaryJson) {
//Value path as JSON pointer.
const json::json_pointer valuePointer = json::json_pointer(pointerSettings.path);
//Intermediary value found.
if (intermediaryJson.contains(pointerSettings.path)) {
//Intermediary value is placeholder.
if (intermediaryJson[pointerSettings.path] == "") {
//Operation if placeholder is not none and source value not found.
if (pointerSettings.patchOperationIfPlaceholder == none || !sourceJson.contains(valuePointer)) {
//Patch operation if placeholder set to copy.
if (pointerSettings.patchOperationIfPlaceholder == copy) {
writeOperationSetOpener(patchText);
if (pointerSettings.patchTestOperation && useInverseTestOps) {
writeOperation(patchText, pointerSettings.from, "false", testInverse);
writeOperation(patchText, pointerSettings.path, "true", testInverse);
}
writeOperation(patchText, pointerSettings.from, pointerSettings.path, copyValue);
writeOperationSetCloser(patchText);
//Patch operation if placeholder set to move.
} else if (pointerSettings.patchOperationIfPlaceholder == move) {
writeOperationSetOpener(patchText);
if (pointerSettings.patchTestOperation && useInverseTestOps) {
writeOperation(patchText, pointerSettings.from, "false", testInverse);
writeOperation(patchText, pointerSettings.path, "true", testInverse);
}
writeOperation(patchText, pointerSettings.from, pointerSettings.path, moveValue);
writeOperationSetCloser(patchText);
}
}
//Intermediary value is not placeholder
} else {
//Source value found.
if (sourceJson.contains(valuePointer)) {
//Remove if equals is not blank and the source value matches it.
if (pointerSettings.patchRemoveIfEquals != "" && sourceJson[valuePointer].dump() == pointerSettings.patchRemoveIfEquals) {
writeOperationSetOpener(patchText);
if (pointerSettings.patchTestOperation) {
std::string sourceValueContents = sourceJson[valuePointer].dump();
if (pointerSettings.convertBreakoutNewlines) convertNewlineBreakoutsToNewline(sourceValueContents);
writeOperation(patchText, pointerSettings.path, sourceValueContents, testValue);
}
writeOperation(patchText, pointerSettings.path, "", removeValue);
writeOperationSetCloser(patchText);
//Intermediary value different from source value.
} else if (intermediaryJson[pointerSettings.path] != sourceJson[valuePointer]) {
writeOperationSetOpener(patchText);
if (pointerSettings.patchTestOperation) {
std::string sourceValueContents = sourceJson[valuePointer].dump();
if (pointerSettings.convertBreakoutNewlines) convertNewlineBreakoutsToNewline(sourceValueContents);
writeOperation(patchText, pointerSettings.path, sourceValueContents, testValue);
}
std::string intermediaryValueContents = intermediaryJson[pointerSettings.path].dump();
if (pointerSettings.convertBreakoutNewlines) convertNewlineBreakoutsToNewline(intermediaryValueContents);
writeOperation(patchText, pointerSettings.path, intermediaryValueContents, replaceValue);
writeOperationSetCloser(patchText);
}
//Source value not found.
} else {
writeOperationSetOpener(patchText);
if (pointerSettings.patchTestOperation && useInverseTestOps) writeOperation(patchText, pointerSettings.path, "true", testInverse);
std::string intermediaryValueContents = intermediaryJson[pointerSettings.path].dump();
if (pointerSettings.convertBreakoutNewlines) convertNewlineBreakoutsToNewline(intermediaryValueContents);
writeOperation(patchText, pointerSettings.path, intermediaryValueContents, addValue);
writeOperationSetCloser(patchText);
}
}
//Continue iteration.
return true;
}
//Stop iteration.
return false;
}
bool JsonPatchWriter::writeRecursiveOperationSet(std::stringstream & patchText, const PointerSettings & pointerSettings, const json & sourceJson, const json & intermediaryJson) {
//Check if there is a marker configured.
if (pointerSettings.numericIteratorMarker != "" && (pointerSettings.intermediaryPlaceholderCondition != fromExists || pointerSettings.from != "")) {
//Check if there is a marker position in path.
int pathMarkerPosition = pointerSettings.path.find(pointerSettings.numericIteratorMarker);
//Check if there is a marker position in from.
int fromMarkerPosition = pointerSettings.from.find(pointerSettings.numericIteratorMarker);
//If there is a marker to replace in path and, if required, from.
if (pathMarkerPosition != std::string::npos && (pointerSettings.intermediaryPlaceholderCondition != fromExists || fromMarkerPosition != std::string::npos)) {
//Pointer settings to be modified for the next recursion.
PointerSettings modifiedPointerSettings = pointerSettings;
int index = 0;
//Iterate with increasing index until no writes happen.
do {
std::string indexString = std::to_string(index);
//Replace the first marker in modifiedPath with the current index.
std::string modifiedPath = pointerSettings.path;
modifiedPath.erase(pathMarkerPosition, pointerSettings.numericIteratorMarker.length());
modifiedPath.insert(pathMarkerPosition, indexString);
modifiedPointerSettings.path = modifiedPath;
//If this segment of path doesn't exist stop iterating.
std::string testPath = modifiedPointerSettings.path;
testPath.erase(pathMarkerPosition + indexString.length());
if (pointerSettings.intermediaryPlaceholderCondition != fromExists && !sourceJson.contains(json::json_pointer(testPath))) {
break;
}
//Replace the first marker in modifiedFrom with the current index if required.
if (pointerSettings.intermediaryPlaceholderCondition == fromExists) {
std::string modifiedFrom = pointerSettings.from;
modifiedFrom.erase(fromMarkerPosition, pointerSettings.numericIteratorMarker.length());
modifiedFrom.insert(fromMarkerPosition, indexString);
modifiedPointerSettings.from = modifiedFrom;
//If this segment of from doesn't exist stop iterating.
std::string testFrom = modifiedPointerSettings.from;
testFrom.erase(fromMarkerPosition + indexString.length());
if (!sourceJson.contains(json::json_pointer(testFrom)) && !sourceJson.contains(json::json_pointer(testPath))) {
break;
}
}
index++;
//Recurse and get the result.
} while (writeRecursiveOperationSet(patchText, modifiedPointerSettings, sourceJson, intermediaryJson));
return true;
}
}
//There is no marker to replace.
return writeOperationSet(patchText, pointerSettings, sourceJson, intermediaryJson);
}
void JsonPatchWriter::writeOperation(std::stringstream & patchText, std::string path, std::string value, patchOperation operation) {
//Comma and new line after all but the last patch.
if (currentOps > 0) {
if (baselinePatchStyle.getNewLineAfterOperationCloserBracket()) patchText << std::endl;
patchText << ',';
if (baselinePatchStyle.getNewLineAfterOperationCloserBracketComma()) patchText << std::endl;
}
//Opening patch bracket.
writeIndent(patchText);
patchText << '{';
if (baselinePatchStyle.getNewLineAfterOperationOpenerBracket()) patchText << std::endl;
indentModifier += baselinePatchStyle.getIndentationInOperationBrackets() ? 1 : 0;
//op
writeIndent(patchText);
patchText << "\"op\"";
writeColon(patchText);
if (operation == addValue) {
patchText << "\"add\",";
} else if (operation == replaceValue) {
patchText << "\"replace\",";
} else if (operation == removeValue) {
patchText << "\"remove\",";
} else if (operation == copyValue) {
patchText << "\"copy\",";
} else if (operation == moveValue) {
patchText << "\"move\",";
} else if (operation == testValue || operation == testInverse) {
patchText << "\"test\",";
}
if (baselinePatchStyle.getNewLineAfterOperationSegment()) patchText << std::endl;
//copy/move from, other path.
writeIndent(patchText);
if (operation == copyValue || operation == moveValue) {
patchText << "\"from\"";
} else {
patchText << "\"path\"";
}
writeColon(patchText);
patchText << '\"' << path << '\"';
//Write third operation segment if not remove.
if (operation != removeValue) {
patchText << ',';
if (baselinePatchStyle.getNewLineAfterOperationSegment()) patchText << std::endl;
//copy/move path, inverse test inverse, other value.
writeIndent(patchText);
if (operation == copyValue || operation == moveValue) {
patchText << "\"path\"";
} else if (operation == testInverse) {
patchText << "\"inverse\"";
} else {
patchText << "\"value\"";
}
writeColon(patchText);
if (operation == copyValue || operation == moveValue) {
patchText << '\"' << value << '\"';
} else {
patchText << value;
}
}
if (baselinePatchStyle.getNewLineAfterOperationSegment()) patchText << std::endl;
//Closing patch bracket.
indentModifier -= baselinePatchStyle.getIndentationInOperationBrackets() ? 1 : 0;
writeIndent(patchText);
patchText << '}';
currentOps++;
}
void JsonPatchWriter::writeOperationSetOpener(std::stringstream & patchText) {
if (useOperationSets) {
currentOps = 0;
//Comma and new line after all but the last patch set.
if (currentOpSets > 0) {
if (baselinePatchStyle.getNewLineAfterOperationSetCloserBracket()) patchText << std::endl;
patchText << ',';
if (baselinePatchStyle.getNewLineAfterOperationSetCloserBracketComma()) patchText << std::endl;
}
//Opening patch set bracket
writeIndent(patchText);
patchText << '[';
if (baselinePatchStyle.getNewLineAfterOperationSetOpenerBracket()) patchText << std::endl;
indentModifier += baselinePatchStyle.getIndentationInOperationSetBrackets() ? 1 : 0;
}
}
void JsonPatchWriter::writeOperationSetCloser(std::stringstream & patchText) {
if (useOperationSets) {
//New line after the last patch.
if (baselinePatchStyle.getNewLineAfterOperationCloserBracket() || baselinePatchStyle.getNewLineAfterOperationCloserBracketComma()) patchText << std::endl;
//Closing patch set bracket
indentModifier -= baselinePatchStyle.getIndentationInOperationSetBrackets() ? 1 : 0;
writeIndent(patchText);
patchText << ']';
}
currentOpSets++;
}
void JsonPatchWriter::writeIndent(std::stringstream & patchText) {
const int indentBy = indentModifier * baselinePatchStyle.getIndentationSpacesPerModifier();
for (int i = 0; i < indentBy; i++) {
patchText << ' ';
}
}
void JsonPatchWriter::writeColon(std::stringstream & patchText) {
if (baselinePatchStyle.getSpaceBeforeOperationColon()) patchText << ' ';
patchText << ':';
if (baselinePatchStyle.getSpaceAfterOperationColon()) patchText << ' ';
}