Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Groovy DSL: No EachLike validation #1076

Open
artamonovkirill opened this issue Apr 23, 2020 · 2 comments
Open

[Question] Groovy DSL: No EachLike validation #1076

artamonovkirill opened this issue Apr 23, 2020 · 2 comments

Comments

@artamonovkirill
Copy link
Contributor

A raw array "eachLike":

withBody eachLike(1) {
    type regexp('.*', 'banana')
}

produces:

[
  {
    "type": "banana"
  }
]

A named array "eachLike":

withBody {
    fruits eachLike(1) {
        type regexp('.*', 'banana')
    }
}

produces:

{
  "fruits": [
    {
      "type": "banana"
    }
  ]
}

A wrong DSL like:

withBody {
    eachLike(1) {
        type regexp('.*', 'banana')
    }
}

produces:

{}

but there is not warning or error regarding incorrect DSL usage.

Is there a way to produce an error or a warning in case "eachLike" is put in a wrong place inside "withBody" DSL?

Code examples can be found here: https://github.com/artamonovkirill/pact-issues-examples/tree/master/src/test/groovy/com/github/artamonovkirill/pact/eachlikevalidaiton

@uglyog
Copy link
Member

uglyog commented May 2, 2020

That will be really hard to detect. The first example works because the eachLike method returns a matcher, which is then passed to the withBody as a parmeter, and that method knows what to do with it.

The second example works because the builder uses the missing method functionality in Groovy, so
fruits eachLike(1) means execute the eachLike as before, but then pass it to the fruits method. And the method missing callback kicks in so it can be handled.

The problem is that there is nothing to hook on too with the third. The outer closure is executed, which contains just the eachLike call, and then the closure terminates. Because the returned value of the eachLike method is not assigned to anything, it is lost.

One thing I could try is checking the result of the closure, and if that is a matcher then maybe something is wrong.

@uglyog
Copy link
Member

uglyog commented May 2, 2020

The return value works, I can detect that example. I can also detect this one:

withBody {
    fruits eachLike(1) {
        regexp('.*', 'banana')
    }
}

However, if something else is returned from the closure, then there is nothing I can do. I.e.,

withBody {
    eachLike(1) {
        type regexp('.*', 'banana')
    }
    "fruits" // This will be the return value
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants