質問

I'm creating a DSL in Groovy for doing Http endpoint testing, I'm want it to be natural language-ish and I have objects that have a 'with' method. When I chain the commands my method is correctly called, but if the 'with' method is called on a variable script compilation fails as the existing Groovy 'with(Closure)' is used. DSL scripts files are read in and executed with the GroovyShell.

This works:

request = http GET to "${url}" with headers, [ 'Cookie': 'monster' ]

But this does not:

request = http GET to "${url}"
request with headers, [ 'Cookie': 'monster' ]

The object being called is written in Java, but can be moved to Groovy. This is the method signature:

<returns this> with(HttpMethodElement eml, Object value);

From the error returned it seems its not finding the Java with method and complaining that a Closure is not being passed in. If I add explicit parenthesis I get a method not found exception. Which is confusing as it was found before it was assigned to a variable...

expecting EOF, found ',' @ line 6, column 31.
request = request with headers, [ 'Cookie': 'monster' ]
                              ^

I think Groovy may be doing some transformation or wrapping of the Java object into a Groovy object within the script but the two methods have distinct signatures and should be legal.

役に立ちましたか?

解決

It's the parser

Groovy sees

request = http GET to "${url}" with headers, [ 'Cookie': 'monster' ]

As

request = http( GET ).to( "${url}" ).with( headers, [ 'Cookie': 'monster' ] )

Which as you have seen is fine, but when it tries to parse

request with headers, [ 'Cookie': 'monster' ]

Is seen as

request( with headers, [ 'Cookie': 'monster' ] )

So with becomes a parameter and then you're missing a comma and it causes your problem. Basically the parser gets lost

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top