when I upgraded my ReactiveMongo library dependencies from 0.11.0.play24 to 0.11.14-play24 it throws me error - scala

I'am upgrading the play from 2.4.3 to 2.5.2 in the process I was able to upgrade the play but when I upgraded the reactive mongo library dependency from 0.11.0.play24 to 0.11.14-play24 it was throwing following errors
found : (String, play.api.libs.json.JsValue)
[error] required: reactivemongo.bson.Producer[reactivemongo.bson.BSONElement]
[error] (which expands to) reactivemongo.bson.Producer[(String, reactivemongo.bson.BSONValue)]
[error] Error occurred in an application involving default arguments.
[error] claimsCollection.update(BSONDocument("id" -> claimId), BSONDocument("$set" -> BSONDocument("status"-> "Approved" , "updationDate" -> Json.toJson(ZonedDateTime.now()))))
[error]
The code I have written like this
import com.greenroom6.auth.models.claims.Claims
import javax.inject.Inject
import play.modules.reactivemongo.ReactiveMongoApi
import play.modules.reactivemongo.json.collection.JSONCollection
import scala.concurrent.ExecutionContext
import com.greenroom6.auth.util.UniqueIdGenerator
import reactivemongo.bson.BSONDocument
import play.api.libs.json.{Json,JsObject}
import play.modules.reactivemongo.json._
import scala.concurrent.Future
import reactivemongo.api.commands.WriteResult
import reactivemongo.api.ReadPreference
import java.time.ZonedDateTime
import play.modules.reactivemongo.json._,ImplicitBSONHandlers._
import reactivemongo.bson._
import play.modules.reactivemongo.ReactiveMongoComponents
class ClaimsDaoImpl #Inject()(reactivemongo : ReactiveMongoApi) extends ClaimsDao {
protected def claimsCollection = reactivemongo.db.collection[JSONCollection]("claims")
def claimProfile(claim:Claims)(implicit ec :ExecutionContext): Future[WriteResult] = {
val claimTosave = claim.copy(id = Some(UniqueIdGenerator.generate), status = "Pending" )
claimsCollection.save(claimTosave)
}
def approveClaim(claimId:String)(implicit ec :ExecutionContext): Future[WriteResult] = {
claimsCollection.update(BSONDocument("id" -> claimId), BSONDocument("$set" -> BSONDocument("status"-> "Approved" , "updationDate" -> Json.toJson(ZonedDateTime.now()))))
}
}
What should I require to do to resolve the issue?

I think you have the wrong dependency. According to their Github Page: ReactiveMongo/Play-ReactiveMongo
val reactiveMongoVer = "0.11.14"
// only for Play 2.5.x
libraryDependencies ++= Seq(
"org.reactivemongo" %% "play2-reactivemongo" % reactiveMongoVer
)
Instead it seems you use this:
// only for Play 2.4.x
libraryDependencies ++= Seq(
"org.reactivemongo" %% "play2-reactivemongo" % s"$reactiveMongoVer-play24"
)

Related

Mapping a case class to a mongodb document using reactive mongo

