[stringtemplate-interest] Multi-valued argument stuff...

Terence Parr parrt at cs.usfca.edu
Mon Jun 25 19:05:50 PDT 2007


On Jun 26, 2007, at 11:58 AM, Tim Spurway wrote:

> Thanks for the quick response, Ter, I have a few more queries :)
>
> > List<Map<String,Object>> x = getXFromSomewhere();
> > template.setAttribute( "someXes", x );
> >
> > in my template, i can iterate the normal way.  For example:
> >
> > $someXes:{ anX | This is the Y value of the $i$th X --> $anX.Y$ }
> >
> > works like a charm.
>
> hi :)  maps iterate through values not keys so the object values in
> your map are assigned to anX.
>
> but someXes is a List<Map> not just a Map

oh, right!  Sorry. I read it too quickly.  first(someXes) should be a  
Map.  iteration should go down the elements in the list whose  
elements are maps.

> - I would expect that the iteration wouldn't get 'down' to the Maps  
> at all and the iteration would assign the Map to anX.

correct.  Then, $anX.Y$ will look up Y in the map using Y as a key.

>   And in the first In the case above, what I am seeing is what I  
> expect - inside of the anonymous 'block' I am able to reference the  
> Y member (key) of the anX iteration element (which must be a Map),  
> ie. that anX.Y returns the value that was assigned to it in each  
> Map in the Java code.  This has been working for me with  
> arbitrarily large (and  sometimes nested) Lists of Maps.

Groovy.

> I could see that iteration over a simple Map would iterate over  
> it's values, but my question has to do with Lists of Maps and if  
> they are treated differently.  It looks as though the first()  
> 'function' is returning the the first element of the List, which in  
> my case is a Map, then the iteration is going over the values of  
> the Map - which would explain my current error.

Yep, that's what it'll do.

> The values are all Strings, so dereferencing the Y element of a  
> string wouldn't make sense.    I was kind of expecting first() to  
> return a single element List so that I could use the iteration  
> semantics to snag and process the single Map.

Hmm...yes, I see the confusion.  first must necessarily return a  
single item or else you couldn't operate on it.  You could never  
reduce a single element list to the element inside to get properties  
or whatever.

> I suppose what I would like to do is the following:
>
> This is the Y value of first X --> $first(someXes).Y$
> $rest(someXes):{ restX | This is the Y value of the $i$th X-->  
> $restX.Y$ }
>
> I tried this, and got a syntax error.

on which expression?  The 2nd should be no problem at all.  perhaps i  
messed up syntax on first expr.

> I really would like to access the first element of my list - I want  
> to set a radio button to 'checked' - everything else is working so  
> smoothly  (and fast, I might add - good job btw), I just can't seem  
> to make this work and it makes me wonder if I am really grokking  
> the framework and how it deals with multi-value attributes, or if  
> the framework was really intended to deal with this type of data.

Hmm...if first(someXes).Y doens't work, then do first(someXes):{anX |  
$anX.y$}

Ter





More information about the stringtemplate-interest mailing list