Lift error-check multiple form fields - scala

I'm following a tutorial on how to validate form fields, but it only demonstrates it for one field. How can I validate, and display errors for multiple fields?
I tried the following - but it always succeeds and does a redirect - no matter the errors:
def process() = {
if (patientName == "Joe") {
S.error("patientName", "Joe not allowed!")
}
if (birthdate == "22/22/2222") {
S.error("birthdate", "Invalid date!")
}
S.notice("Success! You entered Patient name: " + patientName); S.redirectTo("/")
}

Ha! I figured it out. Beautiful.
def process() = {
if (patientName == "Joe") {
S.error("Joe not allowed!")
}
if (birthdate == "22/22/2222") {
S.error("birthdate", "Invalid birthdate!")
}
S.errors match {
case Nil =>S.notice("Patient name: " + patientName); S.redirectTo("/")
case _ =>
}
}

Related

Put a filter into a future

I want to execute sql statements when the type isn't fruits. This is meant by (type != "fruits" && !isFruits)
I tried the following code, but it drops an error because an else is missing. I don't need an else.
if (type != "fruits" && !isFruits) {
con.executeFuture(query).flatMap(_ => {
conn.initQuery(query, isVegetables)
if (isVegetables) {
conn.VegetableCon.get.queryFuture(RunArgs)
} else {
conn.VegetableQueryCon.get.queryFuture(RunArgs)
}
})
}
I did some research and found ".filter", so I tried the following code:
con.executeFuture(query).filter(_ => type != ("ddl") && !isFruits).flatMap(_ => {
conn.initQuery(query, isVegetables)
if (isVegetables) {
conn.VegetableCon.get.queryFuture(RunArgs)
} else {
conn.VegetableQueryCon.get.queryFuture(RunArgs)
}
})
}
It drops me the error : "Future.filter predicate is not satisfied."
Now I don't know how to solve my problem. Do you have any idea about my mistakes?

Type checking on user input Scala Spark

I'm trying to do a type checking on an input that the user insert, but I don't know ho to do.
My code is:
for(iteration<- 0 to 19) {
print(movieRecommended(iteration))
var personalRate = scala.io.StdIn.readDouble()
if(Try(personalRate.toInt).isSuccess){
while((personalRate > 5 || personalRate < 1) || personalRate % 0.5 != 0) {
println("Error: respect the parameters")
print(movieRecommended(iteration))
personalRate = scala.io.StdIn.readDouble()
}
}
else println("Error")
ratingArr(iteration) = personalRate
}
But when I try to give a string input like "a" I receive this error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "a"
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.base/java.lang.Double.parseDouble(Double.java:549)
at scala.collection.immutable.StringLike.toDouble(StringLike.scala:321)
at scala.collection.immutable.StringLike.toDouble$(StringLike.scala:321)
at scala.collection.immutable.StringOps.toDouble(StringOps.scala:33)
at scala.io.StdIn.readDouble(StdIn.scala:167)
at scala.io.StdIn.readDouble$(StdIn.scala:162)
at scala.io.StdIn$.readDouble(StdIn.scala:241)
at CollaborativeFilteringUserBasedALS$.$anonfun$askUserInput$1(CollaborativeFilteringUserBasedALS.scala:192)
at scala.collection.immutable.Range.foreach$mVc$sp(Range.scala:158)
at CollaborativeFilteringUserBasedALS$.askUserInput(CollaborativeFilteringUserBasedALS.scala:189)
at CollaborativeFilteringUserBasedALS$.topRated(CollaborativeFilteringUserBasedALS.scala:133)
at CollaborativeFilteringUserBasedALS$.main(CollaborativeFilteringUserBasedALS.scala:284)
at CollaborativeFilteringUserBasedALS.main(CollaborativeFilteringUserBasedALS.scala)
Someone can explain me what I'm doing wrong?
You can read the input as a string using scala.io.StdIn.readLine() then try to parse it to Int and use pattern matching to handle case for valid int and for error.
Something like this :
for (iteration <- 0 to 19) {
var personalRate = scala.io.StdIn.readLine()
Try(personalRate.toInt).toOption match {
case Some(rate) => {
println(rate)
// your logic for int
}
case _ => println("Error")
//...
}
}

Parsing text and representing it with tokens using Scala

