Scala 3 + http4s problem with encode/decode json - scala

I have a Scala 3 project (3.0.0 version) and I'm trying to build simple Rest API with http4s.
I have a problem with decoding/encoding JSON.
I'm building my code based on http4s.g8.
The issue occurs on this line:
implicit val jokeDecoder: Decoder[Joke] = deriveDecoder[Joke]
Compile error:
no implicit argument of type deriving.Mirror.Of[com.example.quickstart.Jokes.Joke] was found for parameter A of method deriveDecoder in object semiauto
Is there some change in Scala 3 which makes it different?
My dependencies
scalaVersion := "3.0.0"
val Http4sVersion = "0.23.6"
val CirceVersion = "0.14.1"
libraryDependencies ++= Seq(
"org.http4s" %% "http4s-blaze-server" % Http4sVersion,
"org.http4s" %% "http4s-blaze-client" % Http4sVersion,
"org.http4s" %% "http4s-circe" % Http4sVersion,
"org.http4s" %% "http4s-dsl" % Http4sVersion,
"io.circe" %% "circe-core" % CirceVersion,
"io.circe" %% "circe-generic" % CirceVersion
)

final case class Joke(joke: String) extends AnyVal
is the cuprit. Rewrite it as
final case class Joke(joke: String)
it should work

Related

Can not resolve symbol Arbitrary

I am trying to implement Arbitrary for my type as follow:
import cats._
import org.scalacheck.{Arbitrary, Gen}
object Server {
sealed trait ServerHealth
case object ServerOnline extends ServerHealth
case object ServerOffline extends ServerHealth
implicit val healthSemigroup: Semigroup[ServerHealth] = (x: ServerHealth, y: ServerHealth) => x match {
case ServerOnline if y == ServerOnline => ServerOnline
case ServerOffline => ServerOffline
}
implicit val healthEq: Eq[ServerHealth] = (x: ServerHealth, y: ServerHealth) => x match {
case ServerOnline if y == ServerOnline => true
case ServerOffline => false
}
implicit def healthArbitrary[A: Arbitrary]: Arbitrary[ServerHealth] =
Arbitrary(Gen.oneOf(Gen.const(ServerOffline), for {
e <- Arbitrary.arbitrary[A]
} yield (ServerOffline, ServerOnline)))
}
and the compiler complains:
object scalacheck is not a member of package org
[error] import org.scalacheck.{Arbitrary, Gen}
I using intellj and the import is marked as red:
I also checked the build.sbt file:
val Http4sVersion = "0.21.0-M4"
val CirceVersion = "0.12.1"
val Specs2Version = "4.7.0"
val LogbackVersion = "1.2.3"
val ScalaTestVersion = "3.0.8"
val TestContainerVersion = "1.11.3"
val KafkaTestContainerVersion = "1.11.3"
val ConfigVersion = "1.3.4"
val SpringVersion = "5.1.8.RELEASE"
val catsVersion = "2.0.0"
lazy val settings = Seq(
organization := "com.sweetsoft",
name := "connector",
scalaVersion := "2.13.0",
addCompilerPlugin("org.typelevel" %% "kind-projector" % "0.10.3"),
addCompilerPlugin("com.olegpy" %% "better-monadic-for" % "0.3.0"),
scalacOptions ++= Seq(
"-deprecation",
"-encoding", "UTF-8",
"-language:higherKinds",
"-language:postfixOps",
"-feature",
"-Xfatal-warnings",
),
scalacOptions in(Compile, console) ~= {
_.filterNot(Set("-Xlint"))
}
)
lazy val dependencies = Seq(
"org.http4s" %% "http4s-blaze-server" % Http4sVersion,
"org.http4s" %% "http4s-blaze-client" % Http4sVersion,
"org.http4s" %% "http4s-circe" % Http4sVersion,
"org.http4s" %% "http4s-dsl" % Http4sVersion,
"io.circe" %% "circe-generic" % CirceVersion,
"ch.qos.logback" % "logback-classic" % LogbackVersion,
"org.typelevel" %% "cats-core" % catsVersion,
"com.typesafe" % "config" % ConfigVersion % "test",
"org.scalactic" %% "scalactic" % ScalaTestVersion % "test",
"org.scalatest" %% "scalatest" % ScalaTestVersion % "test",
"org.testcontainers" % "testcontainers" % TestContainerVersion % "test",
"org.testcontainers" % "kafka" % KafkaTestContainerVersion % "test",
"org.springframework" % "spring-core" % SpringVersion % "test",
"org.typelevel" %% "cats-laws" % catsVersion % "test",
"com.github.alexarchambault" %% "scalacheck-shapeless_1.14" % "1.2.3" % "test",
"org.scalacheck" %% "scalacheck" % "1.14.0" % "test"
)
lazy val global = project
.in(file("."))
.settings(settings)
.aggregate(core, serversupervisor)
lazy val core = (project in file("core"))
.settings(
settings,
libraryDependencies ++= dependencies
)
lazy val serversupervisor = (project in file("serversupervisor"))
.settings(
settings,
libraryDependencies ++= dependencies
)
.dependsOn(core)
and I should be correct. What is missing?
Update
The implementation is in Server.scala marked on the image:
the serversupervisor project it depends on the core project, because there are some common libs.
You use this in the Compile scope (i.e. sources in src/main), so you need to remove % "test" from the ScalaCheck dependency. Or move that source to the Test scope (i.e. in src/test)

`java.lang.NoSuchMethodError: cats.FlatMap.map2` in runtime when using `.sequence`

I'm getting the following runtime error after migrating from cats v1.1.0 to v1.4.0 (An error arises from places where .sequence (cats.Traverse) is used).
The code looks like:
import cats.implicits._
import cats.effect.IO
List(1, 2, 3).map(x => IO(...)).sequence
java.lang.NoSuchMethodError: cats.FlatMap.map2$(Lcats/FlatMap;Ljava/lang/Object;Ljava/lang/Object;Lscala/Function2;)Ljava/lang/Object;
at cats.effect.IOLowPriorityInstances$IOEffect.map2(IO.scala:765)
...
Here is my build.sbt:
organization := "org.xxx"
name := "yyy"
version := "0.0.1"
scalaVersion := "2.12.9"
resolvers += Resolver.bintrayRepo("hseeberger", "maven")
resolvers ++= Seq("Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/")
lazy val doobieVersion = "0.5.3"
lazy val akkaHttpVersion = "10.1.1"
lazy val akkaVersion = "2.5.12"
lazy val catsVersion = "1.4.0"
lazy val circeVersion = "0.9.3"
lazy val doobieDeps = Seq(
"org.tpolecat" %% "doobie-core" % doobieVersion,
"org.tpolecat" %% "doobie-postgres" % doobieVersion,
"org.tpolecat" %% "doobie-scalatest" % doobieVersion,
"org.tpolecat" %% "doobie-hikari" % doobieVersion
)
lazy val catsDeps = Seq(
"org.typelevel" %% "cats-effect" % catsVersion,
"org.typelevel" %% "cats-core" % catsVersion
)
lazy val otherDeps = Seq(
"com.github.pureconfig" %% "pureconfig" % "0.9.1",
"org.scorexfoundation" %% "scrypto" % "2.1.1",
"de.heikoseeberger" %% "akka-http-circe" % "1.20.1",
"org.scalaj" %% "scalaj-http" % "2.4.0",
"org.flywaydb" % "flyway-core" % "5.1.1",
"com.github.blemale" %% "scaffeine" % "2.5.0",
("org.scorexfoundation" %% "sigma-state" % "master-2b4b07a1-SNAPSHOT")
.exclude("ch.qos.logback", "logback-classic")
.exclude("org.scorexfoundation", "scrypto"),
)
lazy val circeDeps = Seq(
"io.circe" %% "circe-core" % circeVersion,
"io.circe" %% "circe-parser" % circeVersion,
"io.circe" %% "circe-generic" % circeVersion
)
libraryDependencies ++= (otherDeps ++ doobieDeps ++ catsDeps ++ loggingDeps ++ akkaDeps ++ circeDeps ++ testDeps)
I've tried to run it different ways (idea, set run) and on different platforms - the result is always the same.
What could it be caused by?
Try to change versions to
"org.typelevel" %% "cats-effect" % "1.4.0",
"org.typelevel" %% "cats-core" % "1.6.1"
Your project seems to work with them.

Cannot import classes from one module to other one - Scala

I have created a project with 3 different modules. First one is called http and second algebra. I have connected them into one in sbt file, but when I want to use classes from algebra in http then I cannot import them because they do not see each other.
This is my sbt file:
lazy val commonSettings = Seq(
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-core" % CatsVersion,
"org.typelevel" %% "cats-effect" % "1.2.0",
"org.typelevel" %% "cats-tagless-macros" % "0.2.0",
"org.typelevel" %% "cats-mtl-core" % "0.5.0",
)
)
lazy val root = project.in(file(".")).aggregate(http, domain, algebra)
.settings(commonSettings)
.settings(libraryDependencies ++= Seq(
"org.tpolecat" %% "doobie-core" % DoobieVersion,
"org.tpolecat" %% "doobie-h2" % DoobieVersion,
"org.tpolecat" %% "doobie-scalatest" % DoobieVersion,
"org.tpolecat" %% "doobie-hikari" % DoobieVersion,
))
lazy val http = (project in file("http"))
.dependsOn(algebra)
.settings(commonSettings)
.settings(
name := "my-http",
libraryDependencies ++= Seq(
"io.circe" %% "circe-generic" % CirceVersion,
"io.circe" %% "circe-literal" % CirceVersion,
"io.circe" %% "circe-generic-extras" % CirceVersion,
"io.circe" %% "circe-parser" % CirceVersion,
"io.circe" %% "circe-java8" % CirceVersion,
"io.circe" %% "circe-config" % CirceConfigVersion,
"org.http4s" %% "http4s-blaze-server" % Http4sVersion,
"org.http4s" %% "http4s-circe" % Http4sVersion,
"org.http4s" %% "http4s-dsl" % Http4sVersion,
))
lazy val domain = project.in(file("domain"))
lazy val algebra = (project in file("algebra"))
.settings(commonSettings)
.settings(
name := "my-algebra",
)
I tried to refresh all projects but it did not work.
class MyRoutes[F[_]: Effect](services: MyService[F]) extends Http4sDsl[F]{...}
Class MyRoutes is in http module and MyService in algebra module. The error is Cannot find declaration to go to on MyService.
How can I fix it?
Ok, I solved this problem. It was my, stupid mistake. I have not marked directory as source root where is MyService. Because of this, in http module I could not see this class.

Unable to create spark-warehouse directory using spark-2.3.0

I want to create a project with akka and spark. I added dependencies and some other dependencies too. Is these dependencies will cause any effect on using spark.
I have below sbt file
dependencyOverrides += "com.fasterxml.jackson.core" % "jackson-core" % "2.8.7"
dependencyOverrides += "com.fasterxml.jackson.core" % "jackson-databind" % "2.8.7"
dependencyOverrides += "com.fasterxml.jackson.module" % "jackson-module-scala_2.11" % "2.8.7"
lazy val commonSettings = Seq(
organization := "com.bitool.analytics",
scalaVersion := "2.11.12",
libraryDependencies ++= Seq(
"org.scala-lang.modules" %% "scala-async" % "0.9.6",
"com.softwaremill.macwire" %% "macros" % "2.3.0",
"com.softwaremill.macwire" %% "macrosakka" % "2.3.0",
"com.typesafe.akka" %% "akka-http" % "10.0.6",
"io.swagger" % "swagger-jaxrs" % "1.5.19",
"com.github.swagger-akka-http" %% "swagger-akka-http" % "0.9.1",
"io.circe" %% "circe-generic" % "0.8.0",
"io.circe" %% "circe-literal" % "0.8.0",
"io.circe" %% "circe-parser" % "0.8.0",
"io.circe" %% "circe-optics" % "0.8.0",
"org.scalafx" %% "scalafx" % "8.0.144-R12",
"org.scalafx" %% "scalafxml-core-sfx8" % "0.4",
"org.apache.spark" %% "spark-core" % "2.3.0",
"org.apache.spark" %% "spark-sql" % "2.3.0",
"org.apache.spark" %% "spark-hive" % "2.3.0",
"org.scala-lang" % "scala-xml" % "2.11.0-M4",
"mysql" % "mysql-connector-java" % "6.0.5"
)
)
lazy val root = (project in file(".")).
settings(commonSettings: _*).
settings(
name := "BITOOL-1.0"
)
ivyScala := ivyScala.value map {
_.copy(overrideScalaVersion = true)
}
fork in run := true
and below is my spark code
private val warehouseLocation = new File("spark-warehouse").getAbsolutePath
val conf = new SparkConf()
conf.setMaster("local[4]")
conf.setAppName("Bitool")
conf.set("spark.sql.warehouse.dir", warehouseLocation)
val SPARK = SparkSession
.builder().config(conf).enableHiveSupport()
.getOrCreate()
val SPARK_CONTEXT = SPARK.sparkContext
When I trying to execute this, It is creating metastore_db folder but spark-warehouse folder is not creating.
This directory is not created by getOrCreate. You can check it in the Spark source code: getOrCreate delegates its actions to SparkSession.getOrCreate, which is just a setter. All the internal tests and CliSuite use a snippet like this to prematurely initialize the dir: val warehousePath = Utils.createTempDir()
Instead, in the actual user code, you have to perform at least one data modification operation to materialize your warehouse directory. Try running something like that just after your code and check warehouse directory on the hard drive again:
import SPARK.implicits._
import SPARK.sql
sql("DROP TABLE IF EXISTS test")
sql("CREATE TABLE IF NOT EXISTS test (key INT, value STRING) USING hive")

