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

Java arraylist and velocity arraylist behave differently

Open developer-sunnywu opened this issue 10 months ago • 2 comments

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

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

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

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

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

developer-sunnywu avatar Apr 30 '24 06:04 developer-sunnywu