Scala + Play: how to pass a parameter into a route in a template? - scala

I'm tinkering with Scala and Play as a bit of a weekend learning exercise, and one thing that has got me a bit stymied with the templating system is how to pass parameters into routes.
My template looks like this:
#(wallPosts : Array[models.WallPost], wallPostIndex: Integer)
...
Next
...
I don't seem to be able to pass #wallPostIndex into the route in this href? How would I go about this?
For reference, this is the route:
GET / controllers.Application.index(wallPostIndex: Int ?= 1)
Thanks for your help.

Try removing the # before the wallPostIndex in your href, something like this:
Next

Related

Generating and consuming an array within a StringTemplate-4 template

I'm new to StringTemplate4 and probably I am going to ask something overly simple, impossible or stupid but I couldn't find any other information on it.
So far, I have set this minimal set of templates:
define(name,arity) ::= "<name>(<vars(arity)>)."
vars(n) ::= "<n:var();separator=\", \">"
var(n) ::= "V<n>"
and I would like to get:
pred(V1, V2, V3).
by calling the following code:
STGroup group = new STGroupFile(...);
ST st = group.getInstanceOf("define");
st.add("name", "pred");
st.add("arity", 3);
String result = st.render();
Is it possible? Many thanks in advance.
StringTemplate doesn't have built-in operators for repeating. Instead, you'll need to iterate, like described in the following question.
Is there anything like Enumerable.Range(x,y) in Java?
Keep in mind that you'll need to pass an Iterator<T> and not an Iterable<T> due to a current limitation in StringTemplate (the interpreter supports Collection<T>, but not Iterable<T>). If you want to use the built-in iteration variable i or i0, you could also pass an appropriately sized new Object[n].

how to test if a string DON'T match using protractor

I'm migrating my karma-ng-scenario tests suite to protractor. I would like to do something like
// karma-ng-scenario
expect(element('legend').text()).not().toBe("LOGIN_CONNECT");
in the protractor way. But it seems there isn't a not() function.
I'm using angular-translate to bind the LONGIN_CONNECT string into multiple languages and I want to test if the string is translated.
More globally, is there a a way test if something is different ? ... don't have a class, don't exists on the page, is not selected, ...
It is definitely worth looking at the API docs. I have these open pretty much all the time.
There are lots of Web Driver functions you can use like isEnabled(), isDisplayed(), isSelected() etc. Protractor uses Jasmine syntax so you can use '.toBe(false)' to assert things are false.
To check for classes, you can do something like:
expect(myElement.getAttribute('class')).toContain('my-class-name');
To compare strings and assert that they do NOT match you could use .not. Jasmine docs
say:
Every matcher's criteria can be inverted by prepending .not:
expect(x).not.toEqual(y); compares objects or primitives x and y and
passes if they are not equivalent
You can use something like:
expect(model.getText()).not.toContain('abcdef');
There is a .not property nowadays.
I'm using the following to check for NOT matching:
expect(element('legend').text() === "LOGIN_CONNECT").toBe(false);

Logging syntax for Play Framework 2 in Scala

This is a really silly question, but how can you do convenient formatting of log strings in Play Framework 2 (and in Scala?).
I've googled but its very difficult to find an example, essentially most links are talking about configuring Logback in the first place which I've done fine.
I'm basically trying to find the best stylistic way to do something like:
if(Logger.isDebugEnabled)
Logger.debug("Modified: Id = '" + real_session_id + "', Modified = " + modified.toString)
Coming from a C# background (and log4net) I'd assume you could do something like:
if(Logger.isDebugEnabled)
Logger.debug("Modified: Id = '{0}', Modified = {1}", real_session_id, modified.toString)
But I can't see how this would work with the trait the way it is defined. I've also seen vague references to how you might be able to avoid checking Logger.isDebugEnabled by using a lazy evaluative syntax like:
Logger.debug("Modified: Id = ${real_session_id}, Modified = ${modified.toString}")
That uses Scala macros - but again, that doesn't work and I can find very little information about it.
Am I missing something really blatant here?
The framework used for logging is logback. When you type : Logger.debug, the isDebugEnabled is already implicitly checked.
For the syntax of logging, use the Scala string interpolation.
Logger.debug(s"Modified: Id = '$real_session_id', Modified = $modified.toString")
Why not just use the standard String interpolation capabilities of the language/stdlib? http://docs.scala-lang.org/overviews/core/string-interpolation.html
I apologise if I've missed something crucial about your question.
As to avoiding the if (Logger.isDebugEnabled) check, if the logging framework is not providing some sort of lazy evaluation scheme for arguments passed into it, I would just first consider defining my own wrappers:
object MyLazyLogger {
def debug(msg: => Any) =
if (Logger.isDebugEnabled) Logger.debug(msg)
}
Also, I don't think the way in which you interpolate stuff into the string has anything to do with not evaluating the arguments to debug() if logging is disabled—if debug() declares that it eager-evaluates any arguments passed into it, there's no way that I can see you can change to lazy evaluation at the call site by just using a "special form" of string interpolation. (I'd be happy if anyone proved me wrong here and taught me something new :))
Disclosure: I'm not familiar with Play (yet), so I'm just taking a shot at a general approach here.

scala play 2.0 get request header

I am converting some of my java code to scala and I would like to be able to get a specific header and return it as a string.
In java I have:
return request().getHeader("myHeader")
I have been unable to achieve the same thing in scala. Any help would be greatly appreciated! Thanks!
You could write:
request.get("myHeader").orNull
If you wanted something essentially the same as your Java line. But you don't!
request.get("myHeader") returns an Option[String], which is Scala's way of encouraging you to write code that won't throw null pointer exceptions.
You can process the Option in various ways. For example, if you wanted to supply a default value:
val h: String = request.get("myHeader").getOrElse("")
Or if you want to do something with the header if it exists:
request.foreach { h: String => doSomething(h) }
Or just:
request foreach doSomething
See this cheat sheet for more possibilities.
Accepted answer doesn't work for scala with playframework 2.2:
request.get("myHeader").getOrElse("")
It gives me the below error:
value get is not a member of
play.api.mvc.Request[play.api.mvc.AnyContent]
use below
request.headers.get("myHeader").getOrElse("")

Scala manifest and instances

I'm using Jerkson, and I need to check if a given class can be serialized. The java version just needs a class, but jerkson does this:
def canSerialize[A](implicit mf: Manifest[A]) = mapper.canSerialize(mf.erasure)
Given that I have an instance, how can I call this? I pretty much tried
canSerialize[ClassManifest.fromClass(foo)]
But its not working. I wonder why the guys at jerkson could not make it simpler by just making this: canSerialize(Class[_]) ...
Any ideas on how can I invoke this?
Edit:
I fixed this by using:
canSerilialize(Manifest.classType(foo.getClass))
How about this:
canSerialize[Foo]
Compiler can automatically generate manifest for you (if it has enough type information in context)
Since Scala 2.8.0 canSerialize can be written via context bound. See more
If you don't know the class in advance, you can always pass the manifest as a parameter, i.e. this should work: canSerialize( Manifest.classType( foo.getClass ) ).