Posts

Showing posts from November, 2015

Scala combinator parser

This is my attempt to get you started with scala combinator parser. It is a very powerful tool that allows you to create your own lexical analysis similar to yacc and other lexer. Area of application includes

a) calculator that accept input string as follow "3+2"

b) match specific text in a given string like finding "fox" in the string "the brown fox jump over the fence"

I'm going to use an example from reference below. Here we are trying to extend our SimpleParser from RegexParser and defined a function called "word" that is basically a regex itself with the purpose of matching a single word.

Simple Parser

This code is taken from https://wiki.scala-lang.org/

import scala.util.parsing.combinator._ class SimpleParser extends RegexParsers { def word: Parser[String] = """[a-z]+""".r ^^ { _.toString } }
In code above, """ means it is a regex.  

^^ mean if  a match is found, you execute the follow…

Converting json instance type and list in Playframework

I found the easiest way to convert your instance to/from json in Playframework is to use the following code:

For type instance I'm just making use of Json.writes and Json.reads to do the dirty work.

case class CartProduct(id : String, qty : Double) object CartProduct { implicit val cartProductWrites = Json.writes[CartProduct] // convert to json implicit val cartProductReads = Json.reads[CartProduct] // example - get from post }
The code below works too, but a bit too long for me.

implicit val cartProductReads: Reads[CartProduct] = ( (JsPath \ "id").read[String] and (JsPath \ "qty").read[Double] )(CartProduct.apply _)
Somewhere in your controller, you might have the following code to write your instance in json format.

def getProduct(productId : String) = Action { val p = new Product("Demo", "Demo App", 12.0) Ok(Json.toJson(p)) }
To get back your instance from a HTTP post

def add = Action(BodyParsers.parse.json) { reque…

Debugging Playframework with IntelliJ IDEA

Image
To debug your application with IntelliJ is pretty straight forward.

Setup command prompt 

1. Run the following command
$ activator -jvm-debug 9999
2. You will probably see something like this

Listeningfor transport dt_socket at address:9999 3. Wait for the command prompt to appear and type  "Run" - If you have your application running, please stop the running process.


Setting up your IntelliJ 

1.In your IDE, goto Run -> Edit Configuration. Add "Remote" and configure the port to point to 9999.  Example of your configuration could look something like this.





Save this configuration. Then run your remote debug configuration and at this point you would need to set your breakpoints.

Open up your browser and go to "localhost:9000" and you will hit your breakpoint.