Reputation: 6159
I need to concatenate string and variable into a boolean variable in groovy, The case is like that:
I have a loop that looking for a pattern if the pattern exists I get a variable called cname that holds a certain value.
Now I want to use this value to create a new boolean parameter that contains the value of the cname parameter in the boolean parameter name.
This is what I tried:
boolean "trigger-${cname}" = true
boolean "trigger-"+${cname} = true
But it doesn't seem to work on a boolean. I want to be able to have N parameters (as much as cname times) with the value true. For example, if cname is (app, test, rest, .. N), then all the boolean parameters listed below will be true:
trigger-app
trigger-test
trigger-rest
trigger-N
Upvotes: 0
Views: 4831
Reputation: 42272
You cannot use quoted identifier to define a standard variable. Here is the part from Groovy Syntax documentation that explains it in details:
3. Identifiers
3.1. Normal identifiers
Identifiers start with a letter, a dollar or an underscore. They cannot start with a number.
A letter can be in the following ranges:
'a' to 'z' (lowercase ascii letter)
'A' to 'Z' (uppercase ascii letter)
'\u00C0' to '\u00D6'
'\u00D8' to '\u00F6'
'\u00F8' to '\u00FF'
'\u0100' to '\uFFFE'
Then following characters can contain letters and numbers.
Here are a few examples of valid identifiers (here, variable names):
def name def item3 def with_underscore def $dollarStart
But the following ones are invalid identifiers:
def 3tier def a+b def a#b
All keywords are also valid identifiers when following a dot:
foo.as foo.assert foo.break foo.case foo.catch
Source: https://groovy-lang.org/syntax.html#_normal_identifiers
However, when you define a Map
you can use quoted identifiers to access its key-values.
3.2. Quoted identifiers
Quoted identifiers appear after the dot of a dotted expression. For instance, the name part of the
person.name
expression can be quoted withperson."name"
orperson.'name'
. This is particularly interesting when certain identifiers contain illegal characters that are forbidden by the Java Language Specification, but which are allowed by Groovy when quoted. For example, characters like a dash, a space, an exclamation mark, etc.def map = [:] map."an identifier with a space and double quotes" = "ALLOWED" map.'with-dash-signs-and-single-quotes' = "ALLOWED" assert map."an identifier with a space and double quotes" == "ALLOWED" assert map.'with-dash-signs-and-single-quotes' == "ALLOWED"
As we shall see in the following section on strings, Groovy provides different string literals. All kind of strings are actually allowed after the dot:
map.'single quote' map."double quote" map.'''triple single quote''' map."""triple double quote""" map./slashy string/ map.$/dollar slashy string/$
There’s a difference between plain character strings and Groovy’s GStrings (interpolated strings), as in that the latter case, the interpolated values are inserted in the final string for evaluating the whole identifier:
def firstname = "Homer" map."Simpson-${firstname}" = "Homer Simpson" assert map.'Simpson-Homer' == "Homer Simpson"
Source: https://groovy-lang.org/syntax.html#_quoted_identifiers
If you want to make use of quoted identifiers, you will have to store all values in a map, something like:
def map = [:]
map."trigger-${cname}" = true
assert map."trigger-${cname}"
Upvotes: 2