Posts

Showing posts from 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.




Getting ASP.Net to build and run on your machine

If you're trying to debug through ASP.Net MVC / WebAPI to see how it all glued together, you probably need to download and build it locally from here.

You should probably know that, ASP.Net MVC on github runs on DNX. and you need to either install VS2015 Web Developer Tools or set it up manually by following this guide.

If you face reference issue, for example

Unable to Reference DNX 4.5.1 or DNX Core 5.0, you probably have an installation issue with your VS2015 installation. 

Setup your DNX to the latest version and then reinstall VS2015. Yep i think it is stupid but it happened to me.

You can always goto the ASP.Net MVC git clone folder to run "build " manually. If you can  build that successfully, then all you have have to do is reinstall VS2015 and then open and build the project using VS2015.

After a successful build, you can run a test project to see if ASP.Net MVC is working properly.

To test it, run MvcSample.Web project by right clicking it and set it as a star…

How do you detect if your browser is online?

Answer :)

window.navigator.onLine

Generate SqlException with your preferred error code

A quick and dirty way of generating SqlException

class SqlExceptionGenerator
    {
        public static void ThrowException(SqlConnection con)
        {
            con.Open(); // should be a valid connection
            SqlCommand cmd = new SqlCommand("raiserror(50005, 16, 1)", con);
            cmd.ExecuteNonQuery();
        }
    }