Skip to content

Instantly share code, notes, and snippets.

@chandu0101
Last active November 24, 2015 08:58

Revisions

  1. chandu0101 revised this gist Nov 24, 2015. 1 changed file with 20 additions and 12 deletions.
    32 changes: 20 additions & 12 deletions GenrateCompanionsForScalaJSDefined
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,16 @@
    import Math._

    import org.scalajs.dom


    object ScalaJSExample extends js.JSApp{
    case class FieldName(name : String , tpe : String)
    def main(): Unit = {

    val name = "Seed"
    val name = "SeedCategory"

    val input = s"""
    |val id: String
    |val name: String
    | val category: SeedCategory
    | val duration: Int
    | val geolocations: js.Array[GeoJsonFeature]
    | val info: String
    | val image: Image
    |val category: String
    """.stripMargin


    @@ -25,22 +20,35 @@ object ScalaJSExample extends js.JSApp{
    FieldName(s2.head,s2.last)
    }).filter(ft => ft.name.nonEmpty && ft.tpe.nonEmpty)

    val result = s"""
    val comanionObject = s"""
    |object $name {
    |
    | def apply(${fieldTypes.map(ft => s"${ft.name}: ${ft.tpe}").mkString(", ")})= {
    | ${fieldTypes.map(ft => s"val ${ft.name}_sh = ${ft.name} ").mkString("\n")}
    | new $name {
    | ${fieldTypes.map(ft => s"val ${ft.name} = ${ft.name}_sh").mkString("\n\n")}
    | ${fieldTypes.map(ft => s"val ${ft.name} = ${ft.name}_sh").mkString("\n")}
    | }
    | }
    |
    |}
    """.stripMargin

    val copyMethod = s"""

    | def copy(${fieldTypes.map(ft => s"${ft.name}: ${ft.tpe} = ${ft.name}").mkString(", ")})= {
    | ${fieldTypes.map(ft => s"val ${ft.name}_sh = ${ft.name} ").mkString("\n")}
    | new $name {
    | ${fieldTypes.map(ft => s"val ${ft.name} = ${ft.name}_sh").mkString("\n")}
    | }
    | }
    |
    """.stripMargin


    println(result)
    dom.window.console.log(result)
    println(comanionObject)
    dom.window.console.log(comanionObject)
    println(copyMethod)
    dom.window.console.log(copyMethod)

    }
    }
  2. chandu0101 created this gist Nov 24, 2015.
    46 changes: 46 additions & 0 deletions GenrateCompanionsForScalaJSDefined
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    import Math._
    import org.scalajs.dom


    object ScalaJSExample extends js.JSApp{
    case class FieldName(name : String , tpe : String)
    def main(): Unit = {

    val name = "Seed"

    val input = s"""
    |val id: String
    |val name: String
    | val category: SeedCategory
    | val duration: Int
    | val geolocations: js.Array[GeoJsonFeature]
    | val info: String
    | val image: Image
    """.stripMargin


    val fieldTypes = input.split("\n").filter(_.nonEmpty).map(s => {
    val s1 = s.trim.replace("val","").replace(" ","")
    val s2 = s1.split(":")
    FieldName(s2.head,s2.last)
    }).filter(ft => ft.name.nonEmpty && ft.tpe.nonEmpty)

    val result = s"""
    |object $name {
    |
    | def apply(${fieldTypes.map(ft => s"${ft.name}: ${ft.tpe}").mkString(", ")})= {
    | ${fieldTypes.map(ft => s"val ${ft.name}_sh = ${ft.name} ").mkString("\n")}
    | new $name {
    | ${fieldTypes.map(ft => s"val ${ft.name} = ${ft.name}_sh").mkString("\n\n")}
    | }
    | }
    |
    |}
    """.stripMargin


    println(result)
    dom.window.console.log(result)

    }
    }