telosys-cli icon indicating copy to clipboard operation
telosys-cli copied to clipboard

Velocity function to camelCase a string

Open ndawod opened this issue 4 years ago • 4 comments

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? :)

ndawod avatar Aug 13 '20 23:08 ndawod

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 :

  1. create a Java class with your specific conversion method (the method can be static or not)

  2. 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)

  3. 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)

l-gu avatar Aug 18 '20 12:08 l-gu

Is there any resources we can reference? Cannot load the class after placing in the lib or templates folder

developer-sunnywu avatar Apr 21 '24 17:04 developer-sunnywu

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)

developer-sunnywu avatar Apr 25 '24 19:04 developer-sunnywu

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).

l-gu avatar Apr 29 '24 20:04 l-gu