user1207289
user1207289

Reputation: 3273

executing toString() in Eval () - Groovy (method call in Gstring)

Consider this, the value of

$RespNode

is

RespJson.seatbid[0].bid[0].price

I am trying to run

Eval.me('RespJson', RespJson, "assert $RespNode.toString() == '$aValue'") 

but getting error

 [No such property: toString for class: java.lang.String]

When I run this (directly, Without Eval() )

assert RespJson.seatbid[0].bid[0].price.toString()==aValue

it runs fine (no error)

The below also works fine (without toString() )

   Eval.me('RespJson', RespJson, "assert $RespNode == '$aValue'")

any ideas , how to run toString() with Eval() Thanks!

Upvotes: 0

Views: 331

Answers (2)

cfrick
cfrick

Reputation: 37073

$RespNode.toString() will be replaced at once. You have to use ${RespNode}.toString() to have it run via the eval. Otherwise see @WillP's answer (respNode.toString is evaluated at once and toString is no property)

def respJson = [seatbid:[[bid:[[price:666.0G]]]]]
def respNode = 'respJson.seatbid[0].bid[0].price'
def aValue = '666.0'
Eval.me('respJson', respJson, "assert ${respNode}.toString() == '$aValue'")

Upvotes: 2

Will
Will

Reputation: 14559

A method call in a GString needs curly brackets, otherwise the parents don't get parsed as part of the call:

class Foo {
  def getBar() { 'get bar' }
  def bar() { 'method bar' }
}

foo = new Foo()

assert "$foo.bar()".toString() == "get bar()"
assert "${foo.bar()}".toString() == "method bar"

Upvotes: 1

Related Questions