lihaoyi / autowire

Macros for simple/safe RPCs between Scala applications, including ScalaJS/ScalaJVM
378 stars 48 forks source link

Can not assign server.route to a val and use that #73

Open doofin opened 6 years ago

doofin commented 6 years ago

Type parameters for client here worked

  case class objParam(url:String)
  def getAtw[t](): ClientProxy[t, ByteBuffer, Default.Pickler, Default.Pickler] ={ // ok
    val a=autowireJs
    a.atwParams=objParam(sharedOlogx.rpcAuthed_ologx_path)
    a[t]
  }
  object autowireJs2 extends autowire.Client[ByteBuffer, Pickler, Pickler]   {
    override def doCall(req: Request): Future[ByteBuffer] = {
      dom.ext.Ajax
        .post(
          url = sharedOlogx.rpcAuthed_ologx_path + "/" + req.path.mkString("/"),
          data = Pickle.intoBytes(req.args),
          responseType = "arraybuffer",
          headers = Map(("Content-Type", "application/octet-stream"))
        )
        .map(r => TypedArrayBuffer.wrap(r.response.asInstanceOf[ArrayBuffer]))
    }

but the server side do not work(it compiles,but get runtime error when client tries to do rpc call)

      object autowireServer extends autowire.Server[ByteBuffer, Pickler, Pickler]{
        override def read[R: Pickler](p: ByteBuffer) = Unpickle[R].fromBytes(p)

        override def write[R: Pickler](r: R) = Pickle.intoBytes(r)

        @inline
        final def run[t](apiImpl: t,  // damn! this can not be made generic
                   reqPathList: List[String],
                   reqBodyBytes: ByteString): Future[ByteBuffer] = {
          lg("atw reqPathList:"+reqPathList)
          this.route[t](apiImpl)( // the problem is route[t] 
            autowire.Core.Request(
              reqPathList,
              read[Map[String, ByteBuffer]](reqBodyBytes.asByteBuffer))
          )
        }

      }

even if i inline it

the error is

scala.MatchError: Request(List(rpcAuthedOlogx, tst),Map()) (of class autowire.Core$Request) at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:254) at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:252) at jvmUtils$akkaHttpUtil$rpc_server$autowireServerCls$$anonfun$run$1.applyOrElse(jvmUtils.scala:246)