|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +using System.Diagnostics.CodeAnalysis; |
| 6 | +using System.Reflection; |
| 7 | +using System.Text.Json; |
| 8 | +using System.Text.Json.Serialization; |
| 9 | + |
| 10 | +namespace Elastic.Documentation.Search.Common; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Serializes <c>Elastic.Internal.Search.Elasticsearch.RuleQueryMatchCriteria</c>, which is internal to the |
| 14 | +/// Elasticsearch search package and therefore not covered by public <see cref="JsonSerializerContext"/> types. |
| 15 | +/// </summary> |
| 16 | +internal sealed class RuleQueryMatchCriteriaJsonConverterFactory : JsonConverterFactory |
| 17 | +{ |
| 18 | + public static readonly RuleQueryMatchCriteriaJsonConverterFactory Instance = new(); |
| 19 | + |
| 20 | + [DynamicDependency( |
| 21 | + DynamicallyAccessedMemberTypes.All, |
| 22 | + "Elastic.Internal.Search.Elasticsearch.RuleQueryMatchCriteria", |
| 23 | + "Elastic.Internal.Search.Elasticsearch")] |
| 24 | + private static readonly Type RuleQueryMatchCriteriaType = Type.GetType( |
| 25 | + "Elastic.Internal.Search.Elasticsearch.RuleQueryMatchCriteria, Elastic.Internal.Search.Elasticsearch", |
| 26 | + throwOnError: true)!; |
| 27 | + |
| 28 | + public override bool CanConvert(Type typeToConvert) => typeToConvert == RuleQueryMatchCriteriaType; |
| 29 | + |
| 30 | + public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) => |
| 31 | + RuleQueryMatchCriteriaJsonConverter.Instance; |
| 32 | +} |
| 33 | + |
| 34 | +internal sealed class RuleQueryMatchCriteriaJsonConverter : JsonConverter<object> |
| 35 | +{ |
| 36 | + public static readonly RuleQueryMatchCriteriaJsonConverter Instance = new(); |
| 37 | + |
| 38 | + public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => |
| 39 | + throw new NotSupportedException(); |
| 40 | + |
| 41 | + public override void Write(Utf8JsonWriter writer, object? value, JsonSerializerOptions options) |
| 42 | + { |
| 43 | + if (value is null) |
| 44 | + { |
| 45 | + writer.WriteNullValue(); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + var queryString = RuleQueryMatchCriteriaAccessors.GetQueryString(value); |
| 50 | + writer.WriteStartObject(); |
| 51 | + writer.WriteString("query_string", queryString); |
| 52 | + writer.WriteEndObject(); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +internal static class RuleQueryMatchCriteriaAccessors |
| 57 | +{ |
| 58 | + [DynamicDependency( |
| 59 | + DynamicallyAccessedMemberTypes.PublicProperties, |
| 60 | + "Elastic.Internal.Search.Elasticsearch.RuleQueryMatchCriteria", |
| 61 | + "Elastic.Internal.Search.Elasticsearch")] |
| 62 | + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] |
| 63 | + private static readonly Type RuleQueryMatchCriteriaType = Type.GetType( |
| 64 | + "Elastic.Internal.Search.Elasticsearch.RuleQueryMatchCriteria, Elastic.Internal.Search.Elasticsearch", |
| 65 | + throwOnError: true)!; |
| 66 | + |
| 67 | + private static readonly PropertyInfo QueryStringProperty = RuleQueryMatchCriteriaType.GetProperty( |
| 68 | + "QueryString", |
| 69 | + BindingFlags.Public | BindingFlags.Instance)!; |
| 70 | + |
| 71 | + [UnconditionalSuppressMessage( |
| 72 | + "Trimming", |
| 73 | + "IL2075", |
| 74 | + Justification = "RuleQueryMatchCriteria.QueryString is preserved via DynamicDependency on RuleQueryMatchCriteriaType.")] |
| 75 | + public static string GetQueryString(object target) => (string)QueryStringProperty.GetValue(target)!; |
| 76 | +} |
0 commit comments