groovymetaprogrammingkatalon-studio

Is it possible to get the name of variable in Groovy?


I would like to know if it is possible to retrieve the name of a variable.

For example if I have a method:

def printSomething(def something){
//instead of having the literal String something, I want to be able to use the name of the variable that was passed
println('something is: ' + something)
}

If I call this method as follows:

 def ordinary = 58
 printSomething(ordinary)

I want to get:

ordinary is 58

On the other hand if I call this method like this:

def extraOrdinary = 67
printSomething(extraOrdinary)

I want to get:

extraOrdinary is 67

Edit

I need the variable name because I have this snippet of code which runs before each TestSuite in Katalon Studio, basically it gives you the flexibility of passing GlobalVariables using a katalon.features file. The idea is from: kazurayam/KatalonPropertiesDemo

  @BeforeTestSuite
    def sampleBeforeTestSuite(TestSuiteContext testSuiteContext) {
        KatalonProperties props = new KatalonProperties()
        // get appropriate value for GlobalVariable.hostname loaded from katalon.properties files
        WebUI.comment(">>> GlobalVariable.G_Url default value: \'${GlobalVariable.G_Url}\'");

//gets the internal value of GlobalVariable.G_Url, if it's empty then use the one from katalon.features file
            String preferedHostname = props.getProperty('GlobalVariable.G_Url')
            if (preferedHostname != null) {
                GlobalVariable.G_Url = preferedHostname;
                WebUI.comment(">>> GlobalVariable.G_Url new value: \'${preferedHostname}\'");
            } else {
                WebUI.comment(">>> GlobalVariable.G_Url stays unchanged");
            }
 //doing the same for other variables is a lot of duplicate code
        }

Now this only handles 1 variable value, if I do this for say 20 variables, that is a lot of duplicate code, so I wanted to create a helper function:

def setProperty(KatalonProperties props, GlobalVariable var){
   WebUI.comment(">>> " + var.getName()" + default value: \'${var}\'");

//gets the internal value of var, if it's null then use the one from katalon.features file
            GlobalVariable preferedVar = props.getProperty(var.getName())
            if (preferedVar != null) {
                var = preferedVar;
                WebUI.comment(">>> " + var.getName() + " new value: \'${preferedVar}\'");
            } else {
                WebUI.comment(">>> " + var.getName() + " stays unchanged");
            }
}

Here I just put var.getName() to explain what I am looking for, that is just a method I assume.


Solution

  • Since Groovy 4.0, it is possible to do that using NV and SV macro.

    def printSomethingNV(def something) {
        println("NV $something.name is : $something.val")
    }
    
    def printSomethingSV(def something) {
        def (name, val) = something.split('=')
        println("SV $name is : $val")
    }
    
    def ordinary = 58
    printSomethingNV(NV(ordinary))
    printSomethingSV(SV(ordinary))
    
    def extraOrdinary = 67
    printSomethingNV(NV(extraOrdinary))
    printSomethingSV(SV(extraOrdinary))
    
    // Output
    // NV ordinary is : 58
    // SV ordinary is : 58
    // NV extraOrdinary is : 67
    // SV extraOrdinary is : 67