From users-return-4323-apmail-groovy-users-archive=groovy.apache.org@groovy.apache.org Mon Apr 27 13:39:52 2020 Return-Path: X-Original-To: apmail-groovy-users-archive@minotaur.apache.org Delivered-To: apmail-groovy-users-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by minotaur.apache.org (Postfix) with SMTP id 638711932A for ; Mon, 27 Apr 2020 13:39:52 +0000 (UTC) Received: (qmail 39214 invoked by uid 500); 27 Apr 2020 13:39:51 -0000 Delivered-To: apmail-groovy-users-archive@groovy.apache.org Received: (qmail 39184 invoked by uid 500); 27 Apr 2020 13:39:51 -0000 Mailing-List: contact users-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@groovy.apache.org Delivered-To: mailing list users@groovy.apache.org Received: (qmail 39173 invoked by uid 99); 27 Apr 2020 13:39:51 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 27 Apr 2020 13:39:51 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id 63816C2294 for ; Mon, 27 Apr 2020 13:39:50 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -0.019 X-Spam-Level: X-Spam-Status: No, score=-0.019 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HTML_MESSAGE=0.2, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=disabled Authentication-Results: spamd4-us-west.apache.org (amavisd-new); dkim=pass (2048-bit key) header.d=gmail.com Received: from mx1-ec2-va.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id kTV0Kq1ULNTz for ; Mon, 27 Apr 2020 13:39:48 +0000 (UTC) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=209.85.210.46; helo=mail-ot1-f46.google.com; envelope-from=davidmichaelkarr@gmail.com; receiver= Received: from mail-ot1-f46.google.com (mail-ot1-f46.google.com [209.85.210.46]) by mx1-ec2-va.apache.org (ASF Mail Server at mx1-ec2-va.apache.org) with ESMTPS id 4C5ABBB904 for ; Mon, 27 Apr 2020 13:39:48 +0000 (UTC) Received: by mail-ot1-f46.google.com with SMTP id c3so26116167otp.8 for ; Mon, 27 Apr 2020 06:39:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=l0Hc4YulI8Lc8PHkMEQav0ujfFh4y7b69QbLJLfHQbw=; b=ZuaXvRWFB8qjZamtc9dwRjR//rYQfXPAXl7P9/IqilKaUB2w8/ADZlbyUeYirYNqGV XffB6tbr44uUs8ahxsunsuMjEMP0weIRRH5xzEvCT3dkrFCUhNGTEgVm4rknXCmQj3Ju kErB5Cl70PWV0RFIWdpQZUfcuZgLhUl3xJCa7s5hSH2WOxlgBP+Rss4ktWE27S64GOeB jjOhK9NdkL3V9Gyv8CGh8ajjnMvUIelL29EApu2qrd8RES2mqRG0ZAoLyFwlexZxciYr gYjpGKolAymauSQ8eJUSnO6WEs6cEswbXkwHi1MfvpTWbToK1wSwtdUbrGOxgissTs6D Kvag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=l0Hc4YulI8Lc8PHkMEQav0ujfFh4y7b69QbLJLfHQbw=; b=GWys7GhTX5s+rL+QfxuroVj+Ccs5MaYTm6AcNJUowte0O5J/taT5BYfvpjwUhOWMWv jnIG8NFhc7Ss17CT3qoMXkhvF8ySiTwsLGbYcn2n4mYA6SKxWf/6cGw//8hWPtFQTf3g t5V8bzKombqBOynHRcYs4go7+9EP11XtCVJlPVfaZ7JCnEF5XaWRgIHaPqKMCRsK/IMx nVgtaY1sTWw3iJnMC2S39ptzIR4Ayj+khjApPq/hzoh329OpHKmMz/ClLsGpvKPbxJuG hJvtQeGp8oHPHue8b5C0f7bj9FOX7e/hpyhASiabcf8GNTnbWTod/xsne8u0Stp+IKUs Z+WA== X-Gm-Message-State: AGi0PuZaqHsMTGMOWBMcPmRB/4XfXDLfUIa0BWQuCqXSanq4TdmOWd4B Bl2RKSjCAJGHa8rdMdogXn6tExZ/vw+rgWMcZjTqC91B X-Google-Smtp-Source: APiQypKqqQgQV6aOCiZ+haUCu8HMDJpQYS9EXuzcpzRtsxm0T6h1CvfqnU/9eSPZyVz8Hh+nuS+szkIDm4eMJCef5yQ= X-Received: by 2002:aca:5513:: with SMTP id j19mr15338658oib.31.1587994782105; Mon, 27 Apr 2020 06:39:42 -0700 (PDT) MIME-Version: 1.0 References: <5fa91bd3-ad16-0b3d-c9da-09a94c0ac60c@mcneils.net> In-Reply-To: <5fa91bd3-ad16-0b3d-c9da-09a94c0ac60c@mcneils.net> From: David Karr Date: Mon, 27 Apr 2020 06:39:29 -0700 Message-ID: Subject: Re: Strategy for optionally excluding a named method parameter? To: users@groovy.apache.org Content-Type: multipart/alternative; boundary="000000000000e4832005a445d87c" --000000000000e4832005a445d87c Content-Type: text/plain; charset="UTF-8" On Sun, Apr 26, 2020 at 5:43 PM Peter McNeil 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 > --000000000000e4832005a445d87c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

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 <= br> > 2.89.4 .
>
> I'm working with an api that is somewhat dumb in one respect.=C2= =A0 The
> method we call takes ~25 parameters.=C2=A0 We send them as named
> parameters. One of the parameters is of boolean type.=C2=A0 What we= 9;ve
> discovered from testing is that if we send a value as either "tru= e" or
> "false", it acts as if we sent "true".=C2=A0 If we= construct the call
> without that parameter entirely, it acts as if we sent "false&quo= t;.

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>=C2=A0=C2=A0=C2=A0=C2=A0 println "b1 =3D ${args.b1}" groovy>=C2=A0=C2=A0=C2=A0=C2=A0 println "b2 =3D ${args.b2}" groovy>=C2=A0=C2=A0=C2=A0=C2=A0 println "b3 =3D ${args.b3}" groovy>=C2=A0=C2=A0=C2=A0=C2=A0 if (args.b1) {
groovy>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 println "b1= tests true"
groovy>=C2=A0=C2=A0=C2=A0=C2=A0 } else {
groovy>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 println "b1= tests false"
groovy>=C2=A0=C2=A0=C2=A0=C2=A0 }
groovy> }
groovy> foo(b2: true, b1: "false", b3: false)

b1 =3D false
b2 =3D true
b3 =3D false
b1 tests true

The former is more likely= true.=C2=A0 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.=C2=A0 We presently have an "if" for that one flag, = with two calls
> to the method, one taking 25 parameters, the other taking 24.=C2=A0 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.=C2=A0 The reality is, that's not going to happen any ti= me 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 =3D [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 acces= s to the class files, much less have any control over the implementation.


Cheers,

Peter.


--
web: ht= tp://nerderg.com, twitter: http://twitter.com/pmcneil
--000000000000e4832005a445d87c--