I'm getting frustrated trying to convert a small part of the Golang templating language to Scala.
Below are the key parts of the lex.go source code: https://github.com/golang/go/blob/master/src/text/template/parse/lex.go
The tests are here: https://github.com/golang/go/blob/master/src/text/template/parse/lex_test.go
Basically this "class" takes a string and returns an Array of "itemType". In the template string, the start and end of special tokens is using curly braces {{ and }}.
For for example:
"{{for}}"
returns an array of 4 items:
item{itemLeftDelim, 0, "{{" } // scala case class would be Item(ItemLeftDelim, 0, "")
item{itemIdentifier, 0, "for"}
item{itemRightDelim, 0, "}}"}
item{itemEOF, 0, ""}
The actual call would look like:
l := lex("for", `{{for}}`, "{{", "}}") // you pass in the start and end delimeters {{ and }}
for {
item := l.nextItem()
items = append(items, item)
if item.typ == itemEOF || item.typ == itemError {
break
}
}
return
The key parts of the source code are below:
// itemType identifies the type of lex items.
type itemType int
const (
itemError itemType = iota // error occurred; value is text of error
itemEOF
itemLeftDelim // left action delimiter
// .............. skipped
)
const (
leftDelim = "{{"
rightDelim = "}}"
leftComment = "/*"
rightComment = "*/"
)
// item represents a token or text string returned from the scanner.
type item struct {
typ itemType // The type of this item.
pos Pos // The starting position, in bytes, of this item in the input string.
val string // The value of this item.
}
// stateFn represents the state of the scanner as a function that returns the next state.
type stateFn func(*lexer) stateFn
// lexer holds the state of the scanner.
type lexer struct {
name string // the name of the input; used only for error reports
input string // the string being scanned
leftDelim string // start of action
rightDelim string // end of action
state stateFn // the next lexing function to enter
pos Pos // current position in the input
start Pos // start position of this item
width Pos // width of last rune read from input
lastPos Pos // position of most recent item returned by nextItem
items chan item // channel of scanned items
parenDepth int // nesting depth of ( ) exprs
}
// lex creates a new scanner for the input string.
func lex(name, input, left, right string) *lexer {
if left == "" {
left = leftDelim
}
if right == "" {
right = rightDelim
}
l := &lexer{
name: name,
input: input,
leftDelim: left,
rightDelim: right,
items: make(chan item),
}
go l.run()
return l
}
// run runs the state machine for the lexer.
func (l *lexer) run() {
for l.state = lexText; l.state != nil; {
l.state = l.state(l)
}
}
// nextItem returns the next item from the input.
func (l *lexer) nextItem() item {
item := <-l.items
l.lastPos = item.pos
return item
}
// emit passes an item back to the client.
func (l *lexer) emit(t itemType) {
l.items <- item{t, l.start, l.input[l.start:l.pos]}
l.start = l.pos
}
// lexText scans until an opening action delimiter, "{{".
func lexText(l *lexer) stateFn {
for {
if strings.HasPrefix(l.input[l.pos:], l.leftDelim) {
if l.pos > l.start {
l.emit(itemText)
}
return lexLeftDelim
}
if l.next() == eof {
break
}
}
// Correctly reached EOF.
if l.pos > l.start {
l.emit(itemText)
}
l.emit(itemEOF)
return nil
}
// next returns the next rune in the input.
func (l *lexer) next() rune {
if int(l.pos) >= len(l.input) {
l.width = 0
return eof
}
r, w := utf8.DecodeRuneInString(l.input[l.pos:])
l.width = Pos(w)
l.pos += l.width
return r
}
// lexLeftDelim scans the left delimiter, which is known to be present.
func lexLeftDelim(l *lexer) stateFn {
l.pos += Pos(len(l.leftDelim))
if strings.HasPrefix(l.input[l.pos:], leftComment) {
return lexComment
}
l.emit(itemLeftDelim)
l.parenDepth = 0
return lexInsideAction
}
// lexRightDelim scans the right delimiter, which is known to be present.
func lexRightDelim(l *lexer) stateFn {
l.pos += Pos(len(l.rightDelim))
l.emit(itemRightDelim)
return lexText
}
// there are more stateFn
So I was able to write the item and itemType:
case class Item(typ: ItemType, pos: Int, v: String)
sealed trait ItemType
case object ItemError extends ItemType
case object ItemEOF extends ItemType
case object ItemLeftDelim extends ItemType
...
..
.
The stateFn and Lex definitions:
trait StateFn extends (Lexer => StateFn) {
}
I'm basically really stuck on the main parts here. So things seem to be kicked of like this:
A Lex is created, then "go l.run()" is called.
Run is a loop, which keeps looping until EOF or an error is found.
The loop initializes with lexText, which scans until it finds an {{, and then it sends a message to a channel with all the preceding text of type 'itemText', passing it an 'item'. It then returns the function lexLeftDelim. lexLeftDelim does the same sort of thing, it sends a message 'item' of type itemLeftDelim.
It keeps parsing the string until it reaches EOF basically.
I can't think in scala that well, but I know I can use an Actor here to pass it a message 'Item'.
The part of returning a function, I asked I got some good ideas here: How to model recursive function types?
Even after this, I am really frustrated and I can seem to glue these concepts together.
I'm not looking for someone to implement the entire thing for me, but if someone could write just enough code to parse a simple string like "{{}}" that would be awesome. And if they could explain why they did a certain design that would be great.
I created a case class for Lex:
case class Lex(
name: String,
input: String,
leftDelim: String,
rightDelim: String,
state: StateFn,
var pos: Int = 0,
var start: Int = 0,
var width: Int = 0,
var lastPos: Int = 0,
var parenDepth: Int = 0
) {
def next(): Option[String] = {
if (this.pos >= this.input.length) {
this.width = 0
return None
}
this.width = 1
val nextChar = this.input.drop(this.pos).take(1)
this.pos += 1
Some(nextChar)
}
}
The first stateFn is LexText and so far I have:
object LexText extends StateFn {
def apply(l: Lexer) = {
while {
if (l.input.startsWith(l.leftDelim)) {
if (l.pos > l.start) {
// ????????? emit itemText using an actor?
}
return LexLeftDelim
}
if (l.next() == None) {
break
}
}
if(l.pos > l.start) {
// emit itemText
}
// emit EOF
return None // ?? nil? how can I support an Option[StateFn]
}
}
I need guidance on getting the Actor's setup, along with the main run loop:
func (l *lexer) run() {
for l.state = lexText; l.state != nil; {
l.state = l.state(l)
}
}
This is an interesting problem domain that I tried to tackle using Scala, and so far I am a bit confused hoping some else finds it interesting and can work with what little I have so far and provide some code and critique if I am doing it correctly or not.
I know deep down I shouldn't be mutating, but I'm still on the first few pages of the functional book :)
If you translate the go code literally into Scala, you'll get very unidiomatic piece of code. You'll probably get much more maintainable (and shorter!) Scala version by using parser combinators. There are plenty of resources about them on the internet.
import scala.util.parsing.combinator._
sealed trait ItemType
case object LeftDelim extends ItemType
case object RightDelim extends ItemType
case object Identifier extends ItemType
case class Item(ty: ItemType, token: String)
object ItemParser extends RegexParsers {
def left: Parser[Item] = """\{\{""".r ^^ { _ => Item(LeftDelim, "{{") }
def right: Parser[Item] = """\}\}""".r ^^ { _ => Item(RightDelim, "}}") }
def ident: Parser[Item] = """[a-z]+""".r ^^ { x => Item(Identifier, x) }
def item: Parser[Item] = left | right | ident
def items: Parser[List[Item]] = rep(item)
}
// ItemParser.parse(ItemParser.items, "{{foo}}")
// res5: ItemParser.ParseResult[List[Item]] =
// [1.8] parsed: List(Item(LeftDelim,{{), Item(Identifier,foo), Item(RightDelim,}}))
Adding whitespace skipping, or configurable left and right delimiters is trivial.

Unable to call a groovy closure

I am using ws-lite to automate web service testing, and I want to have more flexible control over the xm l request generated.
Basically the request body is a groovy closure that will be passed to MarkupBuilder in order to create the SOAP message.
Here is an example of what I am trying to achieve (example taken from https://github.com/jwagenleitner/groovy-wslite):
#Grab(group='com.github.groovy-wslite', module='groovy-wslite', version='1.1.0')
import wslite.soap.*
def client = new SOAPClient('http://www.holidaywebservice.com/Holidays/US/Dates/USHolidayDates.asmx')
def month = ["Feb"]
def response = client.send(SOAPAction:'http://www.27seconds.com/Holidays/US/Dates/GetMothersDay') {
body {
GetMothersDay('xmlns':'http://www.27seconds.com/Holidays/US/Dates/') {
year(2011)
month.each{ a = null ->
if (a != null){
"month"(a)
}
}
}
}
}
assert "2011-05-08T00:00:00" == response.GetMothersDayResponse.GetMothersDayResult.text()
assert 200 == response.httpResponse.statusCode
assert "ASP.NET" == response.httpResponse.headers['X-Powered-By']
The above example, I can create month tag fine with value/values specified.
But if I change it to be:
#Grab(group='com.github.groovy-wslite', module='groovy-wslite', version='1.1.0')
import wslite.soap.*
def client = new SOAPClient('http://www.holidaywebservice.com/Holidays/US/Dates/USHolidayDates.asmx')
def month_cl = { a -> "month"(a) }
def response = client.send(SOAPAction:'http://www.27seconds.com/Holidays/US/Dates/GetMothersDay') {
body {
GetMothersDay('xmlns':'http://www.27seconds.com/Holidays/US/Dates/') {
year(2011)
month_cl("Feb")
}
}
}
assert "2011-05-08T00:00:00" == response.GetMothersDayResponse.GetMothersDayResult.text()
assert 200 == response.httpResponse.statusCode
assert "ASP.NET" == response.httpResponse.headers['X-Powered-By']
I will have a missing method exception.
I don't quite understand why I can't just invoke a groovy closure like that?
Delegate of month_cl closure has to be set to the current/parent delegate (in this case it is closure passed as param to GetMothersDay). Try with:
body {
GetMothersDay('xmlns':'http://www.27seconds.com/Holidays/US/Dates/') {
year(2011)
month_cl.delegate = delegate
month_cl("Feb")
}
}
It's quite normal to get MissingMethodException, because the closure month_cl is calling month method which does not exist. To do this (in your way), you should pass a Closure c to month_cl and call it on the argument a, like this:
def month_cl = { a, Closure c -> c(a) }
and using the new implementation, month_cl("Feb") becomes month_cl("Feb") { "month" } which results to "month"("Feb").
Here is a working example:
#Grab(group='com.github.groovy-wslite', module='groovy-wslite', version='1.1.0')
import wslite.soap.*
def client = new SOAPClient('http://www.holidaywebservice.com/Holidays/US/Dates/USHolidayDates.asmx')
def months = ["Feb"]
def month_cl = { m, Closure c -> return c(m) }
def response = client.send(SOAPAction:'http://www.27seconds.com/Holidays/US/Dates/GetMothersDay') {
body {
GetMothersDay('xmlns':'http://www.27seconds.com/Holidays/US/Dates/') {
year(2011)
month_cl("Feb") { "month" } // -> month("Feb")
}
}
}
assert "2011-05-08T00:00:00" != response.GetMothersDayResponse.GetMothersDayResult.text()
assert 200 == response.httpResponse.statusCode
assert "ASP.NET" == response.httpResponse.headers['X-Powered-By']

BSONDocument.getAs issue

I have 1 problem and 1 question when using BSONDocument getAs.
Whenever I try to access the value l in the format below by calling this:
docFound.getAs[Int]("v.1.0.2013.9.9.l")
it returns None. However if I do:
docFound.getAs[BSONDocument]("v")
it returtns a valid BSONDocument for the whole v section. What is wrong in my first call? Does reactivemongo support path traversal?
BSONDocument: {
v: {
1.0: {
2013: {
9: {
9: {
l: BSONInteger(0),
s: BSONInteger(8)
}
}
}
}
}
}
The second question is:
I find a document in DB with the following filter:
BSONDocument(
"_id" -> 0,
"v.1.0.2013.9.9.l" -> 1)
But it seems like instead of extracting just these values "_id" & "l" it extracts the whole document. When I do BSONDocument.pretty(foundDoc) I see the whole document, not just "l" value that I have requested. Please clarify if it is even worth specifying fields I am interested in if it always downloads the whole document.
Thanks.
It seems like according to the sources it is not supported in reactivemongo. So I have created a quick helper:
def getAsByPath[T](path: String, doc: BSONDocument)
(implicit reader: BSONReader[_ <: BSONValue, T]): Option[T] = {
val pathChunks = path.split('.')
var pathIndex = 0
var curDoc: Option[BSONDocument] = Some(doc)
var currentChunk = ""
while(pathIndex != pathChunks.size) {
currentChunk += pathChunks(pathIndex)
// If this is the last chunk it must be a value
// and if previous Doc is valid let's get it
if (pathIndex == pathChunks.size - 1 && curDoc != None)
return curDoc.get.getAs[T](currentChunk)
val tmpDoc = curDoc.get.getAs[BSONDocument](currentChunk)
if (tmpDoc != None) {
currentChunk = ""
curDoc = tmpDoc
} else {
// We need to support this as sometimes doc ID
// contain dots, for example "1.0"
currentChunk += "."
}
pathIndex += 1
}
None
}
However my second question is still valid. If someone knows please let me know.