Below is my simple document that represents a Link. I am using reactivemongo in scala for this.
I am getting this error during compilation:
app/components/Link.scala:60:11: No Json deserializer found for type
components.Link. Try to implement an implicit Reads or Format for this
type. [error] .one[Link]) [error] ^ [error] one error
found
I created the implicits in my Link companion object, which I also imported into my LinkRepo class.
Am I handling the mongo document _id correctly?
Should I use a String to map to the document ID, confused as to what is the best practise? Do I have to convert the String to the BSONObjectID at some point?
package components
import javax.inject.Inject
import reactivemongo.bson._
import reactivemongo.api.ReadPreference
import reactivemongo.api.collections.bson.BSONCollection
import reactivemongo.bson.{ BSONDocument, BSONObjectID }
import reactivemongo.api.commands.{ UpdateWriteResult, WriteResult, Upserted }
import reactivemongo.api.commands.bson.BSONUpdateCommand._
import reactivemongo.api.commands.bson.BSONUpdateCommandImplicits._
case class Link(id: Link.ID,
name: String,
url: String)
object Link {
type ID = String
implicit val linkReader: BSONDocumentReader[Link] =
BSONDocumentReader[Link] { doc: BSONDocument =>
Link(
doc.getAs[String]("id").getOrElse(""),
doc.getAs[String]("name").getOrElse(""),
doc.getAs[String]("url").getOrElse(""))
}
implicit val linkWriter: BSONDocumentWriter[Link] =
BSONDocumentWriter[Link] { link: Link =>
BSONDocument(
"id" -> link.id,
"name" -> link.name,
"url" -> link.url)
}
}
import scala.concurrent.{ ExecutionContext, Future }
import reactivemongo.bson.{ BSONDocument, BSONObjectID }
import reactivemongo.api.{ Cursor, ReadPreference }
import reactivemongo.api.commands.WriteResult
import reactivemongo.play.json._
import reactivemongo.play.json.collection.JSONCollection
import play.modules.reactivemongo.ReactiveMongoApi
class LinkRepo #Inject()(implicit ec: ExecutionContext, reactiveMongoApi: ReactiveMongoApi) {
import Link._
def linksCol: Future[JSONCollection] = reactiveMongoApi.database.map(_.collection("links"))
def byId(id: Link.ID): Future[Option[Link]] = {
linksCol.flatMap(_.find(
selector = BSONDocument("_id" -> id),
projection = Option.empty[BSONDocument])
.one[Link])
}
}
My sbt has these lib versions:
scalaVersion := "2.12.7"
libraryDependencies += guice
libraryDependencies ++= Seq(
guice,
"joda-time" % "joda-time" % "2.9.9",
"net.ruippeixotog" %% "scala-scraper" % "2.1.0",
"org.reactivemongo" %% "play2-reactivemongo" % "0.16.0-play26",
"org.scalatestplus.play" %% "scalatestplus-play" % "3.1.2" % Test
)
plugins:
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.20")
As far as I know, you do not NEED TO use BSONObjectID, but it would be recommended.
But one thins you should use "_id" field in mongo, otherwise the default behaviour will be to create an _id on your document when you create a new one... so your document will have _id and id.
So you should get id from "_id" and write it into "_id" even if your case class is id.
As per handlers, for basic case classes you can use their provided macros:
implicit val linkHandler: BSONDocumentHandler[Link] = Macros.handler
this will generate the Reader and Writer for your Link.
When using the macros, you have an annotations that you can use to highlight your id will be "_id" on mongo:
import reactivemongo.bson.Macros.Annotations.Key
case class Link(
#Key("_id")
id: Link.ID,
name: String,
url: String
)

methode map in class cats.data.Nested not recognized

I have a problem when folowing the scala cats library tutorial, the map method applied to the Nested class is highlighted with red, and the compiler doesn't recognize it.
here is my main class code :
import cats._
import cats.data._
import cats.implicits._
import cats.syntax.functor._
import cats.Functor
import cats.instances.list._
import cats.instances.option._
object Main extends App{
val list = List(Some(1), Some(2), None, Some(4))
val nested: Nested[List, Option, Int] = Nested(list)
//here is the problem
nested.map(_ + 1)
}
here is my build.sbt file
name := "dailySBT3"
version := "0.1"
scalaVersion := "2.12.5"
scalacOptions += "-Ypartial-unification"
libraryDependencies += "org.typelevel" %% "cats-core" % "1.1.0"
The problem is you are importing the instances and syntax twice. The following works for me with no problems:
import cats._
import cats.data._
import cats.implicits._
object Main extends App{
val list = List(Some(1), Some(2), None, Some(4))
val nested: Nested[List, Option, Int] = Nested(list)
nested.map(_ + 1)
}
You could also do the same thing as above but get rid of the cats.implicits._ import instead.
When in doubt, check out the cats import guide.

Circe Encoders and Decoders with Http4s

