Accessing config variables stored in maps by key - grails

I have a variable in groovy like below:
project.Map
{
time.'1 1 * ?' = ['T1']
time.'2 1 * ?' = ['T2']
templates.'T1' = ['Z','X','Y']
templates.'T2' = ['Q']
}
Sorry but I am new to groovy ,when i try to access the individual
variable values in project.map how do i access them
i tried something like below
log.info(grailsApplication.config.project.Map.time[1])
log.info(grailsApplication.config.project.Map.get('time.'2 1 * ?'' ))
log.info(grailsApplication.config.project.Map.get('time[0]' ))
log.info(grailsApplication.config.project.Map.time.get('1 1 * ?'))
but they all print null value or object references.how do i access values for
time and templates both within a for loop and without it.

please see http://grails.org/doc/latest/guide/conf.html#config for the ways the config is allowed to nest. your outer syntax is especially mentioned to not be allowed:
However, you can't nest after using the dot notation. In other words, this won't work:
// Won't work!
foo.bar {
hello = "world"
good = "bye"
}
You have to write it as
project { Map { ... } }
The inner dotted parts (with the assignment) are ok (according to the doc)

Related

stumped by simple groovy variable comparison

I have a groovy script (a Jenkins pipeline) and a simple variable comparison is not working like I expect. I have a class defined to hold some constants, like this:
class email_when {
static final int ON_FAILURE = 0
static final int ALWAYS = 1
}
At a certain point in the script I set an environment variable to one of these states, like this:
env.EMAIL_WHEN = email_when.ALWAYS
Then later, I check the value. This check is always failing and I don't understand why.
echo ("email when = "+env.EMAIL_WHEN+ " always = "+email_when.ALWAYS);
if (env.EMAIL_WHEN == email_when.ALWAYS)
{
echo ("Send email.")
}
else
{
echo ("NO EMAIL")
}
So this always prints
email when = 1 always = 1
NO EMAIL
I don't understand why?
I thought maybe it was some sort of object/value comparison thing? Although I am directly setting env.EMAIL_WHEN to email_when.ALWAYS.
I tried this and it still did the same thing:
if (env.EMAIL_WHEN.equals(email_when.ALWAYS))
Can anyone explain what I am missing?
Thanks!
Everything in env map automatically converted to String
so 1 != '1'

Parse Arguments to Lua File

I have File: data.lua
#! /usr/bin/env lua
local a = {
b = {
c = {
version = "z.y"
},
d = {
version = "z.w"
},
getcversion = function ( self )
print( self.c.version )
end
}
}
Now I need to 'getcversion()' Function..
a.b:getcversion()
The problem is, I need to call it from the outside:
data.lua "a.b:getcversion()"
I tried everything that I could, but I couldn't solve this..
Does any one knows how I could call 'getcversion()' ?
Thanks in Advance,
Regards
You'll need to add something like the following to your script:
load(arg[1], "<string>", "t", {a = a})()
load will load the content of the passed parameter (arg[1]) and will return the function that will execute that code in a specific environment (provided as {a = a} table), as you need to pass the values of local variables to your code (Lua 5.2+).
Keep in mind that this will allow the caller to pass arbitrary Lua code to your script, which may be a security issue.
The error handling is left as the exercise for the reader.
Add return a to the end of data.lua to turn it into a module, and then:
$ lua -e 'a = require("data"); print(a.b:getcversion())'
z.y

Lua: Get table value within itself [duplicate]

This question already has answers here:
Use table key inside same (anonymous) table
(2 answers)
Closed 5 years ago.
So I'm trying something which i think should be very easy, but I just can't get it to work...
Basically what I'm trying to do is:
myTable = {
a = 1,
b = a + 1
}
This is not working, I get the error that "a" is nil. Reasonable.
What i have already tried is
myTable = {
a = 1,
b = myTable.a + 1
}
and
myTable = {
a = 1,
b = self.a + 1
}
But it gives me the error me that "myTable"/"self" is nil.
I got the feeling that the solution is rather simple but i couldn't find it out by myself and google wasn't that helpful as well.
There's no way of doing this in one statement (at least not without calling any functions or using metatables). That's because in a statement like foo = bar, the foo variable isn't assigned until the bar expression has been evaluated.
In your second example, the myTable variable isn't assigned until the closing curly brace, so the myTable in myTable.a + 1 is treated as an unnassigned global variable, and gets a value of nil. The self in your third example is the same, only you don't try to assign anything to it later. (In Lua, self is only special inside functions written with the colon syntax.)
To do what you want to do, you have to do something like this:
myTable = {
a = 1
}
myTable.b = myTable.a + 1
Or this:
local a = 1
myTable = {
a = a,
b = a + 1
}

Formatting lua table for a parse $in query that is also a table

I am using corona (lua) with parse.com and I have hit a problem constructing an $in query using values from another table / array.
My code is a little like this:
local usersToFetch = {}
table.insert( usersToFetch, "KnVvDiV2Cj")
table.insert( usersToFetch, "Paf6LDmykp")
and the working query I want to perform is the following lua table (which will get encoded before heading to parse). As I said, this works when I am hard coding the values as shown
local queryTable = {
["where"] = {
["objectId"] = { ["$in"] = {"KnVvDiV2Cj","Paf6LDmykp" }}
},
["limit"] = 1000
}
My problem is how do I incorporate my 'usersToFetch' table in the above table so that it will work the same as hard coding the values?
I swore I tried that but clearly I did not.. I think that I placed it inside the braces whereas they are not needed which is where I went wrong.
Thanks, hjpotte92 - what you put worked fine but this is my final solution in a single declaration:
Where I was going wrong before was because I had too many braces ["objectId"] = { ["$in"] = { usersToFetch } }
local queryTable = {
["where"] = {
["objectId"] = { ["$in"] = usersToFetch}
},
["limit"] = 1000
}

How to prefix a Lua table?

I have a lua file whose content is lua Table as below:
A={},
A.B={},
A.B.C=0;,
The problem is I want to add prefix XYZ before each above statements. So after the parse the database should have something loke this:
XYZ.A={},
XYZ.A.B={},
XYZ.A.B.C={},
Any ideas? Thanks in advance
You can load the file with XYZ as is environment: loadfile("mydata","t",XYZ). See loadfile in the manual.
This works in Lua 5.2. For Lua 5.1, use loadfile followed by setfenv.
If you can afford polluting your global space with A, simply assign it later:
-- load the file
-- if XYZ doesn't exist, XYZ = { A = A } would be probably shorter
XYZ.A = A
A = nil
I think this is what you want:
XYZ = {}
XYZ.A = {}
XYZ.A.B = {}
XYZ.A.B.C = 0
How about you simply do:
XYZ = {
A = {
B = {
C = 0
}
}
}
If you don't want to nest objects so deep then you may do:
XYZ = {
A = A
}
A = nil
This assumes that you have already declared the object A before.

Resources