From users-return-4322-apmail-groovy-users-archive=groovy.apache.org@groovy.apache.org Mon Apr 27 00:43:05 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 F3F0A1939D for ; Mon, 27 Apr 2020 00:43:04 +0000 (UTC) Received: (qmail 74390 invoked by uid 500); 27 Apr 2020 00:43:04 -0000 Delivered-To: apmail-groovy-users-archive@groovy.apache.org Received: (qmail 74374 invoked by uid 500); 27 Apr 2020 00:43:03 -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 74363 invoked by uid 99); 27 Apr 2020 00:43:03 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 27 Apr 2020 00:43:03 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id 3A53E182B68 for ; Mon, 27 Apr 2020 00:43:03 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.696 X-Spam-Level: X-Spam-Status: No, score=0.696 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, SPF_HELO_SOFTFAIL=0.896, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=disabled Authentication-Results: spamd3-us-west.apache.org (amavisd-new); dkim=pass (2048-bit key) header.d=mcneils.net Received: from mx1-ec2-va.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id yREHIiH5XT4N for ; Mon, 27 Apr 2020 00:43:01 +0000 (UTC) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.172.47; helo=mailau.nerderg.com; envelope-from=peter@mcneils.net; receiver= Received: from mailau.nerderg.com (mailau.nerderg.com [172.105.172.47]) by mx1-ec2-va.apache.org (ASF Mail Server at mx1-ec2-va.apache.org) with ESMTPS id 6DAE1BB8FE for ; Mon, 27 Apr 2020 00:43:00 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by mailau.nerderg.com (Postfix) with ESMTP id 1D9F93E83D for ; Mon, 27 Apr 2020 00:42:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mcneils.net; h= content-transfer-encoding:content-language:content-type :content-type:in-reply-to:mime-version:user-agent:date:date :message-id:from:from:references:subject:subject:received :received; s=20200302; t=1587948164; x=1588812165; bh=NXdHDU44rB hYP8j/I0SMbaO7uCJTDF1+hCRJ1Z8B16E=; b=UdNQLtfj2ImTEC7i75ZmS8Ek1V KON+UEdzVtaF+XFVkMHyFeZ0CYLxKM8DQTcwbZ3T3ejUMtoJG0+QbyBztCJavni+ tOT0+y7Da5wq9E55chYI7yx770N4fmIR3M8kq2hej0UTaeFH1w3kMaPjW8YBVw+v pfUJqPGXKnMcgbwSf/xke32zmsYaC8u3HJ5/cWhjZy44FWkjffXdYLCNkS730feV YwOEfbCtKGC4LKXQGbcJjpL+QBmrZakvEZWyv2CQDBXSZfarsmXAXNW3pTNvXhW+ nz+aqbX1V5FLzBMyexMJKxwyit/wCKGwbywyEw4GhSH5zW422qQZQ8838KiA== X-Virus-Scanned: amavisd-new at nerderg.com Received: from mailau.nerderg.com ([127.0.0.1]) by localhost (mailau.nerederg.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id XKB_8KPLyd0X for ; Mon, 27 Apr 2020 00:42:44 +0000 (UTC) Received: from [192.168.2.10] (unknown [59.167.254.119]) by mailau.nerderg.com (Postfix) with ESMTPSA id 90B683E820 for ; Mon, 27 Apr 2020 00:42:44 +0000 (UTC) Subject: Re: Strategy for optionally excluding a named method parameter? To: users@groovy.apache.org References: From: Peter McNeil Message-ID: <5fa91bd3-ad16-0b3d-c9da-09a94c0ac60c@mcneils.net> Date: Mon, 27 Apr 2020 10:42:47 +1000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: quoted-printable G'day David On 25/4/20 6:17 am, David Karr wrote: > Lately my only Groovy work is scripted pipelines in Jenkins, version=20 > 2.89.4 . > > I'm working with an api that is somewhat dumb in one respect.=C2=A0 The= =20 > method we call takes ~25 parameters.=C2=A0 We send them as named=20 > parameters. One of the parameters is of boolean type.=C2=A0 What we've=20 > discovered from testing is that if we send a value as either "true" or=20 > "false", it acts as if we sent "true".=C2=A0 If we construct the call=20 > 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.=20 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 > I tried making it send null, but that just causes it to fail at=20 > runtime.=C2=A0 We presently have an "if" for that one flag, with two ca= lls=20 > to the method, one taking 25 parameters, the other taking 24.=C2=A0 It = is=20 > really obnoxious. you could clean up (DRY up) the code by adding the params to a map then=20 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=20 > correctly.=C2=A0 The reality is, that's not going to happen any time so= on=20 > in geological terms. > > Is there a concise groovy syntax we could use that would optionally=20 > 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) Cheers, Peter. --=20 web: http://nerderg.com, twitter: http://twitter.com/pmcneil