On Sun, Apr 26, 2020 at 5:43 PM Peter McNeil <peter@mcneils.net> wrote:
G'day David

On 25/4/20 6:17 am, David Karr wrote:
> Lately my only Groovy work is scripted pipelines in Jenkins, version
> 2.89.4 .
>
> I'm working with an api that is somewhat dumb in one respect.  The
> method we call takes ~25 parameters.  We send them as named
> parameters. One of the parameters is of boolean type.  What we've
> discovered from testing is that if we send a value as either "true" or
> "false", it acts as if we sent "true".  If we construct the call
> without that parameter entirely, it acts as if we sent "false".

The function could be making a truthyness mistake or you could be. e.g.
if you called the function with "false" instead of false

groovy> def foo(Map args) {
groovy>     println "b1 = ${args.b1}"
groovy>     println "b2 = ${args.b2}"
groovy>     println "b3 = ${args.b3}"
groovy>     if (args.b1) {
groovy>         println "b1 tests true"
groovy>     } else {
groovy>         println "b1 tests false"
groovy>     }
groovy> }
groovy> foo(b2: true, b1: "false", b3: false)

b1 = false
b2 = true
b3 = false
b1 tests true

The former is more likely true.  I've already carefully verified that the parameter I'm passing is truly a boolean by printing the class name of the parameter.



> I tried making it send null, but that just causes it to fail at
> runtime.  We presently have an "if" for that one flag, with two calls
> to the method, one taking 25 parameters, the other taking 24.  It is
> really obnoxious.
you could clean up (DRY up) the code by adding the params to a map then
just removing the parameter if that's the only way to make it work.
>
> Obviously, the proper fix is to change their api so that it works
> correctly.  The reality is, that's not going to happen any time soon
> in geological terms.
>
> Is there a concise groovy syntax we could use that would optionally
> include or exclude a single parameter to the method?

yes:

Map args = [p1: true, p2: false, p3: true ...]

if(!p4) args.remove("p4")

foo(args)

I assume this only works if I can redefine the implementation of the method. I don't even have access to the class files, much less have any control over the implementation.


Cheers,

Peter.


--
web: http://nerderg.com, twitter: http://twitter.com/pmcneil