11package jsonrpclib
22
3- import io .circe .Codec
3+ import jsonrpclib .ErrorCodec .errorPayloadCodec
4+ import io .circe .Decoder
5+ import io .circe .Encoder
46
57sealed trait Endpoint [F [_]] {
68 def method : String
9+
10+ def mapK [G [_]](f : PolyFunction [F , G ]): Endpoint [G ]
711}
812
913object Endpoint {
@@ -16,44 +20,51 @@ object Endpoint {
1620 class PartiallyAppliedEndpoint [F [_]](method : MethodPattern ) {
1721 def apply [In , Err , Out ](
1822 run : In => F [Either [Err , Out ]]
19- )(implicit inCodec : Codec [In ], errCodec : ErrorCodec [Err ], outCodec : Codec [Out ]): Endpoint [F ] =
20- RequestResponseEndpoint (method, (_ : InputMessage , in : In ) => run(in), inCodec, errCodec , outCodec)
23+ )(implicit inCodec : Decoder [In ], errEncoder : ErrorEncoder [Err ], outCodec : Encoder [Out ]): Endpoint [F ] =
24+ RequestResponseEndpoint (method, (_ : InputMessage , in : In ) => run(in), inCodec, errEncoder , outCodec)
2125
2226 def full [In , Err , Out ](
2327 run : (InputMessage , In ) => F [Either [Err , Out ]]
24- )(implicit inCodec : Codec [In ], errCodec : ErrorCodec [Err ], outCodec : Codec [Out ]): Endpoint [F ] =
25- RequestResponseEndpoint (method, run, inCodec, errCodec , outCodec)
28+ )(implicit inCodec : Decoder [In ], errEncoder : ErrorEncoder [Err ], outCodec : Encoder [Out ]): Endpoint [F ] =
29+ RequestResponseEndpoint (method, run, inCodec, errEncoder , outCodec)
2630
2731 def simple [In , Out ](
2832 run : In => F [Out ]
29- )(implicit F : Monadic [F ], inCodec : Codec [In ], outCodec : Codec [Out ]) =
33+ )(implicit F : Monadic [F ], inCodec : Decoder [In ], outCodec : Encoder [Out ]) =
3034 apply[In , ErrorPayload , Out ](in =>
3135 F .doFlatMap(F .doAttempt(run(in))) {
3236 case Left (error) => F .doPure(Left (ErrorPayload (0 , error.getMessage(), None )))
3337 case Right (value) => F .doPure(Right (value))
3438 }
3539 )
3640
37- def notification [In ](run : In => F [Unit ])(implicit inCodec : Codec [In ]): Endpoint [F ] =
41+ def notification [In ](run : In => F [Unit ])(implicit inCodec : Decoder [In ]): Endpoint [F ] =
3842 NotificationEndpoint (method, (_ : InputMessage , in : In ) => run(in), inCodec)
3943
40- def notificationFull [In ](run : (InputMessage , In ) => F [Unit ])(implicit inCodec : Codec [In ]): Endpoint [F ] =
44+ def notificationFull [In ](run : (InputMessage , In ) => F [Unit ])(implicit inCodec : Decoder [In ]): Endpoint [F ] =
4145 NotificationEndpoint (method, run, inCodec)
4246
4347 }
4448
45- final case class NotificationEndpoint [F [_], In ](
49+ private [jsonrpclib] final case class NotificationEndpoint [F [_], In ](
4650 method : MethodPattern ,
4751 run : (InputMessage , In ) => F [Unit ],
48- inCodec : Codec [In ]
49- ) extends Endpoint [F ]
52+ inCodec : Decoder [In ]
53+ ) extends Endpoint [F ] {
54+
55+ def mapK [G [_]](f : PolyFunction [F , G ]): Endpoint [G ] =
56+ NotificationEndpoint [G , In ](method, (msg, in) => f(run(msg, in)), inCodec)
57+ }
5058
51- final case class RequestResponseEndpoint [F [_], In , Err , Out ](
59+ private [jsonrpclib] final case class RequestResponseEndpoint [F [_], In , Err , Out ](
5260 method : Method ,
5361 run : (InputMessage , In ) => F [Either [Err , Out ]],
54- inCodec : Codec [In ],
55- errCodec : ErrorCodec [Err ],
56- outCodec : Codec [Out ]
57- ) extends Endpoint [F ]
62+ inCodec : Decoder [In ],
63+ errEncoder : ErrorEncoder [Err ],
64+ outCodec : Encoder [Out ]
65+ ) extends Endpoint [F ] {
5866
67+ def mapK [G [_]](f : PolyFunction [F , G ]): Endpoint [G ] =
68+ RequestResponseEndpoint [G , In , Err , Out ](method, (msg, in) => f(run(msg, in)), inCodec, errEncoder, outCodec)
69+ }
5970}
0 commit comments