Skip to content

Commit

Permalink
Merge pull request #83 from WIgor/master
Browse files Browse the repository at this point in the history
--target=<scala version> parameter
  • Loading branch information
SandroGrzicic committed Jul 13, 2014
2 parents de76662 + 2c6590f commit af78937
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import java.io.File
* @author Sandro Gržičić
*/

class Generator protected (sourceName: String, importedSymbols: Map[String, ImportedSymbol], generateJsonMethod: Boolean) {
class Generator protected (sourceName: String, importedSymbols: Map[String, ImportedSymbol], generateJsonMethod: Boolean,
targetScalaVersion: Option[String]) {
import Generator._

protected val imports = mutable.ListBuffer[String]()
Expand Down Expand Up @@ -407,9 +408,21 @@ class Generator protected (sourceName: String, importedSymbols: Map[String, Impo

// *** companion object
out.append(indent0).append("object ").append(name).append(" {\n")
.append(indent1).append("@reflect.BeanProperty val defaultInstance = new ").append(name).append("()\n")
.append(indent1)

out.append("\n")
val ge210 = targetScalaVersion.exists {
version =>
val values = version.split("\\.")
if (values.length >= 2 && (values(0).toInt > 2 || (values(0).toInt == 2 && values(1).toInt >= 10))) true
else false
}

if (ge210)
out.append("@beans.BeanProperty val defaultInstance = new ")
else
out.append("@reflect.BeanProperty val defaultInstance = new ")

out.append(name).append("()\n").append("\n")

// parseFrom()
out.append(indent1).append("def parseFrom(data: Array[Byte]): ").append(name)
Expand Down Expand Up @@ -573,8 +586,9 @@ object Generator {
/**
* Returns a valid Scala class.
*/
def apply(tree: List[Node], sourceName: String, importedSymbols: Map[String, ImportedSymbol], generateJsonMethod: Boolean): ScalaClass = {
new Generator(sourceName, importedSymbols, generateJsonMethod).generate(tree)
def apply(tree: List[Node], sourceName: String, importedSymbols: Map[String, ImportedSymbol], generateJsonMethod: Boolean,
targetScalaVersion: Option[String]): ScalaClass = {
new Generator(sourceName, importedSymbols, generateJsonMethod, targetScalaVersion).generate(tree)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ object ScalaBuff {
outputEncoding: Charset = defaultCharset,
verbose: Boolean = false,
extraVerbose: Boolean = false,
generateJsonMethod: Boolean = false)
generateJsonMethod: Boolean = false,
targetScalaVersion: Option[String] = None
)

val defaultSettings = Settings()

Expand All @@ -31,14 +33,14 @@ object ScalaBuff {
def apply(file: File)(implicit settings: Settings = defaultSettings) = {
val tree = parse(file)
val symbols = processImportSymbols(tree)
Generator(tree, file.getName, symbols, settings.generateJsonMethod)
Generator(tree, file.getName, symbols, settings.generateJsonMethod, settings.targetScalaVersion)
}

/**
* Runs ScalaBuff on the specified input String and returns the output Scala class.
*/
def fromString(input: String, generateJsonMethod: Boolean = false) = {
Generator(Parser(input), "", Map(), generateJsonMethod)
def fromString(input: String, generateJsonMethod: Boolean = false, targetScalaVersion: Option[String] = None) = {
Generator(Parser(input), "", Map(), generateJsonMethod, targetScalaVersion)
}

/**
Expand Down Expand Up @@ -230,6 +232,10 @@ object ScalaBuff {
case ue: UnsupportedEncodingException => Strings.UNSUPPORTED_OUTPUT_ENCODING + outputEncoding
}

case s if s startsWith "--target=" =>
val targetVersion = s.substring("--target=".length)
settings.copy(targetScalaVersion = Some(targetVersion))

case "--generate_json_method" =>
settings.copy(generateJsonMethod = true)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ object UpdateTestResources extends App {
// for now, this is hard-coded.
val importedSymbols = Map("PackageTest" -> ImportedSymbol("nested", isEnum = false))

val generated = Generator(parsed, file.getName, importedSymbols, generateJsonMethod = true)
val generated = Generator(parsed, file.getName, importedSymbols, generateJsonMethod = true, None)
val generatedPath = testDir + generated.path + generated.file + ".scala"

new File(testDir + generated.path).mkdirs()
Expand Down

0 comments on commit af78937

Please sign in to comment.