Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Velocity function to camelCase a string #22

Open
ndawod opened this issue Aug 13, 2020 · 4 comments
Open

Velocity function to camelCase a string #22

ndawod opened this issue Aug 13, 2020 · 4 comments

Comments

@ndawod
Copy link

ndawod commented Aug 13, 2020

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

@l-gu
Copy link
Member

l-gu commented Aug 18, 2020

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)

@developer-sunnywu
Copy link

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

@developer-sunnywu
Copy link

developer-sunnywu commented Apr 25, 2024

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)

@l-gu
Copy link
Member

l-gu commented Apr 29, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants