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()

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )
  6. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  7. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  8. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  9. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  10. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  11. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  12. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  13. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  14. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  15. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  16. def toString(): String
    Definition Classes
    AnyRef → Any
  17. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  18. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  19. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )

Inherited from RpcEncoding

Inherited from RawParamAnnotation

Inherited from RawMethodAnnotation

Inherited from RawSymAnnotation

Inherited from StaticAnnotation

Inherited from Annotation

Inherited from AnyRef

Inherited from Any

Ungrouped