Skip to content

Commit

Permalink
some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
carueda committed Dec 1, 2016
1 parent 865d618 commit e259f98
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 156 deletions.
42 changes: 13 additions & 29 deletions src/main/scala/tscfg/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,12 @@ object Main {
val destFile = new File(destFilename)
val out = new PrintWriter(destFile)

implicit val genOpts = GenOpts(opts.packageName, opts.className, opts.j7,
val genOpts = GenOpts(opts.packageName, opts.className, opts.j7,
preamble = Some(
s"""generated by tscfg $version on ${new Date()}
|source: $inputFilename"
""".stripMargin.trim
),
language = opts.language
)
)

println(s"parsing: $inputFilename")
Expand All @@ -121,7 +120,17 @@ object Main {
//println("\nobjSpec:\n |" + objSpec.format().replaceAll("\n", "\n |"))

println(s"generating: $destFile")
generate(objSpec, out)
val pw = out match {
case w: PrintWriter => w
case w => new PrintWriter(w)
}
val generator: Generator = opts.language match {
case "java" => new JavaGenerator(genOpts)
case "scala" => new ScalaGenerator(genOpts)
}
val results = generator.generate(objSpec)
pw.println(results.code)

out.close()

/*
Expand All @@ -134,29 +143,4 @@ object Main {
}
*/
}

/**
* Generates code for the given configuration spec.
*
* @param objSpec specification
* @param out code is written here
* @param genOpts generation options
*/
private def generate(objSpec: ObjSpec, out: Writer)
(implicit genOpts: GenOpts): GenResult = {

val pw = out match {
case w: PrintWriter => w
case w => new PrintWriter(w)
}

val generator: Generator = genOpts.language match {
case "java" => new JavaGenerator(genOpts)
case "scala" => new ScalaGenerator(genOpts)
}

val results = generator.generate(objSpec)
pw.println(results.code)
results
}
}
4 changes: 1 addition & 3 deletions src/main/scala/tscfg/generators/Generator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ abstract class Generator(genOpts: GenOpts) {
* @param className class name
* @param j7 true to generate code for Typesafe Config v <= 1.2.1
* @param preamble preamble to include in generated code
* @param language target language, default "java"
*/
case class GenOpts(packageName: String = defaults.packageName,
className: String = defaults.className,
j7: Boolean = false,
preamble: Option[String] = None,
language: String = "java"
preamble: Option[String] = None
)

case class GenResult(code: String = "?",
Expand Down
5 changes: 5 additions & 0 deletions src/main/scala/tscfg/specs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ object specs {
// $COVERAGE-ON$
}

object ObjSpec {
def apply(name: String, elems: (String, Spec)*): ObjSpec =
ObjSpec(Key(name), Map(elems: _*))
}

case class ListSpec(elemSpec: Spec,
comments: List[String] = List.empty
) extends Spec {
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/tscfg/SpecBuilderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SpecBuilderSpec extends Specification {
"with empty input" should {
val spec = build("")
"only build CFG spec" in {
spec === ObjSpec(Key(genOpts.className), Map.empty)
spec === ObjSpec(genOpts.className)
}
}

Expand Down
88 changes: 26 additions & 62 deletions src/test/scala/tscfg/generators/java/JavaGeneratorSpec.scala
Original file line number Diff line number Diff line change
@@ -1,82 +1,46 @@
package tscfg.generators.java

import com.typesafe.config.ConfigFactory
import org.specs2.mutable.Specification
import tscfg._
import tscfg.generators.{GenOpts, GenResult, Generator}

import tscfg.generators.{GenOpts, GenResult}
import tscfg.specs.types.{INTEGER, STRING}
import tscfg.specs.{AtomicSpec, ObjSpec}

class JavaGeneratorSpec extends Specification {

def generate(configString: String, showOutput: Boolean = false)
(implicit genOpts: GenOpts): GenResult = {

val config = ConfigFactory.parseString(configString).resolve()

val objSpec = new SpecBuilder(Key(genOpts.className)).fromConfig(config)
//println("\nobjSpec:\n |" + objSpec.format().replaceAll("\n", "\n |"))

val generator: Generator = new JavaGenerator(genOpts)

val results = generator.generate(objSpec)
private val genOpts = GenOpts(
packageName = "pkg",
className = "CFG")

if (showOutput)
println("Output:\n " + results.code.replace("\n", "\n "))
private val showOutput = false

private def generate(objSpec: ObjSpec): GenResult = {
val results = new JavaGenerator(genOpts).generate(objSpec)
if (showOutput) println("Output:\n " + results.code.replace("\n", "\n "))
results
}

implicit val genOpts = GenOpts(
language = "java",
packageName = "pkg",
className = "CFG")

"with good input" should {
val result = generate(
"""
|foo {
| bar = string
| baz = int
|}
""".stripMargin)
"with direct ObjSpec" should {
val objSpec = ObjSpec("foo",
"bar" AtomicSpec(STRING),
"baz" AtomicSpec(INTEGER)
)
val result = generate(objSpec)
//println(result.code)

"generate expected classes " in {
result.classNames must contain("CFG", "Foo")
result.classNames.size must beEqualTo(2)
}

"generate expected fields" in {
result.fieldNames must contain("foo", "bar", "baz")
result.fieldNames.size must beEqualTo(3)
result.classNames === Set("Foo")
result.fieldNames === Set("bar", "baz")
}
}

"with empty input" should {
val results = generate("")
"only generate CFG class" in {
results.classNames must contain("CFG")
results.classNames.size must beEqualTo(1)
results.fieldNames must beEmpty
}
}
"with direct empty ObjSpec" should {
val objSpec = ObjSpec("foo")
val result = generate(objSpec)
//println(result.code)

"handle issue14" should {
val result = generate(
"""
|0 = {
| 1 = bar
|}
|2 = foo
""".stripMargin)

"generate expected class " in {
result.classNames must contain("CFG", "_0")
result.classNames.size must beEqualTo(2)
}

"generate expected fields" in {
result.fieldNames must contain("_0", "_1", "_2")
result.fieldNames.size must beEqualTo(3)
"generate expected classes " in {
result.classNames === Set("Foo")
result.fieldNames === Set.empty
}
}
}
87 changes: 26 additions & 61 deletions src/test/scala/tscfg/generators/scala/ScalaGeneratorSpec.scala
Original file line number Diff line number Diff line change
@@ -1,81 +1,46 @@
package tscfg.generators.scala

import com.typesafe.config.ConfigFactory
import org.specs2.mutable.Specification
import tscfg.{Key, SpecBuilder}
import tscfg.generators.{GenOpts, GenResult, Generator}
import tscfg.generators.{GenOpts, GenResult}
import tscfg.specs.types.{INTEGER, STRING}
import tscfg.specs.{AtomicSpec, ObjSpec}

class ScalaGeneratorSpec extends Specification {

def generate(configString: String, showOutput: Boolean = false)
(implicit genOpts: GenOpts): GenResult = {

val config = ConfigFactory.parseString(configString).resolve()

val objSpec = new SpecBuilder(Key(genOpts.className)).fromConfig(config)
//println("\nobjSpec:\n |" + objSpec.format().replaceAll("\n", "\n |"))

val generator: Generator = new ScalaGenerator(genOpts)

val results = generator.generate(objSpec)
private val genOpts = GenOpts(
packageName = "pkg",
className = "CFG")

if (showOutput)
println("Output:\n " + results.code.replace("\n", "\n "))
private val showOutput = false

private def generate(objSpec: ObjSpec): GenResult = {
val results = new ScalaGenerator(genOpts).generate(objSpec)
if (showOutput) println("Output:\n " + results.code.replace("\n", "\n "))
results
}

implicit val genOpts = GenOpts(
language = "scala",
packageName = "pkg",
className = "CFG")

"with good input" should {
val result = generate(
"""
|foo {
| bar = string
| baz = int
|}
""".stripMargin)
"with direct ObjSpec" should {
val objSpec = ObjSpec("foo",
"bar" AtomicSpec(STRING),
"baz" AtomicSpec(INTEGER)
)
val result = generate(objSpec)
//println(result.code)

"generate expected classes " in {
result.classNames must contain("CFG", "Foo")
result.classNames.size must beEqualTo(2)
}

"generate expected fields" in {
result.fieldNames must contain("foo", "bar", "baz")
result.fieldNames.size must beEqualTo(3)
}
}

"with empty input" should {
val results = generate("")
"only generate CFG class" in {
results.classNames must contain("CFG")
results.classNames.size must beEqualTo(1)
results.fieldNames must beEmpty
result.classNames === Set("Foo")
result.fieldNames === Set("bar", "baz")
}
}

"handle issue14" should {
val result = generate(
"""
|0 = {
| 1 = bar
|}
|2 = foo
""".stripMargin)

"generate expected class " in {
result.classNames must contain("CFG", "_0")
result.classNames.size must beEqualTo(2)
}
"with direct empty ObjSpec" should {
val objSpec = ObjSpec("foo")
val result = generate(objSpec)
//println(result.code)

"generate expected fields" in {
result.fieldNames must contain("_0", "_1", "_2")
result.fieldNames.size must beEqualTo(3)
"generate expected classes " in {
result.classNames === Set("Foo")
result.fieldNames === Set.empty
}
}
}

0 comments on commit e259f98

Please sign in to comment.