telosys-tools-bricks / telosys-cli

Telosys CLI - Command Line Interface
https://www.telosys.org/
GNU Lesser General Public License v3.0
166 stars 23 forks source link

Java arraylist and velocity arraylist behave differently #27

Open developer-sunnywu opened 6 months ago

developer-sunnywu commented 6 months ago

Description:

I try to use custom function return arraylist and here is my function in java


    private static Map<String, List<String>> map = Map.of(
            "Long", Arrays.asList("null", "1", "2"),
            "Boolean", Arrays.asList("null", "True", "False"),
            "BigDecimal", Arrays.asList("null", "BigDecimal.ZERO", "BigDecimal.ONE"),
            "LocalTime", Arrays.asList("null", "LocalTime.of(0, 0, 0)", "LocalTime.of(1, 1, 1)")
    );

    public static ArrayList<String> getValuesByFieldName(String filedName) {
        return new ArrayList<>(map.get(filedName));
    }
}

And there is my velocity.vm

#set($FieldUtil = $loader.loadClass("com.example.codegenerator.telosys.util.FieldUtil"))
#set($myJavaList = $FieldUtil.getValuesByFieldName('Long'))
$myJavaList.class.name
$myJavaList
### Will throw no attribute '['
$myJavaList[0]

#set( $mylist = [1, 2, 3, "A", true, 65.78] )
$mylist.class.name
$mylist
### It is okay here
$mylist[0]

I find that even though they have same type(java.util.ArrayList), the myJavaList not work

Version use: telosys-cli-4.1.1-001

Expected behaviour I expect they can both use the same operator which I can access the element using bracket notation here. Hope I specify the problem clearly

l-gu commented 6 months ago

You don't need a Java class for that You can just use a map of lists with Velocity language See https://doc.telosys.org/templates/code-snippets#maps-of-lists

developer-sunnywu commented 6 months ago

Thank you! I will try your suggested solution. But the problem here we should expect they have same behavior?