From d6f44fda1728520d216e1d49ad81f97ef062e07f Mon Sep 17 00:00:00 2001 From: LPeter1997 Date: Sat, 30 Sep 2023 21:13:22 +0200 Subject: [PATCH] Fixes --- .../IJsonRpcMethodHandlerAttribute.cs | 23 ++++++++++++++++ src/Draco.JsonRpc/JsonRpcConnection.cs | 8 ++++-- src/Draco.JsonRpc/JsonRpcMethodHandler.cs | 26 ++++++++++++++++--- 3 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 src/Draco.JsonRpc/IJsonRpcMethodHandlerAttribute.cs diff --git a/src/Draco.JsonRpc/IJsonRpcMethodHandlerAttribute.cs b/src/Draco.JsonRpc/IJsonRpcMethodHandlerAttribute.cs new file mode 100644 index 0000000000..96e71b7f44 --- /dev/null +++ b/src/Draco.JsonRpc/IJsonRpcMethodHandlerAttribute.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Draco.JsonRpc; + +/// +/// Implemented by attributes that mark JSON RPC method handles. +/// +public interface IJsonRpcMethodHandlerAttribute +{ + /// + /// The name of the JSON RPC method to handle. + /// + public string Method { get; } + + /// + /// True, if this method mutates the state. + /// + public bool Mutating { get; } +} diff --git a/src/Draco.JsonRpc/JsonRpcConnection.cs b/src/Draco.JsonRpc/JsonRpcConnection.cs index abc628384a..0db4503dde 100644 --- a/src/Draco.JsonRpc/JsonRpcConnection.cs +++ b/src/Draco.JsonRpc/JsonRpcConnection.cs @@ -48,11 +48,15 @@ public abstract class JsonRpcConnection /// /// Registers an RPC handler method. /// + /// The request attribute type. + /// The notification attribute type. /// The handler method info. /// The method invocation target. - public void AddRpcMethod(MethodInfo handlerMethod, object? target) + public void AddRpcMethod(MethodInfo handlerMethod, object? target) + where TRequest : Attribute, IJsonRpcMethodHandlerAttribute + where TNotification : Attribute, IJsonRpcMethodHandlerAttribute { - var handler = new JsonRpcMethodHandler(handlerMethod, target); + var handler = JsonRpcMethodHandler.Create(handlerMethod, target); this.methodHandlers.Add(handler.MethodName, handler); } diff --git a/src/Draco.JsonRpc/JsonRpcMethodHandler.cs b/src/Draco.JsonRpc/JsonRpcMethodHandler.cs index 95390b7b01..effad7ebcd 100644 --- a/src/Draco.JsonRpc/JsonRpcMethodHandler.cs +++ b/src/Draco.JsonRpc/JsonRpcMethodHandler.cs @@ -14,6 +14,23 @@ namespace Draco.JsonRpc; /// internal sealed class JsonRpcMethodHandler { + /// + /// Constructs a new . + /// + /// The request attribute type. + /// The notification attribute type. + /// The handler method. + /// The invocation target. + /// The constructed handler object. + public static JsonRpcMethodHandler Create(MethodInfo handlerMethod, object? target) + where TRequest : Attribute, IJsonRpcMethodHandlerAttribute + where TNotification : Attribute, IJsonRpcMethodHandlerAttribute + { + var requestAttr = handlerMethod.GetCustomAttribute(); + var notificationAttr = handlerMethod.GetCustomAttribute(); + return new(handlerMethod, target, requestAttr, notificationAttr); + } + /// /// The handler . /// @@ -60,7 +77,11 @@ internal sealed class JsonRpcMethodHandler /// public Type DeclaredReturnType { get; } - public JsonRpcMethodHandler(MethodInfo handlerMethod, object? target) + private JsonRpcMethodHandler( + MethodInfo handlerMethod, + object? target, + IJsonRpcMethodHandlerAttribute? requestAttr, + IJsonRpcMethodHandlerAttribute? notificationAttr) { this.HandlerMethod = handlerMethod; this.Target = target; @@ -104,9 +125,6 @@ public JsonRpcMethodHandler(MethodInfo handlerMethod, object? target) } // Verify attributes - var requestAttr = handlerMethod.GetCustomAttribute(); - var notificationAttr = handlerMethod.GetCustomAttribute(); - if (requestAttr is null && notificationAttr is null) { throw new ArgumentException($"Handler must be marked as either a request or notification handler.", nameof(handlerMethod));