-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatchOperationExtension.cs
381 lines (345 loc) · 19.5 KB
/
PatchOperationExtension.cs
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
using Microsoft.Azure.Cosmos;
using Newtonsoft.Json.Linq;
using System.Reflection;
using Microsoft.AspNetCore.JsonPatch.Operations;
using Microsoft.VisualStudio.Services.WebApi.Patch.Json;
namespace CosmosDBPartialUpdateTypeConverter
{
/// <summary>
/// Need more work
/// </summary>
public static class PatchOperationExtension
{
public static List<PatchOperation> Add(this List<PatchOperation> patchOperations, Microsoft.AspNetCore.JsonPatch.JsonPatchDocument jsonPatchDocument)
{
ArgumentNullException.ThrowIfNull(jsonPatchDocument, nameof(jsonPatchDocument));
//From Microsoft:
//Azure Cosmos DB partial document update is inspired by JSON Patch RFC 6902.
//There are other features such as Conditional Patch
//while some of the features of JSON Patch RFC 6902 such as (Copy, Test) have not been implemented.
//No-op for this case would be just don't do anything I suppose
foreach (var operation in jsonPatchDocument.Operations)
{
switch (operation.OperationType)
{
case OperationType.Add:
patchOperations.Add(PatchOperation.Add(operation.path, operation.value));
break;
case OperationType.Copy:
break;
case OperationType.Move:
patchOperations.Add(PatchOperation.Move(operation.from, operation.path));
break;
case OperationType.Remove:
patchOperations.Add(PatchOperation.Remove(operation.path));
break;
case OperationType.Replace:
patchOperations.Add(PatchOperation.Replace(operation.path, operation.value));
break;
case OperationType.Test:
break;
case OperationType.Invalid: //create an invalid patch operation[?]
break;
default:
break;
}
}
return patchOperations;
}
public static List<PatchOperation> Add(this List<PatchOperation> patchOperations, List<PatchOperation> operations)
{
patchOperations.AddRange(operations);
return patchOperations;
}
public static List<PatchOperation> Add<T>(this List<PatchOperation> patchOperations, List<PatchOperation<T>> operations)
{
patchOperations.AddRange(operations);
return patchOperations;
}
public static List<PatchOperation<T>> Add<T>(this List<PatchOperation<T>> patchOperations, List<PatchOperation<T>> operations)
{
patchOperations.AddRange(operations);
return patchOperations;
}
public static List<PatchOperation> Add(this List<PatchOperation> patchOperations, params PatchOperation[] operations)
{
patchOperations.AddRange(operations);
return patchOperations;
}
public static List<PatchOperation> Add(this List<PatchOperation> patchOperations, string path, object? value)
{
patchOperations.Add(PatchOperation.Add(BuildPath(path), value));
return patchOperations;
}
public static List<PatchOperation> Add<T>(this List<PatchOperation> patchOperations, string path, T? value)
{
patchOperations.Add(PatchOperation.Add(BuildPath(path), value));
return patchOperations;
}
public static List<PatchOperation> Add(this List<PatchOperation> patchOperations, JObject jObject)
{
ArgumentNullException.ThrowIfNull(jObject, nameof(jObject));
foreach (var property in jObject.Properties())
{
string key = property.Name;
JToken value = property.Value;
patchOperations.Add(PatchOperation.Add(BuildPath(key), value));
}
return patchOperations;
}
public static List<PatchOperation> Add<T>(this List<PatchOperation> patchOperations, T entity)
where T : class
{
patchOperations.AddRange(entity switch
{
string => Enumerable.Empty<PatchOperation>(),
_ => typeof(T).GetProperties().Select(property => PatchOperation.Add(BuildPath(property.Name), property.GetValue(entity)))
});
return patchOperations;
}
public static List<PatchOperation> Add<T>(this List<PatchOperation> patchOperations, List<T> entities)
where T : class
{
patchOperations.AddRange(entities.Where(entity => entity is not string)
.SelectMany(entity => entity.GetType().GetProperties().Select(property => PatchOperation.Add(BuildPath(property.Name), property.GetValue(entity)))));
return patchOperations;
}
public static List<PatchOperation> Add<T>(this List<PatchOperation> patchOperations, IEnumerable<T> entities)
where T : class
{
patchOperations.AddRange(entities.Where(entity => entity is not string)
.SelectMany(entity => entity.GetType().GetProperties().Select(property => PatchOperation.Add(BuildPath(property.Name), property.GetValue(entity)))));
return patchOperations;
}
public static List<PatchOperation> Add<T>(this List<PatchOperation> patchOperations, params T[] entities)
where T : class
{
patchOperations.AddRange(entities switch
{
[string item1, string item2] => [PatchOperation.Add(BuildPath(item1), item2)],
_ => entities.Where(entity => entity is not string).SelectMany(entity => entity.GetType().GetProperties().Select(property => PatchOperation.Add(BuildPath(property.Name), property.GetValue(entity))))
});
return patchOperations;
}
public static List<PatchOperation> Add(this List<PatchOperation> patchOperations, object value, Func<PropertyInfo, bool>? propertyInfoFilter = null)
{
ArgumentNullException.ThrowIfNull(value, nameof(value));
patchOperations.AddRange(value switch
{
(string item1, object item2) => [PatchOperation.Add(BuildPath(item1), item2)],
(object item1, object item2) => [PatchOperation.Add(BuildPath(item1.ToString()), item2)], //need to check null or do something
int or long or float or double or decimal or string or bool or null => Enumerable.Empty<PatchOperation>(),
_ => value.GetType().GetProperties().Where(propertyInfoFilter is not null ? propertyInfoFilter : _ => true)
.Select(property => PatchOperation.Add(BuildPath(property.Name), property.GetValue(value)))
});
return patchOperations;
}
public static List<PatchOperation> Add(this List<PatchOperation> patchOperations, IEnumerable<object> values)
{
patchOperations.AddRange(values.SelectMany(value => value switch
{
(string item1, object item2) => [PatchOperation.Add(BuildPath(item1), item2)],
(object item1, object item2) => [PatchOperation.Add(BuildPath(item1.ToString()), item2)], //need to check null or do something
int or long or float or double or decimal or string or bool or null => Enumerable.Empty<PatchOperation>(),
_ => value.GetType().GetProperties()
.Select(property => PatchOperation.Add(BuildPath(property.Name), property.GetValue(value)))
}));
return patchOperations;
}
public static List<PatchOperation> Add(this List<PatchOperation> patchOperations, params object[] values)
{
patchOperations.AddRange(values.SelectMany(value => value switch
{
(string item1, object item2) => [PatchOperation.Add(BuildPath(item1), item2)],
(object item1, object item2) => [PatchOperation.Add(BuildPath(item1.ToString()), item2)], //need to check null or do something
int or long or float or double or decimal or string or bool or null => Enumerable.Empty<PatchOperation>(),
_ => value.GetType().GetProperties()
.Select(property => PatchOperation.Add(BuildPath(property.Name), property.GetValue(value)))
}));
return patchOperations;
}
//For Appending to an json array
//the path must exist first
//need more testing for the corner cases
public static List<PatchOperation> AddAppend(this List<PatchOperation> patchOperations, string path, object? value)
{
patchOperations.Add(PatchOperation.Add($"{BuildPath(path)}/`", value));
return patchOperations;
}
public static List<PatchOperation> AddIncrement(this List<PatchOperation> patchOperations, string path, long value)
{
patchOperations.Add(PatchOperation.Increment(BuildPath(path), value));
return patchOperations;
}
public static List<PatchOperation> AddIncrement(this List<PatchOperation> patchOperations, string path, double value)
{
patchOperations.Add(PatchOperation.Increment(BuildPath(path), value));
return patchOperations;
}
public static List<PatchOperation> AddMove(this List<PatchOperation> patchOperations, string from, string path)
{
patchOperations.Add(PatchOperation.Move(BuildPath(from), BuildPath(path)));
return patchOperations;
}
public static List<PatchOperation> AddRemove(this List<PatchOperation> patchOperations, string path)
{
patchOperations.Add(PatchOperation.Remove(BuildPath(path)));
return patchOperations;
}
public static List<PatchOperation> AddReplace(this List<PatchOperation> patchOperations, string path, object? value)
{
patchOperations.Add(PatchOperation.Replace(BuildPath(path), value));
return patchOperations;
}
public static List<PatchOperation> AddSet(this List<PatchOperation> patchOperations, string path, object? value)
{
patchOperations.Add(PatchOperation.Set(BuildPath(path), value));
return patchOperations;
}
public static List<PatchOperation> AddSet<T>(this List<PatchOperation> patchOperations, string path, T? value)
{
patchOperations.Add(PatchOperation.Set(BuildPath(path), value));
return patchOperations;
}
public static List<PatchOperation> AddSet<T>(this List<PatchOperation> patchOperations, T entity)
where T : class
{
patchOperations.AddRange(entity switch
{
string => Enumerable.Empty<PatchOperation>(),
_ => typeof(T).GetProperties().Select(property => PatchOperation.Set(BuildPath(property.Name), property.GetValue(entity)))
});
return patchOperations;
}
public static List<PatchOperation> AddSet(this List<PatchOperation> patchOperations, JObject jObject)
{
ArgumentNullException.ThrowIfNull(jObject, nameof(jObject));
foreach (var property in jObject.Properties())
{
string key = property.Name;
JToken value = property.Value;
patchOperations.Add(PatchOperation.Set(BuildPath(key), value));
}
return patchOperations;
}
public static List<PatchOperation> AddSet<T>(this List<PatchOperation> patchOperations, IEnumerable<T> entities)
where T : class
{
patchOperations.AddRange(entities.Where(entity => entity is not string)
.SelectMany(entity => entity.GetType().GetProperties().Select(property => PatchOperation.Set(BuildPath(property.Name), property.GetValue(entity)))));
return patchOperations;
}
public static List<PatchOperation> AddSet<T>(this List<PatchOperation> patchOperations, params T[] entities)
where T : class
{
patchOperations.AddRange(entities switch
{
[string item1, string item2] => [PatchOperation.Set(BuildPath(item1), item2)],
_ => entities.Where(entity => entity is not string).SelectMany(entity => entity.GetType().GetProperties().Select(property => PatchOperation.Set(BuildPath(property.Name), property.GetValue(entity))))
});
return patchOperations;
}
public static List<PatchOperation> AddSet(this List<PatchOperation> patchOperations, object value, Func<PropertyInfo, bool>? propertyInfoFilter = null)
{
ArgumentNullException.ThrowIfNull(value, nameof(value));
patchOperations.AddRange(value switch
{
(string item1, object item2) => [PatchOperation.Set(BuildPath(item1), item2)],
(object item1, object item2) => [PatchOperation.Set(BuildPath(item1.ToString()), item2)], //need to check null or do something
int or long or float or double or decimal or string or bool or null => Enumerable.Empty<PatchOperation>(),
_ => value.GetType().GetProperties().Where(propertyInfoFilter is not null ? propertyInfoFilter : _ => true)
.Select(property => PatchOperation.Set(property.Name, property.GetValue(value)))
});
return patchOperations;
}
public static List<PatchOperation> AddSet(this List<PatchOperation> patchOperations, IEnumerable<object> values)
{
patchOperations.AddRange(values.SelectMany(value => value switch
{
(string item1, object item2) => [PatchOperation.Set(BuildPath(item1), item2)],
(object item1, object item2) => [PatchOperation.Set(BuildPath(item1.ToString()), item2)], //need to check null or do something
int or long or float or double or decimal or string or bool or null => Enumerable.Empty<PatchOperation>(),
_ => value.GetType().GetProperties()
.Select(property => PatchOperation.Set(property.Name, property.GetValue(value)))
}));
return patchOperations;
}
public static List<PatchOperation> AddSet(this List<PatchOperation> patchOperations, params object[] values)
{
patchOperations.AddRange(values.SelectMany(value => value switch
{
(string item1, object item2) => [PatchOperation.Set(BuildPath(item1), item2)],
(object item1, object item2) => [PatchOperation.Set(BuildPath(item1.ToString()), item2)], //need to check null or do something
int or long or float or double or decimal or string or bool or null => Enumerable.Empty<PatchOperation>(),
_ => value.GetType().GetProperties()
.Select(property => PatchOperation.Set(BuildPath(property.Name), property.GetValue(value)))
}));
return patchOperations;
}
//Todo
//Should use regex to check if string is of the form ^\/[A-Za-z0-9]+$
//or of the form ^\/[A-Za-z0-9]+\/[A-Za-z0-9]+$, recursively for more than 2 level
//for path such as /address/city
public static string BuildPath(string path)
{
return path.StartsWith('/') ? path : $"/{path}";
}
public static PatchOperation ToPatchOperation<T>(this PatchOperation<T> patchOperation)
{
return patchOperation.OperationType switch
{
PatchOperationType.Add => PatchOperation.Add(patchOperation.Path, patchOperation.Value),
PatchOperationType.Remove => PatchOperation.Remove(patchOperation.Path),
PatchOperationType.Replace => PatchOperation.Replace(patchOperation.Path, patchOperation.Value),
PatchOperationType.Set => PatchOperation.Set(patchOperation.Path, patchOperation.Value),
PatchOperationType.Increment => patchOperation switch
{
PatchOperation<long> longPatchOperation => PatchOperation.Increment(patchOperation.Path, longPatchOperation.Value),
PatchOperation<double> doublePatchOperation => PatchOperation.Increment(patchOperation.Path, doublePatchOperation.Value),
_ => throw new NotSupportedException("Unsupported PatchOperation type")
},
PatchOperationType.Move => PatchOperation.Move(patchOperation.From, patchOperation.Path),
_ => throw new NotSupportedException("Unsupported PatchOperation type")
};
}
public static PatchOperation ToPatchOperation(this Operation operation)
{
return operation.OperationType switch
{
OperationType.Add => PatchOperation.Add(operation.path, operation.value),
OperationType.Remove => PatchOperation.Remove(operation.path),
OperationType.Replace => PatchOperation.Replace(operation.path, operation.value),
OperationType.Move => PatchOperation.Move(operation.from, operation.path),
OperationType.Copy => throw new NotSupportedException("Copy operation is not supported"),
OperationType.Test => throw new NotSupportedException("Test operation is not supported"),
_ => throw new NotSupportedException("Unsupported OperationType")
};
}
public static PatchOperation ToPatchOperation<T>(this Operation<T> operation) where T : class
{
return operation.OperationType switch
{
OperationType.Add => PatchOperation.Add(operation.path, operation.value),
OperationType.Remove => PatchOperation.Remove(operation.path),
OperationType.Replace => PatchOperation.Replace(operation.path, operation.value),
OperationType.Move => PatchOperation.Move(operation.from, operation.path),
OperationType.Copy => throw new NotSupportedException("Copy operation is not supported"),
OperationType.Test => throw new NotSupportedException("Test operation is not supported"),
_ => throw new NotSupportedException("Unsupported OperationType")
};
}
public static PatchOperation ToPatchOperation(this JsonPatchOperation jsonPatchOperation)
{
return jsonPatchOperation.Operation switch
{
Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add => PatchOperation.Add(jsonPatchOperation.Path, jsonPatchOperation.Value),
Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Remove => PatchOperation.Remove(jsonPatchOperation.Path),
Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Replace => PatchOperation.Replace(jsonPatchOperation.Path, jsonPatchOperation.Value),
Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Move => PatchOperation.Move(jsonPatchOperation.From, jsonPatchOperation.Path),
Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Copy => throw new NotSupportedException("Copy operation is not supported"),
Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Test => throw new NotSupportedException("Test operation is not supported"),
_ => throw new NotSupportedException("Unsupported OperationType")
};
}
}
}