telosys-cli
telosys-cli copied to clipboard
Java arraylist and velocity arraylist behave differently
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
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
Thank you! I will try your suggested solution. But the problem here we should expect they have same behavior?