Skip to content

Commit e5627da

Browse files
committed
Allow for intercepting methods in endpoint impl
1 parent b591452 commit e5627da

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

core/src/jsonrpclib/Endpoint.scala

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@ sealed trait Endpoint[F[_]] {
66

77
object Endpoint {
88

9-
def apply[F[_]](method: String): PartiallyAppliedEndpoint[F] = new PartiallyAppliedEndpoint[F](method)
9+
type MethodPattern = String
10+
type Method = String
1011

11-
class PartiallyAppliedEndpoint[F[_]](method: String) {
12+
def apply[F[_]](method: Method): PartiallyAppliedEndpoint[F] = new PartiallyAppliedEndpoint[F](method)
13+
14+
class PartiallyAppliedEndpoint[F[_]](method: MethodPattern) {
1215
def apply[In, Err, Out](
1316
run: In => F[Either[Err, Out]]
17+
)(implicit inCodec: Codec[In], errCodec: ErrorCodec[Err], outCodec: Codec[Out]): Endpoint[F] =
18+
RequestResponseEndpoint(method, (_: Method, in) => run(in), inCodec, errCodec, outCodec)
19+
20+
def full[In, Err, Out](
21+
run: (Method, In) => F[Either[Err, Out]]
1422
)(implicit inCodec: Codec[In], errCodec: ErrorCodec[Err], outCodec: Codec[Out]): Endpoint[F] =
1523
RequestResponseEndpoint(method, run, inCodec, errCodec, outCodec)
1624

@@ -25,16 +33,19 @@ object Endpoint {
2533
)
2634

2735
def notification[In](run: In => F[Unit])(implicit inCodec: Codec[In]): Endpoint[F] =
36+
NotificationEndpoint(method, (_: Method, in) => run(in), inCodec)
37+
38+
def notificationFull[In](run: (Method, In) => F[Unit])(implicit inCodec: Codec[In]): Endpoint[F] =
2839
NotificationEndpoint(method, run, inCodec)
2940

3041
}
3142

32-
final case class NotificationEndpoint[F[_], In](method: String, run: In => F[Unit], inCodec: Codec[In])
43+
final case class NotificationEndpoint[F[_], In](method: Method, run: (Method, In) => F[Unit], inCodec: Codec[In])
3344
extends Endpoint[F]
3445

3546
final case class RequestResponseEndpoint[F[_], In, Err, Out](
36-
method: String,
37-
run: In => F[Either[Err, Out]],
47+
method: Method,
48+
run: (Method, In) => F[Either[Err, Out]],
3849
inCodec: Codec[In],
3950
errCodec: ErrorCodec[Err],
4051
outCodec: Codec[Out]

core/src/jsonrpclib/internals/MessageDispatcher.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ private[jsonrpclib] abstract class MessageDispatcher[F[_]](implicit F: Monadic[F
7777
(input, endpoint) match {
7878
case (InputMessage.NotificationMessage(_, params), ep: NotificationEndpoint[F, in]) =>
7979
ep.inCodec.decode(params) match {
80-
case Right(value) => ep.run(value)
80+
case Right(value) => ep.run(input.method, value)
8181
case Left(value) => reportError(params, value, ep.method)
8282
}
8383
case (InputMessage.RequestMessage(_, callId, params), ep: RequestResponseEndpoint[F, in, err, out]) =>
8484
ep.inCodec.decode(params) match {
8585
case Right(value) =>
86-
doFlatMap(ep.run(value)) {
86+
doFlatMap(ep.run(input.method, value)) {
8787
case Right(data) =>
8888
val responseData = ep.outCodec.encode(data)
8989
sendMessage(OutputMessage.ResponseMessage(callId, responseData))

0 commit comments

Comments
 (0)