I am trying to use http4s, circe and http4s-circe.
Below I am trying to use the auto derivation feature of circe.
import org.http4s.client.blaze.SimpleHttp1Client
import org.http4s.Status.ResponseClass.Successful
import io.circe.syntax._
import org.http4s._
import org.http4s.headers._
import org.http4s.circe._
import scalaz.concurrent.Task
import io.circe._
final case class Login(username: String, password: String)
final case class Token(token: String)
object JsonHelpers {
import io.circe.generic.auto._
implicit val loginEntityEncoder : EntityEncoder[Login] = jsonEncoderOf[Login]
implicit val loginEntityDecoder : EntityDecoder[Login] = jsonOf[Login]
implicit val tokenEntityEncoder: EntityEncoder[Token] = jsonEncoderOf[Token]
implicit val tokenEntityDecoder : EntityDecoder[Token] = jsonOf[Token]
}
object Http4sTest2 extends App {
import JsonHelpers._
val url = "http://"
val uri = Uri.fromString(url).valueOr(throw _)
val list = List[Header](`Content-Type`(MediaType.`application/json`), `Accept`(MediaType.`application/json`))
val request = Request(uri = uri, method = Method.POST)
.withBody(Login("foo", "bar").asJson)
.map{r => r.replaceAllHeaders(list :_*)}.run
val client = SimpleHttp1Client()
val result = client.fetch[Option[Token]](request){
case Successful(response) => response.as[Token].map(Some(_))
case _ => Task(Option.empty[Token])
}.run
println(result)
}
I get multiple instances of these two compiler errors
Error:scalac: missing or invalid dependency detected while loading class file 'GenericInstances.class'.
Could not access type Secondary in object io.circe.Encoder,
because it (or its dependencies) are missing. Check your build definition for
missing or conflicting dependencies. (Re-run with `-Ylog-classpath` to see the problematic classpath.)
A full rebuild may help if 'GenericInstances.class' was compiled against an incompatible version of io.circe.Encoder.
Error:(25, 74) could not find implicit value for parameter encoder: io.circe.Encoder[Login]
implicit val loginEntityEncoder : EntityEncoder[Login] = jsonEncoderOf[Login]
I was able to solve this. I did a search on google on sbt circe dependency and I copy pasted the first search result. that was circe 0.1 and that is why things were not working for me.
I changed my dependencies to
libraryDependencies ++= Seq(
"org.http4s" %% "http4s-core" % http4sVersion,
"org.http4s" %% "http4s-dsl" % http4sVersion,
"org.http4s" %% "http4s-blaze-client" % http4sVersion,
"org.http4s" %% "http4s-circe" % http4sVersion,
"io.circe" %% "circe-core" % "0.7.0",
"io.circe" %% "circe-generic" % "0.7.0"
)
and now automatic derivation works fine and I am able to compile the code below
import org.http4s.client.blaze.SimpleHttp1Client
import org.http4s._
import org.http4s.headers._
import org.http4s.circe._
import scalaz.concurrent.Task
import io.circe.syntax._
import io.circe.generic.auto._
import org.http4s.Status.ResponseClass.Successful
case class Login(username: String, password: String)
case class Token(token: String)
object JsonHelpers {
implicit val loginEntityEncoder : EntityEncoder[Login] = jsonEncoderOf[Login]
implicit val loginEntityDecoder : EntityDecoder[Login] = jsonOf[Login]
implicit val tokenEntityEncoder: EntityEncoder[Token] = jsonEncoderOf[Token]
implicit val tokenEntityDecoder : EntityDecoder[Token] = jsonOf[Token]
}
object Http4sTest2 extends App {
import JsonHelpers._
val url = "http://"
val uri = Uri.fromString(url).valueOr(throw _)
val list = List[Header](`Content-Type`(MediaType.`application/json`), `Accept`(MediaType.`application/json`))
val request = Request(uri = uri, method = Method.POST)
.withBody(Login("foo", "bar").asJson)
.map{r => r.replaceAllHeaders(list :_*)}.run
val client = SimpleHttp1Client()
val result = client.fetch[Option[Token]](request){
case Successful(response) => response.as[Token].map(Some(_))
case _ => Task(Option.empty[Token])
}.run
println(result)
}

Failed assertion with automated initialisation of database in Cassandra

