macro errors

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

macro errors

Sam Steingold
Hi,
I am getting various macro errors:

<#macro sentiment scmr model><#if scmr??>
  <#assign senti = "${scmr.results[model]}">
    <#if senti??>
      <td>${senti} ---- ${senti.sentimentType}</td>
      <td>${senti.score?html}</td>
    <#else><td align="center" colspan="2">none</td>
    </#if>
</#if></#macro>

first error:

POSITIVE(1.0/1) ---- Expected hash. senti evaluated instead to freemarker.template.SimpleScalar

this is coming from "${senti} ---- ${senti.sentimentType}"
apparently, "senti" is the string representation of the object,
not the object itself.
how do I fix that?

second error:

Error executing macro: sentiment required parameter: scmr is not specified.

this is coming from this invocation:

 <@sentiment model=model scmr=result.title/>

here `result' is non-null, but result.title is null.
I do check for that in the macro, but, apparently, it has to be done
outside.  Why?

Thanks!


--
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://dhimmi.com http://thereligionofpeace.com
http://pmw.org.il http://think-israel.org http://jihadwatch.org
Don't take life too seriously, you'll never get out of it alive!


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: macro errors

Daniel Dekany
Wednesday, July 25, 2012, 6:09:00 AM, Sam Steingold wrote:

> Hi,
> I am getting various macro errors:
>
> <#macro sentiment scmr model><#if scmr??>
>   <#assign senti = "${scmr.results[model]}">
>     <#if senti??>
>       <td>${senti} ---- ${senti.sentimentType}</td>
>       <td>${senti.score?html}</td>
>     <#else><td align="center" colspan="2">none</td>
>     </#if>
> </#if></#macro>
>
> first error:
>
> POSITIVE(1.0/1) ---- Expected hash. senti evaluated instead to freemarker.template.SimpleScalar
>
> this is coming from "${senti} ---- ${senti.sentimentType}"
> apparently, "senti" is the string representation of the object,
> not the object itself.
> how do I fix that?
>
> second error:
>
> Error executing macro: sentiment required parameter: scmr is not specified.
>
> this is coming from this invocation:
>
>  <@sentiment model=model scmr=result.title/>
>
> here `result' is non-null, but result.title is null.
> I do check for that in the macro, but, apparently, it has to be done
> outside.  Why?

Yeah, the null-madness from the dark ages of FreeMarker, here to stay
with us as far as backward compatibility is kept. Basically, it's
suspected by FreeMarker that the expression result.title is a typo,
and that's why it's undefined (null), hence the error. Now, maybe it's
defined, like `result` is a JavaBean where you have a getTitle()
method that returns null, but FreeMarker doesn't differentiate,
because, if `result` is a Map on the Java-side and there's no title
then there's indeed no "title" key (hence it can't be told if
result.title is a typo or just Java-null). Surely you could tell that
if `result` is a JavaBean, but you should be to able to switch
back-and-forth between a JavaBean `result` and a Map `result` in the
data-model without changing (breaking) the behavior of templates, so
you get the common subset.

What to do? Here's a hack... If know that result.title should be a
String or null, you can call the macro as (note the "!"):

  <@sentiment model=model scmr=result.title! />

Then in the macro you can do <#if a?is_sequence>title was null</#if>
or such.

