scala - spray marshalling cats.data.Xor -


i writing rest api using spray , having difficulties json marshalling. service returns cats.data.xor[failure, success]. how can return data type rest endpoint? how write response marshaller this?

the simplest solution call toeither on value in router, lets spray-provided either marshaller take over.

another solution provide own marshaller (i've done couple of times myself):

import cats.data.xor import spray.httpx.marshalling.toresponsemarshaller  implicit def xormarshaller[a, b](implicit   ma: toresponsemarshaller[a],   mb: toresponsemarshaller[b] ): toresponsemarshaller[xor[a, b]] =   toresponsemarshaller[xor[a, b]] { (value, ctx) =>     value match {       case xor.left(a)  => ma(a, ctx)       case xor.right(b) => mb(b, ctx)     }   } 

this lets avoid both runtime cost (probably negligible) , syntactic cost (less negligible) of converting.

note cats removing xor in favor of standard library's either in upcoming versions, though, going toeither may practical solution.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -