When a raw parameter is annotated as encoded, macro generated code will translate
between real parameter values and raw parameter values using implicit instances of AsRaw[Raw,Real]
and AsReal[Raw,Real] typeclasses. This annotation may also be applied on a method, but this would be
redundant since method results are encoded by default.
Here's an example of raw RPC definition supposed to handle asynchronous (Future-based) calls that uses
GenCodec in order to encode and decode arguments and results as JSON strings.
It introduces its own wrapper class for JSON strings that has appropriate implicit instances of
AsRaw and AsReal (or AsRawReal which serves as both AsReal and AsRaw).
import com.avsystem.commons._
import com.avsystem.commons.rpc._
import com.avsystem.commons.serialization._
import com.avsystem.commons.serialization.json._
caseclass Json(jsonStr: String)
object Json {
privatedef readJson[T: GenCodec](json: Json): T =
JsonStringInput.read[T](json.jsonStr)
privatedef writeJson[T: GenCodec](value: T): Json =
Json(JsonStringOutput.write[T](value))
implicitdef genCodecBasedJsonEncoding[T: GenCodec]: AsRawReal[T,Json] =
AsRawReal.create[Json,T](writeJson[T], readJson[T])
// instead of using `mapNow`, this method can also take implicit ExecutionContext and just use `map`implicitdef genCodecBasedFutureJsonEncoding[T: GenCodec]: AsRawReal[Future[Json],Future[T]] =
AsRawReal.create[Future[Json],Future[T]](_.mapNow(writeJson[T]), _.mapNow(readJson[T]))
}
trait AsyncRawRpc {
def call(@methodName rpcName: String, @multi args: Map[String,Json]): Future[Json]
}
If you don't want to introduce the wrapper Json class and use more raw type, e.g. plain String then you
can also do it by moving implicit instances of AsReal and AsRaw (or the joined AsRawReal) into the
implicits object of raw RPC companion:
When a raw parameter is annotated as encoded, macro generated code will translate between real parameter values and raw parameter values using implicit instances of
AsRaw[Raw,Real]
andAsReal[Raw,Real]
typeclasses. This annotation may also be applied on a method, but this would be redundant since method results are encoded by default.Here's an example of raw RPC definition supposed to handle asynchronous (
Future
-based) calls that usesGenCodec
in order to encode and decode arguments and results as JSON strings. It introduces its own wrapper class for JSON strings that has appropriate implicit instances ofAsRaw
andAsReal
(orAsRawReal
which serves as bothAsReal
andAsRaw
).If you don't want to introduce the wrapper
Json
class and use more raw type, e.g. plainString
then you can also do it by moving implicit instances ofAsReal
andAsRaw
(or the joinedAsRawReal
) into theimplicits
object of raw RPC companion: