lift error, method not found - scala

I am calling this snippet
package org.palantir.snippet
import net.liftweb._
import util._
import Helpers._
import scala.xml._
object Kitchens
{
def render(html: NodeSeq) : NodeSeq = <p>test</p>
}
With this xml
<lift:Kitchens>
</lift:Kitchens>
And getting this error:
Error processing snippet: kitchens
Reason: Method Not Found
What am I missing?

This occurs because the HTML5 parser Lift uses converts all tag and attribute names to lowercase. It is recommended to use either: class="lift:Snippet" or data-lift="Snippet" to avoid that. This was not an issue with the older XHTML parser as that was case sensitive, so you will find some older documentation that invokes snippets as a tag.
You can find more information here.

Related

Scala object Action in package mvc is deprecated

I'm currently working on a new project and face a warning I haven't seen in older projects yet.
The warning says that Action from the mvc package is deprecated since 2.6.0. I guess it has something to do with Play or Scala itself. So this is my code responsible for the warning:
def getLists: Action[AnyContent] = Action.async {
listRepo.getLists.map(lists => Ok(Json.obj("lists" -> lists)))
}
My buildt.sbt looks like this:
And this is how I import the Action object:
import play.api.mvc.{Action, AnyContent, Controller}
Is there an equivalent alternative or am I doing something wrong?
The warning message tells you what to do (more information is in the linked sections of the Play 2.6 migration guide):
Inject an ActionBuilder (e.g. DefaultActionBuilder) or
extend BaseController/AbstractController/InjectedController

Using any() to stubbing before calling .thenReturn failing

My first question in StackOverflow. Tried to search for answer, but I could not find any related to my problem.
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatra.test.scalatest.ScalatraFunSuite
import org.mockito.Mockito.when
import org.mockito.Matchers.any
#RunWith(classOf[JUnitRunner])
#Category(Array(classOf[DatabaseTest]))
class Test extends ScalatraFunSuite {
test("Test") {
when(pre.findProduct(any[Seq[Sone]]))
.thenReturn(Util.createProduct())
when(ft.findProduct(any()))
.thenReturn(Util.createFT())
when(interval.findSones(any()))
.thenReturn(Util.createInterval())
//The rest is code to utilise the above
}
I have the test code above written in Scala and using Mockito. Here is the error I get when I run it:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced argument matcher detected here:
-> at org.Test.$anonfun$new$1(Test.scala:23)
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains("foo"))
Also, this error might show up because you use argument matchers with
methods that cannot be mocked. Following methods cannot be
stubbed/verified: final/private/equals()/hashCode(). Mocking methods
declared on non-public parent classes is not supported.
That is, both ft.findProduct(any()) and interval.findSones(any()) get the same error. A temporarily fixed has been to use a fixed integer, that is:
test("Test") {
when(pre.findProduct(any[Seq[Sone]]))
.thenReturn(Util.createProduct())
when(ft.findProduct(1)) //any() => 1
.thenReturn(Util.createFT())
when(interval.findSones(3)) //any() => 3
.thenReturn(Util.createInterval())
//The rest is code to utilise the above
}
NOTE: Also tried to use anyInt(), which gave the same error.
My question is: Is there anything wrong with my code? Hope I was able to clarify my problem.

Scala Lift JSON error that is not fixed with normal solution

I have the following Scala project on GitHub. In that repo I have a class, Configurator, whose job it is to read a JSON file's contents into a string, and then use the Lift JSON library to deserialize the string into an instance of an AppConfig:
import scala.io.Source
import net.liftweb.json._
class Configurator {
def loadConfigs(configFileUri : String) : AppConfig = {
implicit val formats = net.liftweb.json.DefaultFormats
parse(Source.fromFile(configFileUri).mkString).extract[AppConfig]
}
}
If you clone this and then run ./gradlew run you'll get the following exception:
/Users/myuser/intellij-scala-gradle-example/shared/src/main/scala/com/me/myapp/Configurator.scala:9: could not find implicit value for parameter formats: net.liftweb.json.Formats
parse(Source.fromFile(configFileUri).mkString).extract[AppConfig]
If you Google that exception you'll see 10,000 recommendations for that implicit format fix which I implemented right here. But that did not work for me. So I'm wondering:
Why am I seeing this exception?
What's the fix?
Switched from Lift JSON to GSON and all my problems are gone.

TypeError: Cannot read property 'freeze' of undefined

I am trying to create a facade for the npm library avsc. When I compile with sbt fastOptJS::webpack and open the .html file, I get Uncaught TypeError: Cannot read property 'freeze' of undefined from the file treepad-fastopt-bundle.js in the line $g["Object"]["freeze"]($env);. I don't use Object.freeze it anywhere.
This is the facade code:
import buffer.Buffer
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSImport, JSName}
#js.native
trait Type extends js.Object {
#JSName("val")
def toBuffer(v: String): Buffer = js.native
}
#JSImport("avsc/", "avro")
#js.native
object avro extends avro
#js.native
trait avro extends js.Object {
def parse(schema: js.Any): Type = js.native
}
Also have a look at the whole project, it's very little code.
Using #JSImport("avsc", JSImport.Namespace) instead did not change anything.
The problem comes from the fact that, in your webpack configuration file, you tell webpack to target the Node.js execution environment instead of web browsers.
However, as you noticed, the avsc module uses the fs Node.js module, which is not available in web browsers. It seems that the right workaround, in this case, is to add the following line to your webpack configuration file:
module.exports.node = { fs: "empty" };
Last but not least, the right #JSImport is indeed #JSImport("avsc", JSImport.Namespace) because you want to import the whole avsc module, as shown in the documentation.

Play exception: '{' expected but 'import' found

It throws me the exception for line 3. The thing is i have just these lines:
package controllers
import play.api._
import play.api.mvc._
import views._
import models._
object Application extends Controller {
def index = Ok(views.html.index("grrr", "blabla"))
}
EDIT: index.scala.html
#import helper._
#main("Todo") {
<h1>Hello World</h1>
}
I'm using play 2.2.0 on windows xp (with sbt)
I think that problem is with yours line separator in IDE. I once change LF(Linux) to CR(Mac)(by mistake, not knowing that this has an impact on compilation) and struggle with the same problem. After changing to default sperator everything back to normal.
The first line in Play! template is reserved for the signature definition. This is also mentioned in the Welcome screen when you create a new Play Application.
Beside the question why you import the helper._, I would do the following:
Make the first line empty, or at least NO import statements.
Run play clean
After this it should work, I hope :-)
Further information:
Play framework template automatically imports models._ among other things
https://github.com/playframework/playframework/blob/1acfd1dc4264e7589876fb1f4ebf37e584ab8bc6/framework/src/play/src/main/scala/views/play20/welcome.scala.html#L81
If you know a bit scala (I don't yet): https://github.com/playframework/playframework/blob/9206bea8c9c88acdc6786ebb2554f081396e8f6a/framework/src/templates-compiler/src/main/scala/play/templates/ScalaTemplateCompiler.scala
EDIT: 2013.09.24 at 22:15
You are passing two arguments to your view template (views.html.index("grrr", "blabla")), (views are compiled to functions). So in your function (`index view') the first line SHOULD define the function signature (arguments). I think that you should write your template as:
#(firstString : String, secondString : String)
#import helper._
#main("Todo") {
<h1>Hello World</h1>
}