Telosys doc
WebsiteTwitterLinkedInNews
  • Telosys documentation
  • Telosys CLI
    • Installation on Linux
    • Installation on Windows
    • CLI configuration
    • CLI commands
    • Getting started
  • Telosys with VSCode
    • Extension installation
    • VSCode settings
    • Telosys terminal in VSCode
    • Telosys editor configuration
  • Telosys with JetBrains IDE
  • Project configuration
  • Project variables
  • Project databases
  • Models and entities
    • Model structure
    • Entity
    • Attribute / Link
    • Annotations
    • Tags
    • Examples
    • Model creation
    • Model installation
    • Models management
  • Bundles of templates
    • Bundle structure
    • Bundle creation
    • Bundles installation
    • Bundles management
    • Velocity language
    • Velocity directives
    • Velocity object types
    • Telosys directives
    • Telosys variables
    • Telosys objects
    • Code snippets
  • Code generation
    • "gen" command
    • "genb" command
  • Target languages
    • C++
    • C#
    • Golang
    • Java
    • JavaScript
    • Kotlin
    • PHP
    • Python
    • Scala
    • TypeScript
  • Target databases (SQL)
    • Predefined rules
    • Specific rules
    • Type size and precision
  • Telosys with Git
    • GitHub usage
    • Install with Git
    • Publish with Git
  • Support the project
  • How to contribute
  • Sponsors
  • IDE and editors
  • Telosys 3 Eclipse plugin
    • Eclipse plugin installation
    • Eclipse customization
    • Telosys 3 database model
Powered by GitBook
On this page
  • Iterate over all entities defined in the model
  • Iterate over entity attributes
  • Iterate over entity links
  • Execute a ".vm" file located in a model folder
  • Get the class of an object
  • Generate a file only once
  • Maps of lists
  1. Bundles of templates

Code snippets

Iterate over all entities defined in the model

#foreach( $e in $model.allEntites )
		...
#end

Iterate over entity attributes

#foreach( $attribute in $entity.attributes )
		...
#end

#foreach( $attribute in $entity.keyAttributes)
		...
#end

#foreach( $attribute in $entity.nonKeyAttributes)
		...
#end

Iterate over entity links

#foreach( $link in $entity.links )
		...
#end

Execute a ".vm" file located in a model folder

The function "$fn.fileFromModel(fileName)" returns an instance of "file" located in the current model. If the file exists, its content is loaded and evaluated with "#evaluate" directive.

#set( $file = $fn.fileFromModel("model-init.vm") )
#if($file.exists())#evaluate($file.loadContent())#end

Get the class of an object

As all Velocity references ( "$xxx" ) are references of Java objects, sometimes it could be useful to know the object's class. To do this, just use "class.name" (full name with package) or "class.simpleName" (only the class name without package)

#set( $v = 12 )
$v.class.name 
$v.class.simpleName 

#set( $s = "abc" )
$s.class.name 
$s.class.simpleName 

$entity.class.name 

$now.class.simpleName 

Generate a file only once

Sometimes it can be useful to make sure you only generate a file once to avoid overwriting manual changes after project bootstrapping. Since Telosys 3.3.0 you can do that with #cancel directive.

##--- NB : do not rewrite the file if it already exists
#if($target.outputFileExists() )#cancel("File already exists")#end

Maps of lists

With Velocity, you can create a "map" of "anything". A map can contain any object, even lists.

## --- Example 1 :
#set( $list1 = [ 0, 1, 2 ] )
#set( $list2 = [ "", "A", "B", "C", "D" ] )
## A map of lists :
#set( $m = {"k1" : $list1 , "k2" : $list2} )

## --- Example 2 (literal):
#set( $m2 = {
  "int" : [ 100, 200, 300 ] , 
  "str" : [ "AAA", "BB", "CCC", "D" ] 
  } )

## --- Usage 
$m["k2"]
$m["k2"][2]

$m2["int"][1]

PreviousTelosys objectsNextCode generation

Last updated 1 month ago