There's also an old implementation bug (that's kept for
backward-compatibility), where, if you pass an undefined expression to
a macro as a parameter (like even something that's indeed a typo), and
the macro defines a default value for the parameter, that default
value will kick in. So if you prefer, you can just do this:

  <#macro sentiment scmr! model><#if scmr?is_sequence>

and then you don't need the `!` when calling the macro.

BTW, instead of `?is_sequence` you could use `== ''` too, however if
the parameter can be legally an empty string then it's not a generic
solution.

> Thanks!

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: macro errors

Denis Bredelet
Hi Sam

Hi,
I am getting various macro errors:

<#macro sentiment scmr model><#if scmr??>
 <#assign senti = "${scmr.results[model]}">
   <#if senti??>
     <td>${senti} ---- ${senti.sentimentType}</td>
     <td>${senti.score?html}</td>
   <#else><td align="center" colspan="2">none</td>
   </#if>
</#if></#macro>

first error:

POSITIVE(1.0/1) ---- Expected hash. senti evaluated instead to freemarker.template.SimpleScalar

this is coming from "${senti} ---- ${senti.sentimentType}"
apparently, "senti" is the string representation of the object,
not the object itself.
how do I fix that?

You need to use:

 <#assign senti = scmr.results[model]>

-- Denis. 


second error:

Error executing macro: sentiment required parameter: scmr is not specified.

this is coming from this invocation:

<@sentiment model=model scmr=result.title/>

here `result' is non-null, but result.title is null.
I do check for that in the macro, but, apparently, it has to be done
outside.  Why?

Yeah, the null-madness from the dark ages of FreeMarker, here to stay
with us as far as backward compatibility is kept. Basically, it's
suspected by FreeMarker that the expression result.title is a typo,
and that's why it's undefined (null), hence the error. Now, maybe it's
defined, like `result` is a JavaBean where you have a getTitle()
method that returns null, but FreeMarker doesn't differentiate,
because, if `result` is a Map on the Java-side and there's no title
then there's indeed no "title" key (hence it can't be told if
result.title is a typo or just Java-null). Surely you could tell that
if `result` is a JavaBean, but you should be to able to switch
back-and-forth between a JavaBean `result` and a Map `result` in the
data-model without changing (breaking) the behavior of templates, so
you get the common subset.

What to do? Here's a hack... If know that result.title should be a
String or null, you can call the macro as (note the "!"):

 <@sentiment model=model scmr=result.title! />

Then in the macro you can do <#if a?is_sequence>title was null</#if>
or such.

There's also an old implementation bug (that's kept for
backward-compatibility), where, if you pass an undefined expression to
a macro as a parameter (like even something that's indeed a typo), and
the macro defines a default value for the parameter, that default
value will kick in. So if you prefer, you can just do this:

 <#macro sentiment scmr! model><#if scmr?is_sequence>

and then you don't need the `!` when calling the macro.

BTW, instead of `?is_sequence` you could use `== ''` too, however if
the parameter can be legally an empty string then it's not a generic
solution.

Thanks!

--
Best regards,
Daniel Dekany


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: macro errors

Sam Steingold
In reply to this post by Daniel Dekany
The following message is a courtesy copy of an article
that has been posted to gmane.comp.web.freemarker.user as well.

Thanks for your kind reply!

> * Daniel Dekany <[hidden email]> [2012-07-25 10:59:16 +0200]:
> Wednesday, July 25, 2012, 6:09:00 AM, Sam Steingold wrote:
>> I am getting various macro errors:
>>
>> <#macro sentiment scmr model><#if scmr??>
>>   <#assign senti = "${scmr.results[model]}">
>>     <#if senti??>
>>       <td>${senti} ---- ${senti.sentimentType}</td>
>>       <td>${senti.score?html}</td>
>>     <#else><td align="center" colspan="2">none</td>
>>     </#if>
>> </#if></#macro>
>>
>> first error:
>>
>> POSITIVE(1.0/1) ---- Expected hash. senti evaluated instead to freemarker.template.SimpleScalar
>>
>> this is coming from "${senti} ---- ${senti.sentimentType}"
>> apparently, "senti" is the string representation of the object,
>> not the object itself.
>> how do I fix that?
>>
>> second error:
>>
>> Error executing macro: sentiment required parameter: scmr is not specified.
>>
>> this is coming from this invocation:
>>
>>  <@sentiment model=model scmr=result.title/>
>>
>> here `result' is non-null, but result.title is null.
>> I do check for that in the macro, but, apparently, it has to be done
>> outside.  Why?
>
> Yeah, the null-madness from the dark ages of FreeMarker, here to stay
> with us as far as backward compatibility is kept. Basically, it's
> suspected by FreeMarker that the expression result.title is a typo,
> and that's why it's undefined (null), hence the error. Now, maybe it's
> defined, like `result` is a JavaBean where you have a getTitle()
> method that returns null, but FreeMarker doesn't differentiate,
> because, if `result` is a Map on the Java-side and there's no title
> then there's indeed no "title" key (hence it can't be told if
> result.title is a typo or just Java-null). Surely you could tell that
> if `result` is a JavaBean, but you should be to able to switch
> back-and-forth between a JavaBean `result` and a Map `result` in the
> data-model without changing (breaking) the behavior of templates, so
> you get the common subset.

