about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/Extensions
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-26 02:43:54 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-27 17:43:00 +0200
commit3ed00f732a284b5a3e96e52d4e3a71869135869b (patch)
tree308cdd5c9891a676dc55cbf0e0e998ab5a74b2d2 /MatrixRoomUtils.Core/Extensions
parentWorking state, refactored Rory&::LibMatrix (diff)
downloadMatrixUtils-3ed00f732a284b5a3e96e52d4e3a71869135869b.tar.xz
Dependency injection stuff
Diffstat (limited to 'MatrixRoomUtils.Core/Extensions')
-rw-r--r--MatrixRoomUtils.Core/Extensions/IEnumerableExtensions.cs31
-rw-r--r--MatrixRoomUtils.Core/Extensions/JsonElementExtensions.cs8
2 files changed, 8 insertions, 31 deletions
diff --git a/MatrixRoomUtils.Core/Extensions/IEnumerableExtensions.cs b/MatrixRoomUtils.Core/Extensions/IEnumerableExtensions.cs

index 98b0aab..8994529 100644 --- a/MatrixRoomUtils.Core/Extensions/IEnumerableExtensions.cs +++ b/MatrixRoomUtils.Core/Extensions/IEnumerableExtensions.cs
@@ -1,36 +1,9 @@ -using System.Reflection; -using System.Text.Json; using MatrixRoomUtils.Core.Interfaces; -using MatrixRoomUtils.Core.Responses; namespace MatrixRoomUtils.Core.Extensions; -public static class IEnumerableExtensions { - public static List<StateEventResponse> DeserializeMatrixTypes(this List<JsonElement> stateEvents) { - return stateEvents.Select(DeserializeMatrixType).ToList(); - } - - public static StateEventResponse DeserializeMatrixType(this JsonElement stateEvent) { - var type = stateEvent.GetProperty("type").GetString(); - var knownType = StateEvent.KnownStateEventTypes.FirstOrDefault(x => x.GetCustomAttribute<MatrixEventAttribute>()?.EventName == type); - if (knownType == null) { - Console.WriteLine($"Warning: unknown event type '{type}'!"); - return new StateEventResponse(); - } - - var eventInstance = Activator.CreateInstance(typeof(StateEventResponse).MakeGenericType(knownType))!; - stateEvent.Deserialize(eventInstance.GetType()); - - return (StateEventResponse) eventInstance; - } - - public static void Replace(this List<StateEvent> stateEvents, StateEvent old, StateEvent @new) { - var index = stateEvents.IndexOf(old); - if (index == -1) return; - stateEvents[index] = @new; - } -} - +[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class MatrixEventAttribute : Attribute { public string EventName { get; set; } + public bool Legacy { get; set; } } \ No newline at end of file diff --git a/MatrixRoomUtils.Core/Extensions/JsonElementExtensions.cs b/MatrixRoomUtils.Core/Extensions/JsonElementExtensions.cs
index 78f4456..36da644 100644 --- a/MatrixRoomUtils.Core/Extensions/JsonElementExtensions.cs +++ b/MatrixRoomUtils.Core/Extensions/JsonElementExtensions.cs
@@ -7,7 +7,7 @@ using System.Text.Json.Serialization; namespace MatrixRoomUtils.Core.Extensions; public static class JsonElementExtensions { - public static void FindExtraJsonElementFields([DisallowNull] this JsonElement? res, Type t) { + public static bool FindExtraJsonElementFields([DisallowNull] this JsonElement? res, Type t) { var props = t.GetProperties(); var unknownPropertyFound = false; foreach (var field in res.Value.EnumerateObject()) { @@ -17,8 +17,10 @@ public static class JsonElementExtensions { } if (unknownPropertyFound) Console.WriteLine(res.Value.ToJson()); + + return unknownPropertyFound; } - public static void FindExtraJsonObjectFields([DisallowNull] this JsonObject? res, Type t) { + public static bool FindExtraJsonObjectFields([DisallowNull] this JsonObject? res, Type t) { var props = t.GetProperties(); var unknownPropertyFound = false; foreach (var field in res) { @@ -31,5 +33,7 @@ public static class JsonElementExtensions { } if (unknownPropertyFound) Console.WriteLine(res.ToJson()); + + return unknownPropertyFound; } } \ No newline at end of file