scala - Finch: not enough arguments for method 'toService' -


i've made pretty simple rest-method using finch , finagle:

val getusers:endpoint[list[user]] = get("users") {ok(getallusers())} http.serve(":8080", getusers.toservice) 

and got error:

error:(50, 32) not enough arguments method toservice: (implicit ts: io.finch.internal.toservice[list[dal.instances.user.user]])com.twitter.finagle.service[com.twitter.finagle.http.request,com.twitter.finagle.http.response]. unspecified value parameter ts.   http.serve(":8080", getusers.toservice)                                ^ 

any idea on how fix it?

the compiler error little better in recent version of finch (0.10). if have following build config:

scalaversion := "2.11.7"  librarydependencies += "com.github.finagle" %% "finch-core" % "0.10.0" 

and setup:

import com.twitter.finagle.http import io.finch._  case class user(id: string, name: string)  def getallusers(): list[user] = list(user("111", "foo mcbar")) val getusers: endpoint[list[user]] = get("users") { ok(getallusers()) } 

then when try use toservice, following:

<console>:18: error: can convert router finagle service if result type of router 1 of following:    * response   * value of type encoderesponse instance   * coproduct made of combination of above  list[user] not satisfy requirement. may need provide encoderesponse instance list[user] (or  part of list[user]).         http.serve(":8080", getusers.toservice)                                     ^ 

the problem haven't told finch how translate instances of user type http responses. finch's encoderesponse example of type class, approach polymorphism that's used in scala (including standard library) , many other statically-typed functional programming languages.

the easiest way provide appropriate encoderesponse instances add finch's circe compatibility module build:

librarydependencies ++= seq(   "io.circe" %% "circe-generic" % "0.3.0",   "com.github.finagle" %% "finch-circe" % "0.10.0" ) 

and need following imports:

import io.finch.circe._, io.circe.generic.auto._ 

and toservice work fine:

scala> http.serve(":8080", getusers.toservice) feb 26, 2016 8:32:24 com.twitter.finagle.init$$anonfun$1 apply$mcv$sp info: finagle version 6.33.0 (rev=21d0ee8b5070b735eda5c84d7aa6fbf1ba7b1635) built @ 20160203-202859 res2: com.twitter.finagle.listeningserver = group(/0:0:0:0:0:0:0:0:8080) 

now if go http://localhost:8080/users, you'll see following:

[{"id":"111","name":"foo mcbar"}] 

this looks magic, what's happening principled. circe json library provides generic codec derivation figures out @ compile-time how represent case classes json values (see blog post here more context).

the finch cookbook great resource learning more questions this, , first section goes detail other ways provide encoderresponse instances toservice needs. if have other questions or if of above isn't clear, feel free ask either here or on gitter.


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -