I am using Scala 3 and http4s 1.0.0-M35. I want to use auth with json handling.
val routes = AuthedRoutes.of[User, IO] {
case request#POST -> Root / "dialog" / LongVar(dialogId) / "send" as user => {
for {
sendMessageRequest <- request.as[SendMessageRequest]
But got error
[error] | sendMessageRequest <- request.as[SendMessageRequest]
[error] | ^^^^^^^^^^
[error] |value as is not a member of org.http4s.ContextRequest[cats.effect.IO, repository.User], but could be made available as an extension method.
It suggests some imports:
[error] |One of the following imports might fix the problem:
[error] |
[error] | import cats.Functor.nonInheritedOps.toFunctorOps
[error] | import cats.Functor.ops.toAllFunctorOps
[error] | import cats.NonEmptyTraverse.ops.toAllNonEmptyTraverseOps
[error] | import cats.Traverse.ops.toAllTraverseOps
[error] | import cats.implicits.toFunctorOps
[error] | import cats.syntax.all.toFunctorOps
[error] | import cats.syntax.functor.toFunctorOps
But after importing it, I have another error:
sendMessageRequest type is org.http4s.ContextRequest[cats.effect.IO, service.CreateDialogRequest]
How to use json handling in AuthedRoutes it?
I solved my problem. Just:
request.req.as
Related
I am trying to write some tests for my code in scala
the problem is everytime i try to run jacoco with sbt I am getting all kinds of errors.
I am using VSCode if that has anything to do with it
This is a code snippet of the test
package model
import org.scalatest.Wordspec
import org.scalatest.matchers.should.Matchers._
import model._
class fieldSpec extends WordSpec with Matchers {
"A Field" when {
"not set any value" should {
val emptyField = Field(0)
"have value 0" in {
emptyField.value should be(0)
}
This is my build.sbt
val scala3Version = "3.1.1"
lazy val root = project
.in(file("."))
.settings(
name := "MADN",
version := "0.1.0-SNAPSHOT",
scalaVersion := scala3Version,
libraryDependencies += "org.scalameta" %% "munit" % "0.7.29" % Test,
libraryDependencies += "org.scalactic" %% "scalactic" % "3.2.11",
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.11" % "test"
)
When I am trying to run sbt jacoco I get this error message as an output
[error] -- [E006] Not Found Error: C:\Software-Engineering\MADN\src\test\scala\model\fieldSpec.scala:7:24
[error] 7 |class fieldSpec extends WordSpec with Matchers {
[error] | ^^^^^^^^
[error] | Not found: type WordSpec
[error] -- [E006] Not Found Error: C:\Software-Engineering\MADN\src\test\scala\model\fieldSpec.scala:7:38
[error] 7 |class fieldSpec extends WordSpec with Matchers {
[error] | ^^^^^^^^
[error] | Not found: type Matchers
[error] -- [E008] Not Found Error: C:\Software-Engineering\MADN\src\test\scala\model\fieldSpec.scala:9:12
[error] 9 | "A Field" when {
[error] | ^^^^^^^^^^^^^^
[error] |value when is not a member of String - did you mean ("A Field" : String).wait?
[error] -- [E006] Not Found Error: C:\Software-Engineering\MADN\src\test\scala\model\fieldSpec.scala:11:23
[error] 11 | val emptyField = Field(0)
[error] | ^^^^^
[error] | Not found: Field
[error] -- [E008] Not Found Error: C:\Software-Engineering\MADN\src\test\scala\model\fieldSpec.scala:12:21
[error] 12 | "have value 0" in {
[error] | ^^^^^^^^^^^^^^^^^
[error] | value in is not a member of String
[error] -- [E008] Not Found Error: C:\Software-Engineering\MADN\src\test\scala\model\fieldSpec.scala:15:19
[error] 15 | "not be set" in {
[error] | ^^^^^^^^^^^^^^^
[error] | value in is not a member of String
[error] -- [E006] Not Found Error: C:\Software-Engineering\MADN\src\test\scala\model\fieldSpec.scala:20:26
[error] 20 | val nonEmptyField = Field(2)
[error] | ^^^^^
[error] | Not found: Field
[error] -- [E008] Not Found Error: C:\Software-Engineering\MADN\src\test\scala\model\fieldSpec.scala:21:26
[error] 21 | "return that value" in {
[error] | ^^^^^^^^^^^^^^^^^^^^^^
[error] | value in is not a member of String
[error] -- [E008] Not Found Error: C:\Software-Engineering\MADN\src\test\scala\model\fieldSpec.scala:24:15
[error] 24 | "be set" in {
[error] | ^^^^^^^^^^^
[error] | value in is not a member of String
[error] 9 errors found
[error] 9 errors found
[error] (Test / compileIncremental) Compilation failed
[error] Total time: 7 s, completed 06.04.2022 14:29:46
I also included the plugin at project/plugins.sbt
addSbtPlugin("com.github.sbt" % "sbt-jacoco" % "3.4.0")
My tests are in the following directory:
src>test>scala>model>fieldSpec.scala
So I was able to fix it by using AnyWordSpec instead of just WordSpec
and also by deleting the following line out of my build.sbt
libraryDependencies += "org.scalameta" %% "munit" % "0.7.29" % Test,
I am creating a custom inputKey which looks like
val rating = inputKey[Option[Int]]("How will you rate this course?")
rating := {
import complete.DefaultParsers._
import complete.Parser
val r: Parser[Int] = IntBasic.examples("<rating>")
r.result
}
This sits in file projectRoot/build.sbt.
I try to run this on sbt shell using multiple times, each time it fails
sbt:Hello> rating 1
[error] Expected whitespace character
[error] Expected '/'
[error] rating 1
[error] ^
sbt:Hello>
Then,
sbt:Hello> show "rating 3"
[error] Expected whitespace character
[error] Expected 'Global'
[error] Expected '*'
[error] Expected 'Zero'
[error] Expected 'ThisBuild'
[error] Expected 'ProjectRef('
[error] Expected '{'
[error] Expected project ID
[error] Expected configuration
[error] Expected configuration ident
[error] Expected key
[error] show "rating 3"
[error] ^
sbt:Hello>
Also, as
sbt:Hello> rating "5"
[error] Expected whitespace character
[error] Expected '/'
[error] rating "5"
[error] ^
sbt:Hello>
I do not know what I am missing here. Can someone please point out my mistake here?
Since there is a space character before the integer, try using Space ~> IntBasic parser combination like so
lazy val rating = inputKey[Int]("How will you rate this course?")
rating := {
import complete.DefaultParsers._
val rating = (Space ~> IntBasic).examples("<rating>").parsed
println(s"Rating input = $rating")
rating
}
Executing rating 3 in sbt should now output Rating input = 3
I am trying to import a module from another project and did as following:
As you can see on the image, the imported library is /home/developer/...kafka-api.
I am using the importing library in testing.
When I compile my spec files with statement test:compile and I've got following error:
[IJ]sbt:auth_stream> test:compile
[info] Compiling 3 Scala sources to /home/developer/Desktop/microservices/bary/auth-stream/target/scala-2.12/test-classes ...
[error] /home/developer/Desktop/microservices/bary/auth-stream/src/test/scala/io/khinkali/auth/AppSpec.scala:14:20: object kkapi is not a member of package io.khinkali
[error] import io.khinkali.kkapi.consumer.{KkConsumer, KkConsumerConfig, KkConsumerCreator}
[error] ^
[error] /home/developer/Desktop/microservices/bary/auth-stream/src/test/scala/io/khinkali/auth/AppSpec.scala:15:20: object kkapi is not a member of package io.khinkali
[error] import io.khinkali.kkapi.producer.{KkProducer, KkProducerCreator, MaxBlockMsConfig}
[error] ^
[error] /home/developer/Desktop/microservices/bary/auth-stream/src/test/scala/io/khinkali/auth/AppSpec.scala:24:56: not found: value KkConsumer
[error] private val consumer: IO[Consumer[String, String]] = KkConsumer.create(createConsumer())
[error] ^
[error] /home/developer/Desktop/microservices/bary/auth-stream/src/test/scala/io/khinkali/auth/AppSpec.scala:52:5: not found: type KkConsumerCreator
[error] : KkConsumerCreator
[error] ^
[error] /home/developer/Desktop/microservices/bary/auth-stream/src/test/scala/io/khinkali/auth/AppSpec.scala:25:56: not found: value KkProducer
[error] private val producer: IO[Producer[String, String]] = KkProducer.create(createProducer())
[error] ^
[error] /home/developer/Desktop/microservices/bary/auth-stream/src/test/scala/io/khinkali/auth/AppSpec.scala:46:5: not found: type KkProducerCreator
[error] : KkProducerCreator
[error] ^
[error] /home/developer/Desktop/microservices/bary/auth-stream/src/test/scala/io/khinkali/auth/AppSpec.scala:47:5: not found: value KkProducerCreator
[error] = KkProducerCreator(sys.env.get("KAFKA_SERVER").get,
[error] ^
[error] /home/developer/Desktop/microservices/bary/auth-stream/src/test/scala/io/khinkali/auth/AppSpec.scala:49:10: not found: value MaxBlockMsConfig
[error] List(MaxBlockMsConfig(2000)))
[error] ^
[error] /home/developer/Desktop/microservices/bary/auth-stream/src/test/scala/io/khinkali/auth/AppSpec.scala:53:5: not found: value KkConsumerCreator
[error] = KkConsumerCreator(sys.env.get("KAFKA_SERVER").get,
[error] ^
[error] /home/developer/Desktop/microservices/bary/auth-stream/src/test/scala/io/khinkali/auth/AppSpec.scala:57:16: not found: type KkConsumerConfig
[error] List.empty[KkConsumerConfig])
[error] ^
[error] 10 errors found
[error] (test:compileIncremental) Compilation failed
What am I doing wrong?
Hint, that the package of both project starts with the name, namely:
The current project:
The imported project:
As you can see, the name differs only at the end. Could it be the problem?
What I am trying to approach is, to use a function for kafka-api project.
I'm trying to use chisel 3.
I tried to test GCD.scala file in the chisel project template repo using sbt test and sbt "test-only example.GCD" commands following the answer to a previous question. But this gives an error(s) that I cannot find the reason for. I didn't do any changes to the build.sbt file or repo layout. I'm posting only the last part of the error message since it is very long and repetitive.
[info] Loading project definition from /home/isuru/fyp/ChiselProjects/TrialProject/project
[info] Set current project to chisel-module-template (in build file:/home/isuru/fyp/ChiselProjects/TrialProject/)
[info] Compiling 1 Scala source to /home/isuru/fyp/ChiselProjects/TrialProject/target/scala-2.11/classes...
[error] /home/isuru/fyp/ChiselProjects/TrialProject/src/main/scala/example/GCD.scala:5: not found: object Chisel3
[error] import Chisel3._
[error] ^
[error] /home/isuru/fyp/ChiselProjects/TrialProject/src/main/scala/example/GCD.scala:7: not found: type Module
[error] class GCD extends Module {
[error] ^
[error] /home/isuru/fyp/ChiselProjects/TrialProject/src/main/scala/example/GCD.scala:8: not found: type Bundle
[error] val io = new Bundle {
[error] ^
[error] /home/isuru/fyp/ChiselProjects/TrialProject/src/main/scala/example/GCD.scala:9: not found: value UInt
[error] val a = UInt(INPUT, 16)
[error] ^
[error] /home/isuru/fyp/ChiselProjects/TrialProject/src/main/scala/example/GCD.scala:9: not found: value INPUT
[error] val a = UInt(INPUT, 16)
[error] ^
[error] /home/isuru/fyp/ChiselProjects/TrialProject/src/main/scala/example/GCD.scala:10: not found: value UInt
[error] val b = UInt(INPUT, 16)
[error] ^
[error] /home/isuru/fyp/ChiselProjects/TrialProject/src/main/scala/example/GCD.scala:10: not found: value INPUT
[error] val b = UInt(INPUT, 16)
[error] ^
[error] /home/isuru/fyp/ChiselProjects/TrialProject/src/main/scala/example/GCD.scala:11: not found: value Bool
[error] val e = Bool(INPUT)
[error] ^
[error] /home/isuru/fyp/ChiselProjects/TrialProject/src/main/scala/example/GCD.scala:11: not found: value INPUT
[error] val e = Bool(INPUT)
[error] ^
[error] /home/isuru/fyp/ChiselProjects/TrialProject/src/main/scala/example/GCD.scala:12: not found: value UInt
[error] val z = UInt(OUTPUT, 16)
[error] ^
[error] /home/isuru/fyp/ChiselProjects/TrialProject/src/main/scala/example/GCD.scala:12: not found: value OUTPUT
[error] val z = UInt(OUTPUT, 16)
[error] ^
[error] /home/isuru/fyp/ChiselProjects/TrialProject/src/main/scala/example/GCD.scala:13: not found: value Bool
[error] val v = Bool(OUTPUT)
[error] ^
[error] /home/isuru/fyp/ChiselProjects/TrialProject/src/main/scala/example/GCD.scala:13: not found: value OUTPUT
[error] val v = Bool(OUTPUT)
[error] ^
[error] /home/isuru/fyp/ChiselProjects/TrialProject/src/main/scala/example/GCD.scala:15: not found: value Reg
[error] val x = Reg(UInt())
[error] ^
[error] /home/isuru/fyp/ChiselProjects/TrialProject/src/main/scala/example/GCD.scala:15: not found: value UInt
[error] val x = Reg(UInt())
[error] ^
[error] /home/isuru/fyp/ChiselProjects/TrialProject/src/main/scala/example/GCD.scala:16: not found: value Reg
[error] val y = Reg(UInt())
[error] ^
[error] /home/isuru/fyp/ChiselProjects/TrialProject/src/main/scala/example/GCD.scala:16: not found: value UInt
[error] val y = Reg(UInt())
[error] ^
[error] /home/isuru/fyp/ChiselProjects/TrialProject/src/main/scala/example/GCD.scala:17: not found: value when
[error] when (x > y) { x := x - y }
[error] ^
[error] /home/isuru/fyp/ChiselProjects/TrialProject/src/main/scala/example/GCD.scala:18: not found: value unless
[error] unless (x > y) { y := y - x }
[error] ^
[error] /home/isuru/fyp/ChiselProjects/TrialProject/src/main/scala/example/GCD.scala:19: not found: value when
[error] when (io.e) { x := io.a; y := io.b }
[error] ^
[error] 20 errors found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 2 s, completed Dec 1, 2016 8:26:25 PM
The errors you have shown suggest that sbt is somehow not finding Chisel, could you by chance show the full list of errors (especially early on ones)? With the following sequence of commands I am unable to reproduce the errors you are seeing:
git clone git#github.com:ucb-bar/chisel-template.git
cd chisel-template
sbt test
It is not the cause of this issue, but to run the test in chisel-template you should actually run sbt "test-only examples.test.GCDTester". example.GCD is the top of the design, but to run the test you have to refer to the Tester class in src/test/scala/examples/test/GCDUnitTest.scala.
I just encountered this same problem when making my own chisel project. However it was not that the import chisel3._ was wrong. The problem I had was I did not have a build.sbt file included in my directory.
I found my solution here.
https://chisel.eecs.berkeley.edu/2.0.6/getting-started.html
Could someone please help. I have Play2 project in which I need to test some DAO code.
I used documentaion from http://www.playframework.org/documentation/2.0.2/ScalaTest.
The test is very simple:
import models.Calendar
import org.specs2.mutable._
import play.api.test._
import play.api.test.Helpers._
class CalendarSpec extends Specification {
"Calendar model" should {
"be retrieved by id" in {
val fakeApp = FakeApplication()
running(fakeApp) {
lazy val calendarId= Calendar.addCalendar(
Calendar(subject="test",
upAccount = "mytest",
masterId = 1,
calendarType = 1,
isAllDayEvent = false,
hasAttachment = false,
category = "test",
instanceType = 1,
upName = "test" ))
lazy val Some(calendar) = Calendar.getCalendar(calendarId.get)
calendar.upAccount must equalTo("mytest")
}
}
}
}
And when I run 'sbt test' I get strange error:
[info] Calendar model should
[error] ! Fragment evaluation error
[error] ThrowableException: play.api.test.Helpers$.play$api$http$HeaderNames$_setter_$ACCESS_CONTROL_ALLOW_ORIGIN_$eq(Ljava/lang/String;)V (TraversableLike.scala:194)
[error] play.api.http.HeaderNames$class.$init$(StandardValues.scala:195)
[error] play.api.test.Helpers$.<init>(Helpers.scala:16)
[error] play.api.test.Helpers$.<clinit>(Helpers.scala:111)
[error] CalendarSpec$$anonfun$1$$anonfun$apply$1.apply(CalendarSpec.scala:13)
[error] CalendarSpec$$anonfun$1$$anonfun$apply$1.apply(CalendarSpec.scala:10)
[error] play.api.test.Helpers$.play$api$http$HeaderNames$_setter_$ACCESS_CONTROL_ALLOW_ORIGIN_$eq(Ljava/lang/String;)V
[error] play.api.http.HeaderNames$class.$init$(StandardValues.scala:195)
[error] play.api.test.Helpers$.<init>(Helpers.scala:16)
[error] play.api.test.Helpers$.<clinit>(Helpers.scala:111)
[error] CalendarSpec$$anonfun$1$$anonfun$apply$1.apply(CalendarSpec.scala:13)
[error] CalendarSpec$$anonfun$1$$anonfun$apply$1.apply(CalendarSpec.scala:10)
StackOverflow/Google knows nothing about this exception. Thanks in advance.
The stacktrace makes me think that a library is incorrect or missing in your classpath. This is why you are seeing "Helpers$." traces where the class constructor seems to be failing.
You can validate this by writing a small app in your test directory, without specs2 but using Play2's helper classes and see what happens.
I found solution - https://groups.google.com/forum/#!msg/play-framework/NSN9xfktUks/EwiG1Cc0C9oJ:
new play.core.StaticApplication(new java.io.File(".")) should be added to actualy start Play app so DAO calls can work in test.