Re: [FreeMarker-user] The "params" Map of TemplateDirectiveModel

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.devel,gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Wednesday, February 17, 2010, 1:29:28 AM, Grégory Joseph wrote:

> Hi,
>
> I'm currently writing a bunch of TemplateDirectiveModel
> implementations, and as part of the parameters validation, I was
> wondering if it was considered "safe" or even good practice to
> manipulate the params map. What I had in mind is: get/convert
> parameters and remove them from the map as I go (using utility
> method which take care of checking types, throwing exception with
> adequate messages etc - see below); once I fetched all my
> parameters, I should be able to do something along the lines of:
> if (!params.isEmpty())
>   throw new TemplateModelException("unsupported parameter(s): " + params);
>
> Any thought or advice ? Would this be a decent way to go, or is
> there something easier I could do ?

So, your question is if it's allowed to *modify* parameter maps? The
API docs doesn't clarify this (er...), which practically means you
rather don't do it, because who knows. But that was why you were
asking it so... I looked into the source code of 2.3.x, and saw that
in the case when the parameter map is empty, it re-uses the same
shared EmptyMap.instance object. So that's a proof of that the
implementator (Jonathan maybe) assumed that you don't modify the map.
So no, don't do it. Usually it will work, but who knows if one day
another internalt optimization will start reuse maps in another case
(like when all map items are constants).

Guys, it must be documented in the API. Unless you decide that
modifying will be allowed after all, and according that you modify the
(single?) spot where that breaks now, I will say that it shouldn't be
modified, because than anything can happen, like faeries coming out of
the computer case and attack you, etc. (Of course, that restriction
can be later revoked in later version, if we wish, as that's BC.)

> Cheers,
>
> -greg
>
> ps: for the record: all my directives extend a custom Abstract one,
> which has a _param() method (see below) and other simpler protected
> methods which eventually all delegate to _param(), such as:
>
>     protected String mandatoryString(Map params, String key) throws TemplateModelException {
>         return _param(params, key, TemplateScalarModel.class, true).getAsString();
>     }
>
>     // [...] other param* methods [...]
>
>     protected <MT extends TemplateModel> MT _param(Map<String,
> TemplateModel> params, String key, Class<MT> type, boolean
> isMandatory) throws TemplateModelException {
>         if (isMandatory && !params.containsKey(key)) {
>             throw new TemplateModelException("The '" + key + "' parameter is mandatory.");
>
>         }
>         final TemplateModel m = params.get(key);
>         if (m != null && !type.isAssignableFrom(m.getClass())) {
>             throw new TemplateModelException("The '" + key + "'
> parameter must be a " + type.getSimpleName() + " and is a " +
> m.getClass().getSimpleName() + ".");
>         }
>         if (m == null && params.containsKey(key)) {
>             // parameter is passed but null value ... (happens with
> content.nonExistingSubNode apparently)
>             throw new TemplateModelException("The '" + key + "'
> parameter was passed but not or wrongly specified.");
>         }
>
>         return (MT) m;
>     }
>
>
>
>
> ------------------------------------------------------------------------------
> SOLARIS 10 is the OS for Data Centers - provides features such as DTrace,
> Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
> http://p.sf.net/sfu/solaris-dev2dev
> _______________________________________________
> FreeMarker-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
SOLARIS 10 is the OS for Data Centers - provides features such as DTrace,
Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
http://p.sf.net/sfu/solaris-dev2dev
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.