Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
capture java and scala wrapper supporting methods in proper classes
to facilitate validation at compile time
- Loading branch information
Showing
10 changed files
with
274 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
src/main/resources/codeTemplates/ | ||
### Scala template | ||
*.class | ||
*.log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package tscfg.codeTemplates; | ||
|
||
/** | ||
* Captures various snippets to be included in the generated wrapper. | ||
* This is not used as compile code in the generator itself but the | ||
* text of this file is retrieved as a resource at runtime. | ||
* Capturing this as code helps with validation at compile time. | ||
*/ | ||
public class JavaCodeTemplates { | ||
|
||
/////////////////////////////////////////////////////////////////////// | ||
// definition of methods used to access list's elements of basic type: | ||
|
||
//<$_bln> | ||
private static java.lang.Boolean $_bln(com.typesafe.config.ConfigValue cv) { | ||
java.lang.Object u = cv.unwrapped(); | ||
if (cv.valueType() != com.typesafe.config.ConfigValueType.BOOLEAN || | ||
!(u instanceof java.lang.Boolean)) throw $_expE(cv, "boolean"); | ||
return (java.lang.Boolean) u; | ||
} | ||
//</$_bln> | ||
|
||
//<$_dbl> | ||
private static java.lang.Double $_dbl(com.typesafe.config.ConfigValue cv) { | ||
java.lang.Object u = cv.unwrapped(); | ||
if (cv.valueType() != com.typesafe.config.ConfigValueType.NUMBER || | ||
!(u instanceof java.lang.Number)) throw $_expE(cv, "double"); | ||
return ((java.lang.Number) u).doubleValue(); | ||
} | ||
//</$_dbl> | ||
|
||
//<$_int> | ||
private static java.lang.Integer $_int(com.typesafe.config.ConfigValue cv) { | ||
java.lang.Object u = cv.unwrapped(); | ||
if (cv.valueType() != com.typesafe.config.ConfigValueType.NUMBER || | ||
!(u instanceof java.lang.Integer)) throw $_expE(cv, "integer"); | ||
return (java.lang.Integer) u; | ||
} | ||
//</$_int> | ||
|
||
//<$_lng> | ||
private static java.lang.Long $_lng(com.typesafe.config.ConfigValue cv) { | ||
java.lang.Object u = cv.unwrapped(); | ||
if (cv.valueType() != com.typesafe.config.ConfigValueType.NUMBER || | ||
!(u instanceof java.lang.Long) && !(u instanceof java.lang.Integer)) throw $_expE(cv, "long"); | ||
return ((java.lang.Number) u).longValue(); | ||
} | ||
//</$_lng> | ||
|
||
//<$_str> | ||
private static java.lang.String $_str(com.typesafe.config.ConfigValue cv) { | ||
return java.lang.String.valueOf(cv.unwrapped()); | ||
} | ||
//</$_str> | ||
|
||
// $_siz: since there's no something like cv.getBytes() nor is SimpleConfig.parseBytes visible, | ||
// use ConfigFactory.parseString: | ||
//<$_siz> | ||
private static java.lang.Long $_siz(com.typesafe.config.ConfigValue cv) { | ||
java.lang.Object u = cv.unwrapped(); | ||
if (cv.valueType() == com.typesafe.config.ConfigValueType.NUMBER || | ||
(u instanceof java.lang.Long) || (u instanceof java.lang.Integer)) | ||
return ((java.lang.Number) u).longValue(); | ||
if (cv.valueType() == com.typesafe.config.ConfigValueType.STRING) { | ||
return com.typesafe.config.ConfigFactory.parseString("s = " + '"' + u + '"').getBytes("s"); | ||
} | ||
throw $_expE(cv, "size"); | ||
} | ||
//</$_siz> | ||
|
||
//<$_expE> | ||
private static java.lang.RuntimeException $_expE(com.typesafe.config.ConfigValue cv, java.lang.String exp) { | ||
java.lang.Object u = cv.unwrapped(); | ||
return new java.lang.RuntimeException(cv.origin().lineNumber() | ||
+ ": expecting: " +exp + " got: " + (u instanceof java.lang.String ? "\"" +u+ "\"" : u)); | ||
} | ||
//</$_expE> | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
tscfg.version = 0.9.91 | ||
tscfg.version = 0.9.92 |
88 changes: 88 additions & 0 deletions
88
src/main/scala/tscfg/codeTemplates/ScalaCodeTemplates.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package tscfg.codeTemplates | ||
|
||
/** | ||
* Captures various snippets to be included in the generated wrapper. | ||
* This is not used as compile code in the generator itself but the | ||
* text of this file is retrieved as a resource at runtime. | ||
* Capturing this as code helps with validation at compile time. | ||
*/ | ||
object ScalaCodeTemplates { | ||
|
||
/////////////////////////////////////////////////////////////////////// | ||
// definition of methods used to access list's elements of basic type: | ||
|
||
//<$_bln> | ||
private def $_bln(cv:com.typesafe.config.ConfigValue): scala.Boolean = { | ||
val u: Any = cv.unwrapped | ||
if ((cv.valueType != com.typesafe.config.ConfigValueType.BOOLEAN) || | ||
!u.isInstanceOf[java.lang.Boolean]) throw $_expE(cv, "boolean") | ||
u.asInstanceOf[java.lang.Boolean].booleanValue() | ||
} | ||
//</$_bln> | ||
|
||
//<$_dbl> | ||
private def $_dbl(cv:com.typesafe.config.ConfigValue): scala.Double = { | ||
val u: Any = cv.unwrapped | ||
if ((cv.valueType != com.typesafe.config.ConfigValueType.NUMBER) || | ||
!u.isInstanceOf[java.lang.Number]) throw $_expE(cv, "double") | ||
u.asInstanceOf[java.lang.Number].doubleValue() | ||
} | ||
//</$_dbl> | ||
|
||
//<$_int> | ||
private def $_int(cv:com.typesafe.config.ConfigValue): scala.Int = { | ||
val u: Any = cv.unwrapped | ||
if ((cv.valueType != com.typesafe.config.ConfigValueType.NUMBER) || | ||
!u.isInstanceOf[Integer]) throw $_expE(cv, "integer") | ||
u.asInstanceOf[Integer] | ||
} | ||
//</$_int> | ||
|
||
//<$_lng> | ||
private def $_lng(cv:com.typesafe.config.ConfigValue): scala.Long = { | ||
val u: Any = cv.unwrapped | ||
if ((cv.valueType != com.typesafe.config.ConfigValueType.NUMBER) || | ||
!u.isInstanceOf[java.lang.Integer] && !u.isInstanceOf[java.lang.Long]) throw $_expE(cv, "long") | ||
u.asInstanceOf[java.lang.Number].longValue() | ||
} | ||
//</$_lng> | ||
|
||
//<$_str> | ||
private def $_str(cv:com.typesafe.config.ConfigValue) = { | ||
java.lang.String.valueOf(cv.unwrapped()) | ||
} | ||
//</$_str> | ||
|
||
//<$_siz> | ||
private def $_siz(cv:com.typesafe.config.ConfigValue): scala.Long = { | ||
val u: Any = cv.unwrapped | ||
if (cv.valueType() == com.typesafe.config.ConfigValueType.NUMBER || | ||
u.isInstanceOf[scala.Long] || u.isInstanceOf[scala.Int]) | ||
u.asInstanceOf[java.lang.Number].longValue() | ||
else if (cv.valueType() == com.typesafe.config.ConfigValueType.STRING) { | ||
com.typesafe.config.ConfigFactory.parseString("s = " + '"' + u + '"').getBytes("s") | ||
} | ||
else throw $_expE(cv, "size") | ||
} | ||
//</$_siz> | ||
|
||
//<$_expE> | ||
private def $_expE(cv:com.typesafe.config.ConfigValue, exp:java.lang.String) = { | ||
val u: Any = cv.unwrapped | ||
new java.lang.RuntimeException(cv.origin.lineNumber + | ||
": expecting: " + exp + " got: " + | ||
(if (u.isInstanceOf[java.lang.String]) "\"" + u + "\"" else u)) | ||
} | ||
//</$_expE> | ||
|
||
//<$_require> | ||
def $_require[T](parentPath: java.lang.String, path: java.lang.String)(get: ⇒ T): T = { | ||
import com.typesafe.config.ConfigException.Missing | ||
try get | ||
catch { | ||
case e: Missing ⇒ throw if (parentPath.isEmpty) e else new Missing(parentPath + path, e) | ||
} | ||
} | ||
//</$_require> | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package tscfg | ||
|
||
import java.util.regex.Pattern | ||
|
||
import scala.util.control.NonFatal | ||
|
||
package object codeTemplates { | ||
private val beginTemplatePattern = Pattern.compile("\\s*//<([^>]+)>.*$") | ||
|
||
private val javaMap = getMap("codeTemplates/JavaCodeTemplates") | ||
private val scalaMap = getMap("codeTemplates/ScalaCodeTemplates") | ||
|
||
def getJavaTemplate(key: String): String = javaMap(key) | ||
|
||
def getScalaTemplate(key: String): String = scalaMap(key) | ||
|
||
private def getMap(templateName: String): Map[String, String] = try { | ||
//println(s"codeTemplates.getMap $templateName") | ||
val map = collection.mutable.HashMap[String, String]() | ||
val is = getClass.getClassLoader.getResourceAsStream(templateName) | ||
assert(is != null) | ||
val source = io.Source.fromInputStream(is, "utf-8") | ||
assert(source != null) | ||
var key: String = null | ||
val template = new StringBuilder | ||
for (line ← source.getLines()) { | ||
if (key == null) { | ||
val m = beginTemplatePattern.matcher(line) | ||
if (m.find) { | ||
key = m.group(1) | ||
} | ||
} | ||
else if (line.contains("//</" + key + ">")) { | ||
map.update(key, template.toString) | ||
key = null | ||
template.setLength(0) | ||
} | ||
else template.append(line).append("\n") | ||
} | ||
is.close() | ||
map.toMap | ||
} | ||
catch { | ||
case NonFatal(ex) ⇒ | ||
throw new RuntimeException("Unexpected exception. Please report this bug.", ex) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.