Salat Error: class file needed by SalatDAO is missing. reference type MongoCollection of com.mongodb.casbah.TypeImports refers to nonexisting symbol

I'm getting the following error when I try to extend SalatDAO or use grater[T].asObject(x):
class file needed by SalatDAO is missing. reference type MongoCollection of
com.mongodb.casbah.TypeImports refers to nonexisting symbol.
I've followed the Salat examples but, for some reason, extending SalatDAO and graters asObject do not work for me. I cannot find any reference to this error online.
Here's my code:
import net.trevor.model.DBConnection._
import com.novus.salat._
import com.novus.salat.global._
import com.mongodb.casbah.Imports._
import com.novus.salat.dao.SalatDAO
//error occurs on following line:
object HandleDAO extends SalatDAO[Handle, ObjectId](DBConnection.db("Handles")){
def getHandleAsDBObject(handle : Handle) : DBObject =
grater[Handle].asDBObject(handle)
def getHandleFromDBObject(dbObject : DBObject) : Handle =
//error occurs on following line:
grater[Handle].asObject(dbObject)
}
I'd really appreciate any help or advice on this. I'm new to Scala and Mongodb.
I'm compiling using sbt compile. Here's my build.sbt
name := "handle_engine"
version := "1.0"
scalaVersion := "2.9.1"
scalacOptions += "-deprecation"
fork in run := true
resolvers ++= Seq(
"twitter-repo" at "http://maven.twttr.com",
"repo.novus rels" at "http://repo.novus.com/releases/",
"repo.novus snaps" at "http://repo.novus.com/snapshots/",
"Java.net Maven2 Repository" at "http://download.java.net/maven/2/"
)
libraryDependencies ++= {
val liftVersion = "2.4-M5" // Put the current/latest lift version here
Seq(
"net.liftweb" %% "lift-webkit" % liftVersion % "compile->default",
"net.liftweb" %% "lift-mapper" % liftVersion % "compile->default",
"net.liftweb" %% "lift-amqp" % liftVersion % "compile->default",
"net.liftweb" %% "lift-mongodb" % liftVersion % "compile->default",
"net.liftweb" %% "lift-mongodb-record" % liftVersion % "compile->default",
"net.liftweb" %% "lift-wizard" % liftVersion % "compile->default")
}
libraryDependencies ++= Seq(
"org.eclipse.jetty" % "jetty-server" % "8.1.0.RC5", // % "compile,jetty",
"org.eclipse.jetty" % "jetty-servlet" % "8.1.0.RC5", // % "compile,jetty",
"org.mongodb" % "mongo-java-driver" % "compile->default",
"com.rabbitmq" % "amqp-client" % "compile->default",
"org.mongodb" % "casbah_2.9.0-1" % "3.0.0-M2",
"com.novus" % "salat-core_2.8.1" % "0.0.7", //Salat for MongoDB and Casbah
"org.apache.avro" % "avro" % "1.6.2",
"com.twitter" % "util-core_2.9.1" % "1.12.8", "com.twitter" % "util-eval_2.9.1" % "1.12.8",
"junit" % "junit" % "4.5" % "test->default",
"javax.servlet" % "servlet-api" % "2.5" % "provided->default",
"ch.qos.logback" % "logback-classic" % "0.9.26" % "compile->default"
)
seq(webSettings :_*)
libraryDependencies += "org.mortbay.jetty" % "jetty" % "6.1.26" % "test,container"
libraryDependencies += "org.scala-tools.testing" %% "specs" % "1.6.9" % "test"
It looks like you might be on an older version of Salat. Try changing you version to following:
"com.novus" %% "salat-core" % "0.0.8-SNAPSHOT"
or
"com.novus" % "salat-core_2.9.1" % "0.0.8-SNAPSHOT"