final class encoded extends Annotation with RpcEncoding

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._

case class Json(jsonStr: String)
object Json {
  private def readJson[T: GenCodec](json: Json): T =
    JsonStringInput.read[T](json.jsonStr)

  private def writeJson[T: GenCodec](value: T): Json =
    Json(JsonStringOutput.write[T](value))

  implicit def 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`
  implicit def 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:

trait AsyncRawRpc {
  def call(@methodName rpcName: String, @multi args: Map[String,String]): Future[String]
}
object AsyncRawRpc extends RawRpcCompanion[AsyncRawRpc] {
  private def readJson[T: GenCodec](json: String): T =
    JsonStringInput.read[T](json)
  private def writeJson[T: GenCodec](value: T): String =
    JsonStringOutput.write[T](value)

  override object implicits {
    implicit def genCodecBasedJsonEncoding[T: GenCodec]: AsRawReal[String,T] =
      AsRawReal.create[String,T](writeJson[T], readJson[T])
    implicit def genCodecBasedFutureJsonEncoding[T: GenCodec]: AsRawReal[Future[String],Future[T]] =
      AsRawReal.create[Future[String],Future[T]](_.mapNow(writeJson[T]), _.mapNow(readJson[T]))
  }
}
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. encoded
  2. RpcEncoding
  3. RawParamAnnotation
  4. RawMethodAnnotation
  5. RawSymAnnotation
  6. StaticAnnotation
  7. Annotation
  8. AnyRef
  9. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new encoded()