groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "jeff.l" <jef...@mavericklabel.com>
Subject Re: Auto-generating a map key
Date Thu, 23 Jul 2015 20:36:11 GMT
Thanks, Jason.  I had forgotten about the technique of using enclosing parenthesis to ‘resolve’
the function that generates the key, but that is cleaner.

I suppose one could substitute “${closureKey()}” for (closureKey()) instead, but I prefer
the latter.

(It is important that closures are executed in order, so your second suggestion wouldn’t
work in this case.)

— Jeff



> On Jul 23, 2015, at 12:06 PM, Winnebeck, Jason [via Groovy] <ml-node+s329449n5726594h87@n5.nabble.com>
wrote:
> 
> If you are trying to save syntax, the quotes on the keys are optional, and if you want
code you can surround in parens:
> 
>  
> 
> //This can be defined in your base class, as a global, whatever so it’s not in your
fixture DSL
> 
> int c = 0
> 
> def closureKey = {“_CLOSURE_$c”}
> 
>  
> 
>  
> 
> def fixture = {
> 
>   field1: “value1”,
> 
>  field2: “value2”,
> 
>   (closureKey()): {$(‘a.button’).click()},
> 
>   field3: “value3”,
> 
>  (closureKey()): {…}
> 
> }
> 
>  
> 
> Another option is to use the Groovy call syntax, where all “named” parameters turn
into a map, and all the rest are in a list (although order is not preserved). The resulting
DSL would look like this:
> 
>  
> 
> Fixture(
> 
> field1: “value1”,
> 
> field2: “value2”,
> 
> {$(‘a.button’).click()},
> 
> field3: “value3”,
> 
> { … }
> 
> )
> 
>  
> 
> The Fixture method would have this signature:
> 
> def Fixture(Map fields, List closures)
> 
>  
> 
> The drawback is that the relative order between the “fields” and closures is not
preserved.
> 
>  
> 
> Jason
> 
>  
> 
> From: Jeff Lowery [mailto:[hidden email] <x-msg://31/user/SendEmail.jtp?type=node&node=5726594&i=0>]

> Sent: Thursday, July 23, 2015 1:32 PM
> To: [hidden email] <x-msg://31/user/SendEmail.jtp?type=node&node=5726594&i=1>
> Subject: Re: Auto-generating a map key
> 
>  
> 
> This is sort of a unique situation, where I’m setting up test fixtures and trying to
minimize the punctuation. The fixture is iterated through, and in general the key is the name
of a form field and the value is the value the field is set to.  
> 
>  
> 
> The problem is most, but not all, cases fit that pattern.  Some times I have a button
to click, or a mouse hover action to perform.  
> 
>  
> 
> So a fixture looks like:
> 
>  
> 
> def fixture = {
> 
>      “field1”: “value1”,
> 
>      “field2”: “value2”,
> 
>      “_CLOSURE_0”: {$(‘a.button’).click()},
> 
>      “field3”: “value3”,
> 
>      “_CLOSURE_1”: {…}
> 
> }
> 
>  
> 
> So the two points you make about GStrings (which I appreciate), don’t apply here. 
The “_CLOSURE_${c++}” GString is just a way to uniquify the closure keys automatically.
> 
>  
> 
> It is a bit ugly, but it is also concise (it is essentially a script that is run through
an interpreter).  Could a different data structure do the trick?  Sure:
> 
>  
> 
> def fixture = [
> 
>    [“field1”, “value1”],
> 
>    [“field2”, “value2”],
> 
>    [{$(‘a.button’).click()}],
> 
>    …
> 
> ];
> 
>  
> 
> Just a little too brackety in my opinion, but it does dispense with the ugly “_CLOSURE_${c++}”
naming.
> 
>  
> 
> What I was hoping for was a generator-like function that I could use in place of the
“_CLOSURE_${c++}”, but Groovy doesn’t seem to allow method execution when defining a
map key (though GStrings come close).
> 
>  
> 
> — Jeff
> 
>  
> 
>  
> 
> On Jul 22, 2015, at 10:49 PM, Jochen Theodorou <[hidden email] <x-msg://31/user/SendEmail.jtp?type=node&node=5726594&i=2>>
wrote:
> 
>  
> 
> Am 23.07.2015 00:39, schrieb Lowery, Jeff:
> 
> 
> Trying to find the simplest way to auto generate a map key; the best
> I've come up with is:
> 
> def c=0
> 
> def map = {
>    "_KEY$c++" : "foo", "_KEY$c++": "bar", ... }
> 
> 
> First of all: You shall not use GString as map key.
> a) GString is a mutable, using mutables as map keys can lead to very interesting behaviour
of the map
> b) GString is not having the same hashcode as String even if the toString() representation
is the same.
> 
> Factor (a) is probably not a problem here, but (b) can easily cause a problem.
> 
> 
> 
> Is there better?
> 
> 
> you are not really giving enough information. For example, why not simply use a list
here? The key seems not to play an important role, thus I doubt that searching is the problem
you try to solve with the map. But then it is questionable if a map is even the right datastructure
for your problem.
> 
> 
> bye blackdrag
> 
> -- 
> Jochen "blackdrag" Theodorou
> blog: http://blackdragsview.blogspot.com/ <http://blackdragsview.blogspot.com/>
>  
> 
> This email message and any attachments are for the sole use of the intended recipient(s).
Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the
intended recipient, please contact the sender by reply email and destroy all copies of the
original message and any attachments.
> 
> 
> If you reply to this email, your message will be added to the discussion below:
> http://groovy.329449.n5.nabble.com/Auto-generating-a-map-key-tp5726536p5726594.html <http://groovy.329449.n5.nabble.com/Auto-generating-a-map-key-tp5726536p5726594.html>
> To unsubscribe from Auto-generating a map key, click here <http://groovy.329449.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5726536&code=amVmZi5sQG1hdmVyaWNrbGFiZWwuY29tfDU3MjY1MzZ8LTE3NzYzODYwODI=>.
> NAML <http://groovy.329449.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>




--
View this message in context: http://groovy.329449.n5.nabble.com/Auto-generating-a-map-key-tp5726536p5726595.html
Sent from the Groovy Users mailing list archive at Nabble.com.
Mime
View raw message