| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Xml.Linq; |
| | 6 | | using Azure.Messaging.ServiceBus.Diagnostics; |
| | 7 | |
|
| | 8 | | namespace Azure.Messaging.ServiceBus.Management |
| | 9 | | { |
| | 10 | | internal static class RuleFilterExtensions |
| | 11 | | { |
| | 12 | | public static RuleFilter ParseFromXElement(XElement xElement) |
| | 13 | | { |
| 48 | 14 | | var attribute = xElement.Attribute(XName.Get("type", ManagementClientConstants.XmlSchemaInstanceNamespace)); |
| 48 | 15 | | if (attribute == null) |
| | 16 | | { |
| 0 | 17 | | return null; |
| | 18 | | } |
| | 19 | |
|
| 48 | 20 | | switch (attribute.Value) |
| | 21 | | { |
| | 22 | | case "SqlFilter": |
| 16 | 23 | | return SqlRuleFilterExtensions.ParseFromXElement(xElement); |
| | 24 | | case "CorrelationFilter": |
| 20 | 25 | | return CorrelationRuleFilterExtensions.ParseFromXElement(xElement); |
| | 26 | | case "TrueFilter": |
| 12 | 27 | | return new TrueRuleFilter(); |
| | 28 | | case "FalseFilter": |
| 0 | 29 | | return new FalseRuleFilter(); |
| | 30 | | default: |
| 0 | 31 | | ServiceBusEventSource.Log.ManagementSerializationException( |
| 0 | 32 | | $"{nameof(RuleFilterExtensions)}_{nameof(ParseFromXElement)}", |
| 0 | 33 | | xElement.ToString()); |
| 0 | 34 | | return null; |
| | 35 | | } |
| | 36 | | } |
| | 37 | |
|
| | 38 | | public static XElement Serialize(this RuleFilter filter) |
| | 39 | | { |
| | 40 | | switch (filter) |
| | 41 | | { |
| | 42 | | case SqlRuleFilter sqlRuleFilter: |
| | 43 | | switch (sqlRuleFilter) |
| | 44 | | { |
| | 45 | | case TrueRuleFilter _: |
| 28 | 46 | | return sqlRuleFilter.Serialize("TrueFilter"); |
| | 47 | |
|
| | 48 | | case FalseRuleFilter _: |
| 0 | 49 | | return sqlRuleFilter.Serialize("FalseFilter"); |
| | 50 | |
|
| | 51 | | default: |
| 8 | 52 | | return sqlRuleFilter.Serialize("SqlFilter"); |
| | 53 | | } |
| | 54 | |
|
| | 55 | | case CorrelationRuleFilter correlationRuleFilter: |
| 12 | 56 | | return correlationRuleFilter.Serialize("CorrelationFilter"); |
| | 57 | |
|
| | 58 | | default: |
| 0 | 59 | | throw new NotImplementedException($"filter type {filter.GetType().Name} is not supported."); |
| | 60 | | } |
| | 61 | | } |
| | 62 | | } |
| | 63 | | } |