-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPatchOperationList.cs
More file actions
161 lines (115 loc) · 6.49 KB
/
PatchOperationList.cs
File metadata and controls
161 lines (115 loc) · 6.49 KB
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
using Microsoft.Azure.Cosmos;
using Newtonsoft.Json.Linq;
using System.Collections;
using System.Reflection;
using Microsoft.AspNetCore.JsonPatch;
namespace CosmosDBPartialUpdateTypeConverter
{
/// <summary>
///Not done yet
///Maybe to limit to 10 PatchOperations is not a good idea
///Need more testing
///Not thread safe
///https://learn.microsoft.com/en-us/azure/cosmos-db/partial-document-update-faq
/// </summary>
public class PatchOperationList(List<PatchOperation> _patchOperations) : IList<PatchOperation>, IReadOnlyList<PatchOperation>
{
public static implicit operator PatchOperationList(List<PatchOperation> patchOperations) => new PatchOperationList([..patchOperations]); //shadow copy to prevent modifications to the original list
public static implicit operator PatchOperationList(PatchOperation[] patchOperations) => new PatchOperationList([..patchOperations]); //shadow copy to prevent modifications to the original list
public static implicit operator PatchOperationList(PatchOperation patchOperation) => new PatchOperationList(patchOperation);
public PatchOperationList() : this([]) { }
public PatchOperationList(PatchOperationList patchOperationList) : this([.. patchOperationList]) { } //shadow copy to prevent modifications to the original list
public PatchOperationList(IList<PatchOperation> patchOperations) : this([.. patchOperations]) { } //shadow copy to prevent modifications to the original list
public PatchOperationList(IEnumerable<PatchOperation> patchOperations) : this(patchOperations.ToList()) { } //shadow a copy to prevent modifications to the original list
public PatchOperationList(PatchOperation patchOperation) : this([patchOperation]) { }
public int Count => _patchOperations.Count;
public PatchOperation this[int index]
{
get => _patchOperations[index];
set => _patchOperations[index] = value;
}
public PatchOperationListBuilder Builder => new(_patchOperations);
public bool IsReadOnly => false;
PatchOperation IList<PatchOperation>.this[int index]
{
get => (_patchOperations as IList<PatchOperation>)[index];
set => (_patchOperations as IList<PatchOperation>)[index] = value;
}
public void Add(string path, object? value) => _patchOperations.Add(path, value);
public void Add(PatchOperation item) => _patchOperations.Add(item);
public void Add(JObject jObject)
{
ArgumentNullException.ThrowIfNull(jObject, nameof(jObject));
_patchOperations.Add(jObject);
}
public void Add(JsonPatchDocument jsonPatchDocument)
{
ArgumentNullException.ThrowIfNull(jsonPatchDocument, nameof(jsonPatchDocument));
_patchOperations.Add(jsonPatchDocument);
}
public void Add(List<PatchOperation> operations) => _patchOperations.Add(operations);
public void Add<T>(T entity)
where T : class
{
ArgumentNullException.ThrowIfNull(entity, nameof(entity));
_patchOperations.Add(entity);
}
public void Add<T>(IEnumerable<T> entities)
where T : class => _patchOperations.Add(entities);
public void Add<T>(List<T> entities)
where T : class => _patchOperations.Add(entities);
public void Add<T>(params T[] entities)
where T : class
{
if(entities is PatchOperation[] patchOperations)
{
_patchOperations.Add(patchOperations);
}
else
{
_patchOperations.Add(entities);
}
}
public void Add(object value, Func<PropertyInfo, bool>? propertyInfoFilter = null)
{
ArgumentNullException.ThrowIfNull(value, nameof(value));
_patchOperations.Add(value, propertyInfoFilter);
}
public void AddAppend(string path, object? value) => _patchOperations.AddAppend(path, value);
public void AddIncrement(string path, long value) => _patchOperations.AddIncrement(path, value);
public void AddIncrement(string path, double value) => _patchOperations.AddIncrement(path, value);
public void AddMove(string from, string path) => _patchOperations.AddMove(from, path);
public void AddRemove(string path) => _patchOperations.AddRemove(path);
public void AddReplace(string path, object? value) => _patchOperations.AddReplace(path, value);
public void AddSet(string path, object? value) => _patchOperations.AddSet(path, value);
public void AddSet(JObject value)
{
ArgumentNullException.ThrowIfNull(value, nameof(value));
_patchOperations.AddSet(value);
}
public void AddSet<T>(T entity)
where T : class
{
ArgumentNullException.ThrowIfNull(entity, nameof(entity));
_patchOperations.AddSet(entity);
}
public void AddSet<T>(IEnumerable<T> entities)
where T : class => _patchOperations.AddSet(entities);
public void AddSet<T>(params T[] entities)
where T : class => _patchOperations.AddSet(entities);
public void AddSet(object value, Func<PropertyInfo, bool>? propertyInfoFilter = null) => _patchOperations.AddSet(value, propertyInfoFilter);
public void AddSet(IEnumerable<object> values) => _patchOperations.AddSet(values);
public void AddSet(params object[] values) => _patchOperations.AddSet(values);
public int IndexOf(PatchOperation item) => _patchOperations.IndexOf(item);
public void Insert(int index, PatchOperation item) => _patchOperations.Insert(index, item);
public void RemoveAt(int index) => _patchOperations.RemoveAt(index);
public void Clear() => _patchOperations.Clear();
public bool Contains(PatchOperation item) => _patchOperations.Contains(item);
public void CopyTo(PatchOperation[] array, int arrayIndex) => _patchOperations.CopyTo(array, arrayIndex);
public bool Remove(PatchOperation item) => _patchOperations.Remove(item);
public IEnumerator<PatchOperation> GetEnumerator() => _patchOperations.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public void ForEach(Action<PatchOperation> action) => _patchOperations.ForEach(action);
public IReadOnlyList<PatchOperation> AsReadOnly() => _patchOperations.AsReadOnly();
}
}