|
1 | | -using System.Collections.Generic; |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.Linq; |
3 | 4 | using System.Runtime.Serialization; |
4 | 5 | using Elasticsearch.Net; |
@@ -120,8 +121,108 @@ private void AddAction(ActionBase agg) |
120 | 121 | } |
121 | 122 | } |
122 | 123 |
|
| 124 | + internal class ActionsInterfaceFormatter : IJsonFormatter<IActions> |
| 125 | + { |
| 126 | + private static readonly ActionsFormatter ActionsFormatter = new ActionsFormatter(); |
| 127 | + |
| 128 | + public void Serialize(ref JsonWriter writer, IActions value, IJsonFormatterResolver formatterResolver) |
| 129 | + { |
| 130 | + writer.WriteBeginObject(); |
| 131 | + if (value != null) |
| 132 | + { |
| 133 | + var count = 0; |
| 134 | + foreach (var kvp in value.Where(kv => kv.Value != null)) |
| 135 | + { |
| 136 | + if (count > 0) |
| 137 | + writer.WriteValueSeparator(); |
| 138 | + |
| 139 | + var action = kvp.Value; |
| 140 | + writer.WritePropertyName(kvp.Key); |
| 141 | + writer.WriteBeginObject(); |
| 142 | + if (action.ThrottlePeriod != null) |
| 143 | + { |
| 144 | + writer.WritePropertyName("throttle_period"); |
| 145 | + var timeFormatter = formatterResolver.GetFormatter<Time>(); |
| 146 | + timeFormatter.Serialize(ref writer, action.ThrottlePeriod, formatterResolver); |
| 147 | + writer.WriteValueSeparator(); |
| 148 | + } |
| 149 | + |
| 150 | + if (!string.IsNullOrEmpty(action.Foreach)) |
| 151 | + { |
| 152 | + writer.WritePropertyName("foreach"); |
| 153 | + writer.WriteString(action.Foreach); |
| 154 | + writer.WriteValueSeparator(); |
| 155 | + } |
| 156 | + if (action.MaxIterations.HasValue) |
| 157 | + { |
| 158 | + writer.WritePropertyName("max_iterations"); |
| 159 | + writer.WriteInt32(action.MaxIterations.Value); |
| 160 | + writer.WriteValueSeparator(); |
| 161 | + } |
| 162 | + |
| 163 | + if (action.Transform != null) |
| 164 | + { |
| 165 | + writer.WritePropertyName("transform"); |
| 166 | + formatterResolver.GetFormatter<TransformContainer>().Serialize(ref writer, action.Transform, formatterResolver); |
| 167 | + writer.WriteValueSeparator(); |
| 168 | + } |
| 169 | + |
| 170 | + if (action.Condition != null) |
| 171 | + { |
| 172 | + writer.WritePropertyName("condition"); |
| 173 | + formatterResolver.GetFormatter<ConditionContainer>().Serialize(ref writer, action.Condition, formatterResolver); |
| 174 | + writer.WriteValueSeparator(); |
| 175 | + } |
| 176 | + |
| 177 | + writer.WritePropertyName(kvp.Value.ActionType.GetStringValue()); |
| 178 | + |
| 179 | + switch (action.ActionType) |
| 180 | + { |
| 181 | + case ActionType.Email: |
| 182 | + Serialize<IEmailAction>(ref writer, action, formatterResolver); |
| 183 | + break; |
| 184 | + case ActionType.Webhook: |
| 185 | + Serialize<IWebhookAction>(ref writer, action, formatterResolver); |
| 186 | + break; |
| 187 | + case ActionType.Index: |
| 188 | + Serialize<IIndexAction>(ref writer, action, formatterResolver); |
| 189 | + break; |
| 190 | + case ActionType.Logging: |
| 191 | + Serialize<ILoggingAction>(ref writer, action, formatterResolver); |
| 192 | + break; |
| 193 | + case ActionType.Slack: |
| 194 | + Serialize<ISlackAction>(ref writer, action, formatterResolver); |
| 195 | + break; |
| 196 | + case ActionType.PagerDuty: |
| 197 | + Serialize<IPagerDutyAction>(ref writer, action, formatterResolver); |
| 198 | + break; |
| 199 | + default: |
| 200 | + var actionFormatter = formatterResolver.GetFormatter<IAction>(); |
| 201 | + actionFormatter.Serialize(ref writer, action, formatterResolver); |
| 202 | + break; |
| 203 | + } |
| 204 | + |
| 205 | + writer.WriteEndObject(); |
| 206 | + count++; |
| 207 | + } |
| 208 | + } |
| 209 | + writer.WriteEndObject(); |
| 210 | + } |
| 211 | + |
| 212 | + private static void Serialize<TAction>(ref JsonWriter writer, IAction value, IJsonFormatterResolver formatterResolver) |
| 213 | + where TAction : class, IAction |
| 214 | + { |
| 215 | + var formatter = formatterResolver.GetFormatter<TAction>(); |
| 216 | + formatter.Serialize(ref writer, value as TAction, formatterResolver); |
| 217 | + } |
| 218 | + public IActions Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) => |
| 219 | + ActionsFormatter.Deserialize(ref reader, formatterResolver); |
| 220 | + } |
| 221 | + |
123 | 222 | internal class ActionsFormatter : IJsonFormatter<Actions> |
124 | 223 | { |
| 224 | + private static readonly ActionsInterfaceFormatter ActionsInterfaceFormatter = new ActionsInterfaceFormatter(); |
| 225 | + |
125 | 226 | private static readonly AutomataDictionary Fields = new AutomataDictionary |
126 | 227 | { |
127 | 228 | { "throttle_period", 0 }, |
@@ -225,95 +326,7 @@ public Actions Deserialize(ref JsonReader reader, IJsonFormatterResolver formatt |
225 | 326 | return new Actions(dictionary); |
226 | 327 | } |
227 | 328 |
|
228 | | - public void Serialize(ref JsonWriter writer, Actions value, IJsonFormatterResolver formatterResolver) |
229 | | - { |
230 | | - writer.WriteBeginObject(); |
231 | | - if (value != null) |
232 | | - { |
233 | | - var count = 0; |
234 | | - foreach (var kvp in value.Where(kv => kv.Value != null)) |
235 | | - { |
236 | | - if (count > 0) |
237 | | - writer.WriteValueSeparator(); |
238 | | - |
239 | | - var action = kvp.Value; |
240 | | - writer.WritePropertyName(kvp.Key); |
241 | | - writer.WriteBeginObject(); |
242 | | - if (action.ThrottlePeriod != null) |
243 | | - { |
244 | | - writer.WritePropertyName("throttle_period"); |
245 | | - var timeFormatter = formatterResolver.GetFormatter<Time>(); |
246 | | - timeFormatter.Serialize(ref writer, action.ThrottlePeriod, formatterResolver); |
247 | | - writer.WriteValueSeparator(); |
248 | | - } |
249 | | - |
250 | | - if (!string.IsNullOrEmpty(action.Foreach)) |
251 | | - { |
252 | | - writer.WritePropertyName("foreach"); |
253 | | - writer.WriteString(action.Foreach); |
254 | | - writer.WriteValueSeparator(); |
255 | | - } |
256 | | - if (action.MaxIterations.HasValue) |
257 | | - { |
258 | | - writer.WritePropertyName("max_iterations"); |
259 | | - writer.WriteInt32(action.MaxIterations.Value); |
260 | | - writer.WriteValueSeparator(); |
261 | | - } |
262 | | - |
263 | | - if (action.Transform != null) |
264 | | - { |
265 | | - writer.WritePropertyName("transform"); |
266 | | - formatterResolver.GetFormatter<TransformContainer>().Serialize(ref writer, action.Transform, formatterResolver); |
267 | | - writer.WriteValueSeparator(); |
268 | | - } |
269 | | - |
270 | | - if (action.Condition != null) |
271 | | - { |
272 | | - writer.WritePropertyName("condition"); |
273 | | - formatterResolver.GetFormatter<ConditionContainer>().Serialize(ref writer, action.Condition, formatterResolver); |
274 | | - writer.WriteValueSeparator(); |
275 | | - } |
276 | | - |
277 | | - writer.WritePropertyName(kvp.Value.ActionType.GetStringValue()); |
278 | | - |
279 | | - switch (action.ActionType) |
280 | | - { |
281 | | - case ActionType.Email: |
282 | | - Serialize<IEmailAction>(ref writer, action, formatterResolver); |
283 | | - break; |
284 | | - case ActionType.Webhook: |
285 | | - Serialize<IWebhookAction>(ref writer, action, formatterResolver); |
286 | | - break; |
287 | | - case ActionType.Index: |
288 | | - Serialize<IIndexAction>(ref writer, action, formatterResolver); |
289 | | - break; |
290 | | - case ActionType.Logging: |
291 | | - Serialize<ILoggingAction>(ref writer, action, formatterResolver); |
292 | | - break; |
293 | | - case ActionType.Slack: |
294 | | - Serialize<ISlackAction>(ref writer, action, formatterResolver); |
295 | | - break; |
296 | | - case ActionType.PagerDuty: |
297 | | - Serialize<IPagerDutyAction>(ref writer, action, formatterResolver); |
298 | | - break; |
299 | | - default: |
300 | | - var actionFormatter = formatterResolver.GetFormatter<IAction>(); |
301 | | - actionFormatter.Serialize(ref writer, action, formatterResolver); |
302 | | - break; |
303 | | - } |
304 | | - |
305 | | - writer.WriteEndObject(); |
306 | | - count++; |
307 | | - } |
308 | | - } |
309 | | - writer.WriteEndObject(); |
310 | | - } |
311 | | - |
312 | | - private static void Serialize<TAction>(ref JsonWriter writer, IAction value, IJsonFormatterResolver formatterResolver) |
313 | | - where TAction : class, IAction |
314 | | - { |
315 | | - var formatter = formatterResolver.GetFormatter<TAction>(); |
316 | | - formatter.Serialize(ref writer, value as TAction, formatterResolver); |
317 | | - } |
| 329 | + public void Serialize(ref JsonWriter writer, Actions value, IJsonFormatterResolver formatterResolver) => |
| 330 | + ActionsInterfaceFormatter.Serialize(ref writer, value, formatterResolver); |
318 | 331 | } |
319 | 332 | } |
0 commit comments