Git Product home page Git Product logo

polyjuicelib's Introduction

Polyjuice library for mapping JSON objects with dynamic JavaScript code in Scala

CircleCI Crates.io

What it does

                                       +----------------+
                                       |                |
                                       | Dynamic        |
                                       | JavaScript code|
                                       |       +        |
                                       +-------|--------+
                                               |
                                               |
                                               |
                                               v
         +------------+                +---------------+                +-------------+
   JVM   |            |                |               |  mapped object |             |      JVM
+-object---> Json4s  +--object as JSON --> Polyjuice +----------by------->  Json4s   +--mapped object->
         | serialize  |                |               |  given js code | Deserialize |
         |            |                |               |     as JSON    |             |
         +------------+                +---------------+                +-------------+

Why it was build

Let's say you have an expense report which is modeled something like this

case class Report(createdAt: Option[LocalDateTime], currency: Currency, amount: Double, country: Option[String], customFields: Map[String,CustomField]) 

Here at VATBox we are dealing with a lot of Reports (no they don't look like this unfortunately), each one of them have different custom fields which is customer depending. For example one customer can have a custom field called "center" with ~20 different values one for each country it has a subsidiary in. Since each "center" is in a different country there are different rules hence we need to map each report by different custom fields with countries etc.

This way we can write customer's report specific mapping code and just "upload" it to an already running applications without having them altered for this.

Features

  • Nashorn
  • Supports 'primitive' types or your own custom models via Json4s
  • Malicious code safety (endless loops, external libraries, native code)

SBT

For json4s 3.5.0 and scala-logging 3.5.0 use polyjuice version 1.1.8

libraryDependencies ++= Seq(
  "com.vatbox" %% "polyjuice" % "1.1.8",
  
  /* Unless you already have them */
  "com.typesafe.scala-logging" %% "scala-logging" % "3.5.0",
  "org.json4s" %% "json4s-native" % "3.5.0",
  "org.json4s" %% "json4s-ext" % "3.5.0"
)

For json4s 3.4.0 and scala-logging 3.1.0 use polyjuice version 1.1.7

libraryDependencies ++= Seq(
  "com.vatbox" %% "polyjuice" % "1.1.7",
  
  /* Unless you already have them */
  "com.typesafe.scala-logging" %% "scala-logging" % "3.4.0",
  "org.json4s" %% "json4s-native" % "3.4.0",
  "org.json4s" %% "json4s-ext" % "3.1.0"
)

Examples

There are more examples in Tests

  • Return a String value of the "name" key
"Return String" in {
        val mapper = Polyjuice.createMapper(varName = "report", userCode = s"""return report.name;""")
        val triedT = mapper.map[String](s"""{"name" : "hello World"}""")
        triedT.futureValue.value shouldBe "hello World"
      }
  • Return an Int
"Return Int" in {
        val mapper = Polyjuice.createMapper("expense", s"""if (expense.expense > 400) {return 1;} else {return 2;}""")
        val triedT = mapper.map[Int](s"""{"expense" : 500}""")
        triedT.futureValue.value shouldBe 1
      }
  • Return a Double (well you got the idea)
"Return Double" in {
        val mapper = Polyjuice.createMapper("unused", s"""return 3.3;""")
        val triedT = mapper.map[Double](s"""{"irrelevant" : 500}""")
        triedT.futureValue.value shouldBe 3.3
      }
  • Return a LocalDateTime
"Return Date as LocalDateTime" in {
        val mapper = Polyjuice.createMapper("unused", s"""return new Date();""")
        val triedT = mapper.map[LocalDateTime](s"""{"irrelevant" : 500}""")
        triedT.futureValue.value shouldBe a [LocalDateTime]
      }
  • Now return that same Date but now as 8601 String
"Return Date as String" in {
        val mapper = Polyjuice.createMapper("unused", s"""return new Date();""")
        val triedT = mapper.map[String](s"""{"irrelevant" : 500}""")
        triedT.futureValue.value shouldBe a [String]
        // 2016-10-19T10:07:05.427Z
      }
  • Return a custom object
case class SimpleTestModel(name: String, value: Int)
"Return Object as Object" in {
        val mapper = Polyjuice.createMapper("report", s"""return {name: report.firstName, value : 5};""")
        val triedT = mapper.map[SimpleTestModel](s"""{"firstName" : "Cool"}""")
        triedT.futureValue.value shouldBe SimpleTestModel("Cool", 5)
      }

Safety part - endless loop

    "Infinite loop" in {
      val mapper = Polyjuice.createMapper("whatever",
        s"""
           |while(true){
           |}""".stripMargin)
      val triedT = mapper.map(jsonObject = s"""{ "name" : "somename" }""", timeout = 3 seconds)
      triedT.failed.futureValue shouldBe a [TimedoutExecution]
    }

polyjuicelib's People

Contributors

talgendler avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.