yes. result is a JavaBean in my case, and getTitle() is defined.

are you suggesting that "." denotes both hash table and field access?
i.e., I could have written scmr.results.model instead of
scmr.results[model], right?

> There's also an old implementation bug (that's kept for
> backward-compatibility), where, if you pass an undefined expression to
> a macro as a parameter (like even something that's indeed a typo), and
> the macro defines a default value for the parameter, that default
> value will kick in. So if you prefer, you can just do this:
>   <#macro sentiment scmr! model><#if scmr?is_sequence>
> and then you don't need the `!` when calling the macro.

I got
Encountered "!" at line 2, column 23 in com/addthis/sentiment/sentidemo.ftl.
Was expecting one of:
    "=" ...
    "..." ...
    "," ...
    ")" ...
    <ID> ...
    ">" ...

column 23 is "!" in scmr!


okay,  here is what seems to be working for me:

<#macro sentiment scmr model><#if scmr != ''>
  <#assign senti = scmr.results[model]>
    <#if senti??>
      <td>${senti.sentimentType}</td>
      <td>${senti.score?html}</td>
    <#else><td align="center" colspan="2">none</td>
    </#if>
</#if></#macro>


      <@sentiment model=model scmr=result.title! />
      <@sentiment model=model scmr=result.body! />
      <@sentiment model=model scmr=result.mean! />

the big question:
will the "<#if senti??>" test in the macro work as intended?
what is the value of "senti" anyway? a string? the java bean object?

thanks!

--
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://pmw.org.il http://camera.org http://memri.org
http://iris.org.il http://dhimmi.com http://honestreporting.com
Genius is immortal, but morons live longer.

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: macro errors

Daniel Dekany
Wednesday, July 25, 2012, 5:42:51 PM, Sam Steingold wrote:

[snip]
> yes. result is a JavaBean in my case, and getTitle() is defined.
>
> are you suggesting that "." denotes both hash table and field access?

If the left-hand-operand is a java.util.Map, then it calls
Map.get(rho), otherwise it reads a JavaBean property or reads a public
Java field. Usually. To be exact, what "." does entirely depends on
the object_wrapper setting, and the type of the object. Like, if you
are using the default object wrapper, for a W3C DOM node it will issue
an XPath query.

> i.e., I could have written scmr.results.model instead of
> scmr.results[model], right?

scmr.results["model"] is the same as scmr.results.model. But
scmr.results[model] is something different (dynamic key).

>> There's also an old implementation bug (that's kept for
>> backward-compatibility), where, if you pass an undefined expression to
>> a macro as a parameter (like even something that's indeed a typo), and
>> the macro defines a default value for the parameter, that default
>> value will kick in. So if you prefer, you can just do this:
>>   <#macro sentiment scmr! model><#if scmr?is_sequence>
>> and then you don't need the `!` when calling the macro.
>
> I got
> Encountered "!" at line 2, column 23 in
> com/addthis/sentiment/sentidemo.ftl.

Eh... sorry. I meant:

  <#macro sentiment scmr=false model><#if scmr?is_boolean>

> Was expecting one of:
>     "=" ...
>     "..." ...
>     "," ...
>     ")" ...
>     <ID> ...
>     ">" ...
>
> column 23 is "!" in scmr!
>
>
> okay,  here is what seems to be working for me:
>
> <#macro sentiment scmr model><#if scmr != ''>
>   <#assign senti = scmr.results[model]>
>     <#if senti??>
>       <td>${senti.sentimentType}</td>
>       <td>${senti.score?html}</td>
>     <#else><td align="center" colspan="2">none</td>
>     </#if>
> </#if></#macro>
>
>
>       <@sentiment model=model scmr=result.title! />
>       <@sentiment model=model scmr=result.body! />
>       <@sentiment model=model scmr=result.mean! />
>
> the big question:
> will the "<#if senti??>" test in the macro work as intended?

That line alone would, but <#assign senti = scmr.results[model]> will
fail if it evaluates to a null. In general, wherever you can get a
null during normal operation, you have to specify a default with "!".

> what is the value of "senti" anyway? a string? the java bean object?

I can't know, since I don't know your data-model. Looking at the macro
I guess it's a JavaBean.

> thanks!
>

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: macro errors

Sam Steingold
The following message is a courtesy copy of an article
that has been posted to gmane.comp.web.freemarker.user as well.

> * Daniel Dekany <[hidden email]> [2012-07-25 22:59:46 +0200]:
>
>>
>> <#macro sentiment scmr model><#if scmr != ''>
>>   <#assign senti = scmr.results[model]>
>>     <#if senti??>
>>       <td>${senti.sentimentType}</td>
>>       <td>${senti.score?html}</td>
>>     <#else><td align="center" colspan="2">none</td>
>>     </#if>
>> </#if></#macro>
>>
>>
>>       <@sentiment model=model scmr=result.title! />
>>       <@sentiment model=model scmr=result.body! />
>>       <@sentiment model=model scmr=result.mean! />
>>
>> the big question:
>> will the "<#if senti??>" test in the macro work as intended?
>
> That line alone would, but <#assign senti = scmr.results[model]> will
> fail if it evaluates to a null. In general, wherever you can get a
> null during normal operation, you have to specify a default with "!".

you mean it should be

<#assign senti = scmr.results[model]! />

right?

>> what is the value of "senti" anyway? a string? the java bean object?
>
> I can't know, since I don't know your data-model. Looking at the macro
> I guess it's a JavaBean.

what I meant was whether it was a java object or an html/freemarker string.


thanks!

--
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://palestinefacts.org http://iris.org.il
http://dhimmi.com http://americancensorship.org http://www.memritv.org
Time would have been the best Teacher, if it did not kill all its students.

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: macro errors

Daniel Dekany
Wednesday, July 25, 2012, 11:09:59 PM, Sam Steingold wrote:

>> * Daniel Dekany <[hidden email]> [2012-07-25 22:59:46 +0200]:
>>
>>>
>>> <#macro sentiment scmr model><#if scmr != ''>
>>>   <#assign senti = scmr.results[model]>
>>>     <#if senti??>
>>>       <td>${senti.sentimentType}</td>
>>>       <td>${senti.score?html}</td>
>>>     <#else><td align="center" colspan="2">none</td>
>>>     </#if>
>>> </#if></#macro>
>>>
>>>
>>>       <@sentiment model=model scmr=result.title! />
>>>       <@sentiment model=model scmr=result.body! />
>>>       <@sentiment model=model scmr=result.mean! />
>>>
>>> the big question:
>>> will the "<#if senti??>" test in the macro work as intended?
>>
>> That line alone would, but <#assign senti = scmr.results[model]> will
>> fail if it evaluates to a null. In general, wherever you can get a
>> null during normal operation, you have to specify a default with "!".
>
> you mean it should be
>
> <#assign senti = scmr.results[model]! />
>
> right?

Only if the default value that you want is empty string + empty
sequence + empty hash (a multi-type value). See:
http://freemarker.org/docs/dgui_template_exp.html#dgui_template_exp_missing

Also note that if you have a default value, then <#if senti??> will
never be executed, of course.

>>> what is the value of "senti" anyway? a string? the java bean object?
>>
>> I can't know, since I don't know your data-model. Looking at the macro
>> I guess it's a JavaBean.
>
> what I meant was whether it was a java object or an html/freemarker string.

Assuming it's a JavaBean in Java, it's a hash (+ string) in the
type-system of FreeMarker. Assuming you are using the default object
wrapper at least.

> thanks!
>

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user