Skip to content

Commit

Permalink
0.9.1: minor release: fix typo and add some info regarding backticks …
Browse files Browse the repository at this point in the history
…for scala identifiers
  • Loading branch information
carueda committed Feb 12, 2018
1 parent 245af53 commit ac561c8
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
lazy val tscfgVersion = setVersion("0.9.0")
lazy val tscfgVersion = setVersion("0.9.1")

organization := "com.github.carueda"
name := "tscfg"
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ Options (default):
--dd <destDir> (/tmp)
--j7 generate code for java <= 7 (8)
--scala generate scala code (java)
--scala:` use backsticks (false)
--scala:bt use backticks (#30) (false)
--java generate java code (the default)
--tpl <filename> generate config template (no default)
--tpl.ind <string> template indentation string (" ")
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.conf
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tscfg.version = 0.9.0
tscfg.version = 0.9.1
12 changes: 7 additions & 5 deletions src/main/scala/tscfg/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ object Main {
| --dd <destDir> ($defaultDestDir)
| --j7 generate code for java <= 7 (8)
| --scala generate scala code (java)
| --scala:` use backsticks (false)
| --scala:bt use backticks (see #30) (false)
| --java generate java code (the default)
| --tpl <filename> generate config template (no default)
| --tpl.ind <string> template indentation string ("${templateOpts.indent}")
| --tpl.cp <string> prefix for template comments ("${templateOpts.commentPrefix}")
|Output is written to $$destDir/$$className.ext
|
|More information at https://github.com/carueda/tscfg
""".stripMargin

case class CmdLineOpts(inputFilename: Option[String] = None,
Expand All @@ -48,7 +50,7 @@ object Main {
destDir: String = defaultDestDir,
j7: Boolean = false,
language: String = "java",
useBacksticks: Boolean = false,
useBackticks: Boolean = false,
tplFilename: Option[String] = None
)

Expand Down Expand Up @@ -88,8 +90,8 @@ object Main {
case "--scala" :: rest =>
traverseList(rest, opts.copy(language = "scala"))

case "--scala:`" :: rest =>
traverseList(rest, opts.copy(useBacksticks = true))
case "--scala:bt" :: rest =>
traverseList(rest, opts.copy(useBackticks = true))

case "--java" :: rest =>
traverseList(rest, opts.copy(language = "java"))
Expand Down Expand Up @@ -133,7 +135,7 @@ object Main {
val out = new PrintWriter(destFile)

val genOpts = GenOpts(opts.packageName, opts.className, opts.j7,
useBacksticks = opts.useBacksticks)
useBackticks = opts.useBackticks)

println(s"parsing: $inputFilename")
val source = io.Source.fromFile(new File(inputFilename)).mkString.trim
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/tscfg/generators/Generator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class Generator(genOpts: GenOpts) {
case class GenOpts(packageName: String,
className: String,
j7: Boolean,
useBacksticks: Boolean = false
useBackticks: Boolean = false
)

case class GenResult(code: String = "?",
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/tscfg/generators/scala/ScalaGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ScalaGen(genOpts: GenOpts) extends Generator(genOpts) {
val getter = Getter(hasPath, accessors, methodNames)
import methodNames._

val scalaUtil: ScalaUtil = new ScalaUtil(useBacksticks = genOpts.useBacksticks)
val scalaUtil: ScalaUtil = new ScalaUtil(useBackticks = genOpts.useBackticks)
import scalaUtil.{scalaIdentifier, getClassName}

def generate(objectType: ObjectType): GenResult = {
Expand Down Expand Up @@ -161,7 +161,7 @@ object ScalaGen {
// $COVERAGE-OFF$
def generate(filename: String,
showOut: Boolean = false,
useBacksticks: Boolean = false
useBackticks: Boolean = false
): GenResult = {
val file = new File("src/main/tscfg/" + filename)
val source = io.Source.fromFile(file).mkString.trim
Expand All @@ -187,7 +187,7 @@ object ScalaGen {
}

val genOpts = GenOpts("tscfg.example", className, j7 = false,
useBacksticks = useBacksticks)
useBackticks = useBackticks)

val generator = new ScalaGen(genOpts)

Expand Down
20 changes: 10 additions & 10 deletions src/main/scala/tscfg/generators/scala/ScalaUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import tscfg.util
* that need translation of the given identifier to make it valid Scala.
* Similarly, [[getClassName]] also does some related logic.
*
* With this flag set to true, both methods will change their logic to uses
* backsticks instead of replacing or removing the characters that would
* With this flag set to true, both methods will change their logic to use
* backticks instead of replacing or removing the characters that would
* make the resulting identifiers invalid.
*
* @param useBacksticks False by default
* @param useBackticks False by default
*/
class ScalaUtil(useBacksticks: Boolean = false) {
class ScalaUtil(useBackticks: Boolean = false) {
import ScalaUtil._

/**
Expand All @@ -25,28 +25,28 @@ class ScalaUtil(useBacksticks: Boolean = false) {
* - otherwise:
* - returns symbol if it is a valid java identifier
* - otherwise:
* if useBacksticks is true, enclose symbol in backsticks
* if useBackticks is true, encloses symbol in backticks
* otherwise, returns `javaGenerator.javaIdentifier(symbol)`
*/
def scalaIdentifier(symbol: String): String = {
if (scalaReservedWords.contains(symbol)) "`" + symbol + "`"
else if (noArgMethodInScope.contains(symbol)) symbol + "_"
else if (javaUtil.isJavaIdentifier(symbol)) symbol
else if (useBacksticks) "`" + symbol + "`"
else if (useBackticks) "`" + symbol + "`"
else javaUtil.javaIdentifier(symbol)
}

/**
* Returns a class name from the given symbol.
* If useBacksticks:
* This is basically capitalizing the first character that
* can be capitalized. If none, then a `U` is prepended.
* If useBackticks:
* This is basically capitalizing the first character that
* can be capitalized. If none, then a `U` is prepended.
* Otherwise:
* Since underscores are specially used in generated code,
* this method camelizes the symbol in case of any underscores.
*/
def getClassName(symbol: String): String = {
if (useBacksticks) {
if (useBackticks) {
val scalaId = scalaIdentifier(symbol)
val search = scalaId.zipWithIndex.find { case (c, _) c.toUpper != c }
search match {
Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/tscfg/generators/scala/ScalaMainSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,9 @@ class ScalaMainSpec extends Specification {
}

"issue30" should {
"generate as indicated for useBacksticks" in {
"generate as indicated for useBackticks" in {
val r = ScalaGen.generate("example/issue30.spec.conf",
useBacksticks = true)
useBackticks = true)

r.classNames === Set("ScalaIssue30Cfg", "`Foo-object`")
r.fields.size === 4
Expand Down
8 changes: 4 additions & 4 deletions src/test/scala/tscfg/scalaIdentifierSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ object scalaIdentifierSpec extends Specification {
}
}

"""scalaIdentifier with useBacksticks=true""" should {
val scalaUtil: ScalaUtil = new ScalaUtil(useBacksticks = true)
"""scalaIdentifier with useBackticks=true""" should {
val scalaUtil: ScalaUtil = new ScalaUtil(useBackticks = true)
import scalaUtil.scalaIdentifier

List("foo-bar", "foo:bar", "foo#bar").foldLeft(Fragments.empty) { (res, id) =>
res.append(s"""put non scala id with backsticks: "$id" -> "`$id`"""" in {
res.append(s"""put non scala id with backticks: "$id" -> "`$id`"""" in {
scalaIdentifier(id) must_== s"`$id`"
})
}

List("0", "1", "3").foldLeft(Fragments.empty) { (res, id) =>
res.append(s"""put literal with backsticks: "$id" -> "`$id`"""" in {
res.append(s"""put literal number with backticks: "$id" -> "`$id`"""" in {
scalaIdentifier(id) must_== s"`$id`"
})
}
Expand Down

0 comments on commit ac561c8

Please sign in to comment.