grails expression - grails

Wondering if there is any thing wrong in this expression
${message(code:'${domianObject.paymentMode }.label', default:'Test')}
All I am doing is iterating over list of domain object and trying to replace value of an attribute using resource bundle.
Above code is resulting in weird parsing error
expecting ''', found '\r' # line 95, column 132.
${domianObject.paymentMode })
^
1 error
at java.lang.Thread.run(Thread.java:662)
Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:

I think that's a simple typo. Not domianObject but domainObject. Isn't it?

Nesting of gstring expressions can be a bit hairy, but once you're inside a ${} you can use any Groovy expression so instead of the inner gstring you can say
${message(code:(domainObject.paymentMode + '.label'), default:'Test')}
using normal string concatenation.

Related

Cannot use index variable in thymeleaf template after 100th use on an array

When I try to use the same variable as an array's index 101th times (even over multiple renderings of my view), I have an exception on evaluating the expression. I have to restart my server to evaluate the expression again (until the 101th evaluation).
<th:block th:with="array=${new String[]{'item1'}}, index=0">
<th:block th:each="i : ${#numbers.sequence( 1, 101, 1)}">
[[${i}]]:[[${array[index]}]]
</th:block>
</th:block>
Result is :
1:item1 2:item1 [...] 100:item1 101:
Exception is :
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "array[index]" (template: "test.html" - line 10, col 13)
at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:290)
Caused by: java.lang.IllegalStateException: Failed to instantiate CompiledExpression
at org.springframework.expression.spel.standard.SpelCompiler.compile(SpelCompiler.java:111)
Caused by: java.lang.VerifyError: (class: spel/Ex3, method: getValue signature: (Ljava/lang/Object;Lorg/springframework/expression/EvaluationContext;)Ljava/lang/Object;) Expecting to find integer on stack
at java.lang.Class.getDeclaredConstructors0(Native Method)
Environment is :
thymeleaf version:'3.0.11.RELEASE'
thymeleaf-spring5 version:'3.0.11.RELEASE'
Tomcat 9.0.36
JDK 1.8.0_74
What's going on ?
The error snippet here mentions that Spring is attempting to compile the expression:
SpelCompiler.compile()
And, as you note, the problem is very specifically when the code attempts more than 100 array index accesses - even if the index variable never changes:
array[index]
It's interesting to note that I did not get this error when I hard-coded the index value - for example:
array[0]
I know of 2 ways around the problem:
Option 1
Configure SpEL to not use expression compilation. How you do this depends on your specific config set-up, but the setting is:
spring.expression.compiler.mode=off
I tried using mode immediate and also mode mixed - but neither worked. Only mode off.
Option 2
Avoid using an array with an index variable - for example there is no problem iterating over a list (or an array) like this, where the list contains more than 100 objects:
<th:block th:each="i,iterStat : ${myList}">
[[${iterStat.index}]]:[[${i}]]
</th:block>
Whether this option works for you probably depends on the wider context of what your code is doing. So this may not be an option.
Either way, I was surprised that the mixed compilation mode did not work. The documentation does note, however, that there are limitations to what the compiler can handle.
Just as an observation: I am assuming that you want to use the [[...]] syntax in your template, as opposed to something more like this:
<th:block th:each="i,iterStat : ${myList}">
<span th:text="${iterStat.index} + ':' + ${i}"></span>
</th:block>
(Doing things this way does not avoid the compilation problem, though.)

In Bazel, is it possible to use a function output as input to a load statement?

In Bazel, is it possible to use simple functions and variables as input to a load statement?
For example:
my_workspace = "a" + "b"
load(my_workspace, "foo")
load(my_workspace, "bar")
WARNING: Target pattern parsing failed.
ERROR: error loading package 'loadtest/simple': malformed load statements
The exact error message might have changed with version, I'd see:
syntax error at 'my_workspace': expected string literal
but no, you cannot use anything but string literal as per docs:
Use the load statement to import a symbol from an extension.
...
Arguments must be string literals (no variable)...

How should I write an OptaPlanner Planning Entity PlanningVariable Annotation in Grails?

I tried to convert my OptaPlanner code from Java to Grails. Everything else is fine except I'm stuck when I changed the Planning Entity class into a Groovy file. Then Error message with this would show:
startup failed: F:\Users\Administrator\Documents\workspace-ggts-3.2.0.RELEASE\spa\src\groovy\optaplanner\domain\AllocationEntity.groovy: 15: Annotation list attributes must use Groovy notation [el1, el2] in #org.optaplanner.core.api.domain.variable.PlanningVariable # line 15, column 48. able(valueRangeProviderRefs = {"projects ^ 1 error
And my Intellij IDEA also would prompt an error message when I hover over the line #PlanningVariable(valueRangeProviderRefs = {"projectsRange"}) with red warning highlight under {"projectsRange"}, and the error message is this:
Cannot assign 'Class' to 'String[]'
I wish to use Groovy instead of Java for the GORM feature to query database. But how can I fix this error so I can use the Planning Entity as a Groovy class?
Most Java code is valid Groovy code, but there are a few exceptions, mostly when dealing with curly braces. Closures are defined in Groovy as a code block inside of curly braces, e.g.
def foo = {
...
}
so other uses of curly braces will confuse the Groovy parser. In most cases you just use regular braces instead. In this case your annotation list should be
#PlanningVariable(valueRangeProviderRefs = ["projectsRange"])

Determining ANTLR version to use, or converting between?

I want to take the .g files from Apache Hive and build a parser (targeting JavaScript) -- initially, as just a way to validate user-input Hive queries. The files I'm using come from apache-hive-1.0.0-src\ql\src\java\org\apache\hadoop\hive\ql\parse from the Hive tgz: HiveLexer.g, HiveParser.g, FromClauseParser.g, IdentifiersParser.g, SelectClauseParser.g.
I see no indication within the grammar files which version of ANTLR to use, so I've tried running antlr (from apt-get pccts), antlr3 and antlr4. they all throw errors of some sort, so I have no clue which one to run or if I can (or need to) convert the .g files between versions.
The errors I'm getting are as follows:
antlr -Dlanguage=JavaScript HiveParser.g (looks like it doesn't support JS anyway):
warning: invalid option: '-Dlanguage=JavaScript'
HiveParser.g, line 17: syntax error at "grammar" missing { QuotedTerm PassAction ! \< \> : }
HiveParser.g, line 17: syntax error at "HiveParser" missing { QuotedTerm PassAction ! \< \> : }
HiveParser.g, line 17: syntax error at ";" missing Eof
HiveParser.g, line 28: lexical error: invalid token (text was ',')
antlr3 -Dlanguage=JavaScript HiveParser.g:
error(10): internal error: Exception FromClauseParser.g:302:85: unexpected char: '-'#org.antlr.grammar.v2.ANTLRLexer.nextToken(ANTLRLexer.java:347): unexpected stream error from parsing FromClauseParser.g
error(150): grammar file FromClauseParser.g has no rules
error(100): FromClauseParser.g:0:0: syntax error: assign.types: <AST>:299:68: unexpected AST node: ->
error(100): FromClauseParser.g:0:0: syntax error: define: <AST>:299:68: unexpected AST node: ->
error(106): SelectClauseParser.g:151:18: reference to undefined rule: tableAllColumns
antlr4 -Dlanguage=JavaScript HiveParser.g:
warning(202): HiveParser.g:30:0: tokens {A; B;} syntax is now tokens {A, B} in ANTLR 4
error(50): HiveParser.g:636:34: syntax error: '->' came as a complete surprise to me while looking for rule element
error(50): HiveParser.g:636:37: syntax error: '^' came as a complete surprise to me
error(50): HiveParser.g:638:50: syntax error: '->' came as a complete surprise to me while looking for rule element
error(50): HiveParser.g:638:53: syntax error: '^' came as a complete surprise to me
The antlr3 error referencing #org.antlr.grammar.v2.ANTLRLexer.nextToken seems suspect. Is it using the v2 lexer instead of v3? If so, maybe v3 is what I should target, but it's somehow not hitting it?
Or is this not an issue with versioning and instead with invocation? Or is Hive built in a way that provides additional files needed?
According to Hive source code, they use ANTLR 3.4. But before you start remove the last string from FromClauseParser.g
//------------------------------------------------------------------------

Double closure fails in GSP

In a GSP file I write something like this:
${tgs.singleGameSheets.find{it.matchnumber==1}.awayPlayer.fullname()}
But I receive the following error:
org.codehaus.groovy.control.MultipleCompilationErrorsException:
startup failed:
C__STS_Projekte_TischtennisManager_grails_app_views_league__showGameSheet_gsp:
49:expecting '}', found ')' # line 49, column 134.
heets.find{it.matchnumber==1 })
The problem seems to be the double closure as I've found a bug report here.
Unfortunately the solution from the bugreport with the %= and % at the beginning and the end of the tag is not working for me.
Are there any other workarounds or solutions for this double closure problem?
I'm using Grails 1.3.7.
You may have to split this up in to two lines.
Try assigning the find results to a separate var first
<% def r = tgs.singleGameSheets.find{it.matchnumber==1} %>
${r*.awayPlayer.fullname()}
I would recommend firstly to do this sort of data processing in the controller and hand data that is as well prepared as possible down to the view.
If you are unable to do that, I would recommend trying to use parenthesis:
${tgs.singleGameSheets.find{it.matchnumber==1}.awayPlayer.fullname()}
becomes
${(tgs.singleGameSheets.find{it.matchnumber==1}.awayPlayer.fullname())}
That has worked for me on past occasions where I had to do ${(someCollection.findAll { someClause })}

Resources