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])) } }
- Alphabetic
- By Inheritance
- encoded
- RpcEncoding
- RawParamAnnotation
- RawMethodAnnotation
- RawSymAnnotation
- StaticAnnotation
- Annotation
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Instance Constructors
- new encoded()
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )