telosys-cli
telosys-cli copied to clipboard
Velocity function to camelCase a string
Hello and thanks for this wonderful tool.
I looked around for a function that can convert a string_with_underscores
to a camelCase format stringWithUnderscores
, but I cannot seem to find such a function.
Is there such a function? How easy it would be to submit a pr to add it?
Why, you'd ask?
Because if field name have a prefix (personal_computer_model
, personal_computer_ram
, etc) matching the catalog name (personal_computer
), then when auto-generating an entity, there's no need to have the properties named:
personalComputerModel
personalComputerRam
but rather, just:
model
ram
and the class would be named: personalComputer
.
Makes sense? :)
Telosys is extensible by design, so the simplest way to do this kind of conversion is to create your own specific function and to use it in your templates.
Here's how you can do that :
-
create a Java class with your specific conversion method (the method can be static or not)
-
compile your Java class an put the ".class" file in the "classes" folder (in the templates bundle) or create a ".jar" file and put it in the "lib" folder (in the templates bundle)
-
in the template file (".vm") use the "$loader" object to load your Java class and call the specific method See "$loader" reference here : http://www.telosys.org/templates-doc/objects/loader.html
Static method example
#set( $Math = $loader.loadClass("java.lang.Math")
## use the static methods of this class
$Math.random()
Standard method example
## create an instance of StringBuilder and put it in the context with #set
#set( $strBuilder = $loader.newInstance('java.lang.StringBuilder') )
## use the instance
$strBuilder.append('aa')
## create new instance of a specific class
#set( $tool = $loader.newInstance('MyTool') )
You can find class loading examples in the "advanced-templates-samples" templates bundle : https://github.com/telosys-templates-v3/advanced-templates-samples-T300, See templates "loader_examples.vm" and "loader2_examples.vm" (java classes sources are also provided in the bundle in "classes" folder)
Is there any resources we can reference? Cannot load the class after placing in the lib or templates folder
Is there any resources we can reference? Cannot load the class after placing in the lib or templates folder
FYI, if there is package wrap for the class, the file structure need to be followed. e.g com.example.Util Then, the file should be structure like com->example->Util.class (under classes folder)
An example of specific Java classes is available in this bundle of templates : https://github.com/telosys-templates/advanced-templates-samples/tree/master/classes
Please don't use issues to ask questions (use StackOverflow instead).