Completely new to Cassandra. Tried to initialize a database in Cassandra using phantom-dsl. I received this error message.
*** RUN ABORTED ***
java.lang.AssertionError: assertion failed: no symbol could be loaded from class com.datastax.driver.core.Cluster in package core with name Cluster and classloader sun.misc.Launcher$AppClassLoader#279f2327
at scala.reflect.runtime.JavaMirrors$JavaMirror.scala$reflect$runtime$JavaMirrors$JavaMirror$$classToScala1(JavaMirrors.scala:1021)
at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$classToScala$1.apply(JavaMirrors.scala:980)
at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$classToScala$1.apply(JavaMirrors.scala:980)
at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$toScala$1.apply(JavaMirrors.scala:97)
at scala.reflect.runtime.TwoWayCaches$TwoWayCache$$anonfun$toScala$1.apply(TwoWayCaches.scala:39)
at scala.reflect.runtime.Gil$class.gilSynchronized(Gil.scala:19)
at scala.reflect.runtime.JavaUniverse.gilSynchronized(JavaUniverse.scala:16)
at scala.reflect.runtime.TwoWayCaches$TwoWayCache.toScala(TwoWayCaches.scala:34)
at scala.reflect.runtime.JavaMirrors$JavaMirror.toScala(JavaMirrors.scala:95)
at scala.reflect.runtime.JavaMirrors$JavaMirror.classToScala(JavaMirrors.scala:980)
I am not really sure whether it is an issue with the Connector in phantom-dsl or the ClusterBuilder in datastax-driver.
Connector.scala
package com.neruti.db
import com.neruti.db.models._
import com.websudos.phantom.database.Database
import com.websudos.phantom.connectors.ContactPoints
import com.websudos.phantom.dsl.KeySpaceDef
object Connector {
val host= Seq("localhost")
val port = 9160
val keySpace: String = "nrt_entities"
// val inet = InetAddress.getByName
lazy val connector = ContactPoints(host,port).withClusterBuilder(
_.withCredentials("cassandra", "cassandra")
).keySpace(keySpace)
}
CassandraSpec.scala
package com.neruti.db
import com.neruti.User
import com.neruti.db.models._
import com.neruti.db.databases._
import com.neruti.db.services._
import com.neruti.db.Connector._
import java.util.UUID
import com.datastax.driver.core.ResultSet
import org.scalatest._
import org.scalatest.{BeforeAndAfterAll,FlatSpec,Matchers,ShouldMatchers}
import org.scalatest.concurrent.ScalaFutures
import org.scalamock.scalatest.MockFactory
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
abstract class BaseCassandraSpec extends FlatSpec
with BeforeAndAfterAll
with Inspectors
with Matchers
with OptionValues
with ScalaFutures
class CassandraTest extends BaseCassandraSpec
with ProductionDatabase
with UserService
with Connector.connector.Connector{
val user = User(
Some("foobar"),
Some("foo#foobar.com"),
Some(UUID.randomUUID()),
)
override protected def beforeAll(): Unit = {
Await.result(database.userModel.create(user),10.seconds)
}
}
Looks there may be multiple issues you are looking at:
The latest version of phantom is 2.1.3, I'd strongly recommend using that, especially if you are just starting out. The migration guide is here in case you need it.
The entire reflection mechanism has been replaced in the latest version, so that error should magically go away. With respect to testing and generating objects, I would also look to include com.outworkers.util.testing, which is freely available on Maven Central
libraryDependencies ++= Seq(
//..,
"com.outworkers" %% "phantom-dsl" % "2.1.3",
"com.outworkers" %% "util-testing" % "0.30.1" % Test
)
This will offer you automated case class generation:
import com.outworkers.util.testing._
val sample = gen[User]

Could not access type Unmarshaller in value akka.http.javadsl.unmarshalling

I am trying to write a simple Http client using Akka Http Client API. Towards this I have written the following code
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.unmarshalling._
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Sink, Source}
import scala.concurrent.duration._
import scala.concurrent.{Await}
import akka.http.scaladsl.server.Directives
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import spray.json._
final case class Post(postId: Int, id: Int, name: String, email: String, body: String)
trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
implicit val postFormat = jsonFormat5(Post.apply)
}
class AkkaHttpClient extends App with Directives with JsonSupport {
implicit val system = ActorSystem("my-Actor")
implicit val actorMaterializer = ActorMaterializer()
implicit val executionContext = system.dispatcher
val httpClient = Http().outgoingConnection(host="http://jsonplaceholder.typicode.com/")
val flow = Source.single(HttpRequest(uri = Uri("/comments/1")))
.via(httpClient)
.mapAsync(1)(r => Unmarshal(r.entity).to[Post])
.runWith(Sink.head)
val results = Await.result(flow, 15 seconds)
println(results)
}
My build.sbt file looks like
name := "Akka-Http-Client"
version := "1.0"
scalaVersion := "2.11.8"
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-http-experimental" % "2.4.9-RC1",
"com.typesafe.akka" %% "akka-http-spray-json-experimental" % "2.4.9-RC1"
)
When I try to compile my code I get these errors
Error:scalac: missing or invalid dependency detected while loading class file 'Unmarshaller.class'.
Could not access type Unmarshaller in value akka.http.javadsl.unmarshalling,
because it (or its dependencies) are missing. Check your build definition for
missing or conflicting dependencies. (Re-run with `-Ylog-classpath` to see the problematic classpath.)
A full rebuild may help if 'Unmarshaller.class' was compiled against an incompatible version of akka.http.javadsl.unmarshalling.
I am having the same problem on 2.4.9-RC1, falling back to 2.4.8 solves the problem.
OR you could use this workaround described here: https://github.com/akka/akka/issues/21105