LinkTool.addRequestParams is difficult to use due to varargs/String[] parameter

2011-07-27 Thread Christopher Schultz
All,

I've been trying to switch my templates from using
LinkTool.addIgnore()addAllParameters() to use
LinkTool.addRequestParamsExcept(String... allButThese) but I'm having a
bit of difficulty with it.

If I call addRequestParams() with no argument, things work as expect. On
the other hand, this does not work:

#set($ignoreList = ['foo'])
$link.relative('/bar').addRequestParamsExcept($ignoreList)

I get an invalid reference log message and the above "$link..." text is
rendered as written instead of evaluating successfully.

Without much investigation, I believe the problem is that the ignoreList
is a List and it needs to be String[]. Velocity will auto-convert Lists
into Object[] if appropriate, but I suspect that the resulting object
type is Object[] and not, say, String[].

There does not appear to be a way to create a String[] from a Velocity
template, so using addRequestParams and the other, similar methods will
be very difficult to use with an argument.

If anyone has any suggestions for how to use these methods with
template-supplied data, I'd love to hear them. Otherwise, I'm inclined
to either overload or modify these methods to accept an Object[]
parameter and throw an exception if the array elements are not String
objects.

Thanks,
-chris



signature.asc
Description: OpenPGP digital signature


Re: LinkTool.addRequestParams is difficult to use due to varargs/String[] parameter

2011-07-27 Thread Nathan Bubna
On Wed, Jul 27, 2011 at 11:59 AM, Christopher Schultz
 wrote:
> All,
>
> I've been trying to switch my templates from using
> LinkTool.addIgnore()addAllParameters() to use
> LinkTool.addRequestParamsExcept(String... allButThese) but I'm having a
> bit of difficulty with it.
>
> If I call addRequestParams() with no argument, things work as expect. On
> the other hand, this does not work:
>
> #set($ignoreList = ['foo'])
> $link.relative('/bar').addRequestParamsExcept($ignoreList)
>
> I get an invalid reference log message and the above "$link..." text is
> rendered as written instead of evaluating successfully.
>
> Without much investigation, I believe the problem is that the ignoreList
> is a List and it needs to be String[]. Velocity will auto-convert Lists
> into Object[] if appropriate, but I suspect that the resulting object
> type is Object[] and not, say, String[].

i believe you're right.

> There does not appear to be a way to create a String[] from a Velocity
> template, so using addRequestParams and the other, similar methods will
> be very difficult to use with an argument.

why not just do $link.addRequestParamsExcept('foo', 'bar', 'etc')?

> If anyone has any suggestions for how to use these methods with
> template-supplied data, I'd love to hear them. Otherwise, I'm inclined
> to either overload or modify these methods to accept an Object[]
> parameter and throw an exception if the array elements are not String
> objects.

i think that would be a good enhancement regardless.  i don't
particularly like templates and tools being picky about types when
they don't have to be.

-
To unsubscribe, e-mail: user-unsubscr...@velocity.apache.org
For additional commands, e-mail: user-h...@velocity.apache.org



Re: LinkTool.addRequestParams is difficult to use due to varargs/String[] parameter

2011-07-27 Thread Christopher Schultz
Nathan,

On 7/27/2011 4:40 PM, Nathan Bubna wrote:
> On Wed, Jul 27, 2011 at 11:59 AM, Christopher Schultz
>  wrote:
>> All,
>>
>> I've been trying to switch my templates from using
>> LinkTool.addIgnore()addAllParameters() to use
>> LinkTool.addRequestParamsExcept(String... allButThese) but I'm having a
>> bit of difficulty with it.
>>
>> If I call addRequestParams() with no argument, things work as expect. On
>> the other hand, this does not work:
>>
>> #set($ignoreList = ['foo'])
>> $link.relative('/bar').addRequestParamsExcept($ignoreList)
>>
>> I get an invalid reference log message and the above "$link..." text is
>> rendered as written instead of evaluating successfully.
>>
>> Without much investigation, I believe the problem is that the ignoreList
>> is a List and it needs to be String[]. Velocity will auto-convert Lists
>> into Object[] if appropriate, but I suspect that the resulting object
>> type is Object[] and not, say, String[].
> 
> i believe you're right.
> 
>> There does not appear to be a way to create a String[] from a Velocity
>> template, so using addRequestParams and the other, similar methods will
>> be very difficult to use with an argument.
> 
> why not just do $link.addRequestParamsExcept('foo', 'bar', 'etc')?

When you've got an array (or list) of items, it's tough to turn it into
a varargs call :) I don't know the list of "ignore" items beforehand, so
I can't hard-code the list into the template. Instead, a calling
template needs to set up some kind of variable that contains that ignore
list.

>> If anyone has any suggestions for how to use these methods with
>> template-supplied data, I'd love to hear them. Otherwise, I'm inclined
>> to either overload or modify these methods to accept an Object[]
>> parameter and throw an exception if the array elements are not String
>> objects.
> 
> i think that would be a good enhancement regardless.  i don't
> particularly like templates and tools being picky about types when
> they don't have to be.

Okay, I'll go ahead and log it.

-chris



signature.asc
Description: OpenPGP digital signature


Re: LinkTool.addRequestParams is difficult to use due to varargs/String[] parameter [fixed]

2011-07-27 Thread Christopher Schultz
Fixed in 2.0.x branch.

On 7/27/2011 4:49 PM, Christopher Schultz wrote:
> Nathan,
> 
> On 7/27/2011 4:40 PM, Nathan Bubna wrote:
>> On Wed, Jul 27, 2011 at 11:59 AM, Christopher Schultz
>>  wrote:
>>> All,
>>>
>>> I've been trying to switch my templates from using
>>> LinkTool.addIgnore()addAllParameters() to use
>>> LinkTool.addRequestParamsExcept(String... allButThese) but I'm having a
>>> bit of difficulty with it.
>>>
>>> If I call addRequestParams() with no argument, things work as expect. On
>>> the other hand, this does not work:
>>>
>>> #set($ignoreList = ['foo'])
>>> $link.relative('/bar').addRequestParamsExcept($ignoreList)
>>>
>>> I get an invalid reference log message and the above "$link..." text is
>>> rendered as written instead of evaluating successfully.
>>>
>>> Without much investigation, I believe the problem is that the ignoreList
>>> is a List and it needs to be String[]. Velocity will auto-convert Lists
>>> into Object[] if appropriate, but I suspect that the resulting object
>>> type is Object[] and not, say, String[].
>>
>> i believe you're right.
>>
>>> There does not appear to be a way to create a String[] from a Velocity
>>> template, so using addRequestParams and the other, similar methods will
>>> be very difficult to use with an argument.
>>
>> why not just do $link.addRequestParamsExcept('foo', 'bar', 'etc')?
> 
> When you've got an array (or list) of items, it's tough to turn it into
> a varargs call :) I don't know the list of "ignore" items beforehand, so
> I can't hard-code the list into the template. Instead, a calling
> template needs to set up some kind of variable that contains that ignore
> list.
> 
>>> If anyone has any suggestions for how to use these methods with
>>> template-supplied data, I'd love to hear them. Otherwise, I'm inclined
>>> to either overload or modify these methods to accept an Object[]
>>> parameter and throw an exception if the array elements are not String
>>> objects.
>>
>> i think that would be a good enhancement regardless.  i don't
>> particularly like templates and tools being picky about types when
>> they don't have to be.
> 
> Okay, I'll go ahead and log it.
> 
> -chris
> 



signature.asc
Description: OpenPGP digital signature