| | | 1 | | //======================================================================= |
| | | 2 | | // StateJsonConverterFactory.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Linq.Expressions; |
| | | 10 | | using System.Reflection; |
| | | 11 | | using System.Text.Json; |
| | | 12 | | using System.Text.Json.Serialization; |
| | | 13 | | |
| | | 14 | | namespace Chronicler.Serialization; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Creates JSON converters for types that explicitly implement <see cref="IStateBacked{TState}"/>. |
| | | 18 | | /// </summary> |
| | | 19 | | public sealed class StateJsonConverterFactory : JsonConverterFactory |
| | | 20 | | { |
| | | 21 | | /// <summary>Checks whether the type is a candidate for state-backed conversion.</summary> |
| | | 22 | | /// <param name="typeToConvert">The type to inspect. Null is ineligible.</param> |
| | | 23 | | /// <returns>True for a concrete class that implements a state-backed interface. Constructor and contract validation |
| | | 24 | | public override bool CanConvert(Type typeToConvert) |
| | | 25 | | { |
| | 19 | 26 | | return typeToConvert != null |
| | 19 | 27 | | && typeToConvert.IsClass |
| | 19 | 28 | | && !typeToConvert.IsAbstract |
| | 19 | 29 | | && HasStateBackedContract(typeToConvert); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary>Builds a state converter that invokes the record's state constructor.</summary> |
| | | 33 | | /// <param name="typeToConvert">A concrete class with exactly one state-backed interface and a public constructor ac |
| | | 34 | | /// <param name="options">The calling serializer's configuration. Constructor selection depends on the declared reco |
| | | 35 | | /// <returns>A converter whose constructor delegate is bound to the record's public state constructor.</returns> |
| | | 36 | | /// <remarks>Discovers the interface and constructor via reflection and compiles a constructor delegate.</remarks> |
| | | 37 | | /// <exception cref="InvalidOperationException">The type has zero or multiple state-backed interfaces, or lacks the |
| | | 38 | | public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) |
| | | 39 | | { |
| | 9 | 40 | | Type stateType = GetSingleStateType(typeToConvert); |
| | | 41 | | |
| | 7 | 42 | | ConstructorInfo constructor = typeToConvert.GetConstructor(new[] { stateType }) |
| | 7 | 43 | | ?? throw new InvalidOperationException( |
| | 7 | 44 | | $"Type '{typeToConvert}' must define a public constructor accepting '{stateType}'."); |
| | | 45 | | |
| | 6 | 46 | | Type converterType = typeof(StateJsonConverter<,>).MakeGenericType(typeToConvert, stateType); |
| | 6 | 47 | | Delegate factory = CreateFactory(typeToConvert, stateType, constructor); |
| | | 48 | | |
| | 6 | 49 | | return (JsonConverter)Activator.CreateInstance(converterType, factory)!; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | private static bool HasStateBackedContract(Type type) |
| | | 53 | | { |
| | 90 | 54 | | foreach (Type interfaceType in type.GetInterfaces()) |
| | | 55 | | { |
| | 34 | 56 | | if (IsStateBackedInterface(interfaceType)) |
| | 6 | 57 | | return true; |
| | | 58 | | } |
| | | 59 | | |
| | 8 | 60 | | return false; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | private static Type GetSingleStateType(Type type) |
| | | 64 | | { |
| | 9 | 65 | | Type? stateType = null; |
| | | 66 | | |
| | 35 | 67 | | foreach (Type interfaceType in type.GetInterfaces()) |
| | | 68 | | { |
| | 9 | 69 | | if (!IsStateBackedInterface(interfaceType)) |
| | | 70 | | continue; |
| | | 71 | | |
| | 9 | 72 | | Type currentStateType = interfaceType.GetGenericArguments()[0]; |
| | 9 | 73 | | if (stateType != null) |
| | | 74 | | { |
| | 1 | 75 | | throw new InvalidOperationException( |
| | 1 | 76 | | $"Type '{type}' must implement only one IStateBacked<TState> contract."); |
| | | 77 | | } |
| | | 78 | | |
| | 8 | 79 | | stateType = currentStateType; |
| | | 80 | | } |
| | | 81 | | |
| | 8 | 82 | | return stateType |
| | 8 | 83 | | ?? throw new InvalidOperationException( |
| | 8 | 84 | | $"Type '{type}' must implement IStateBacked<TState>."); |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | private static bool IsStateBackedInterface(Type type) |
| | | 88 | | { |
| | 43 | 89 | | return type.IsGenericType |
| | 43 | 90 | | && type.GetGenericTypeDefinition() == typeof(IStateBacked<>); |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | private static Delegate CreateFactory(Type recordType, Type stateType, ConstructorInfo constructor) |
| | | 94 | | { |
| | 6 | 95 | | ParameterExpression stateParameter = Expression.Parameter(stateType, "state"); |
| | 6 | 96 | | NewExpression createRecord = Expression.New(constructor, stateParameter); |
| | 6 | 97 | | Type factoryType = typeof(Func<,>).MakeGenericType(stateType, recordType); |
| | | 98 | | |
| | 6 | 99 | | return Expression.Lambda(factoryType, createRecord, stateParameter).Compile(); |
| | | 100 | | } |
| | | 101 | | } |