Number types with custom formatting

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

Number types with custom formatting

Smerek, Martin


Hi all,

We'd like to use FreeMarker for our project. We have special formatting for different number types (internal id, multiple currencies, quantity) and we'd like to offer our users a comfort of automatic formatting without making them to call build-ins, e.g. use simply

${transaction.total}
${transaction.quantity}

instead of

${transaction.total?string.currency}
${transaction.quantity?string.number}

We tried to implement both TemplateNumberModel and TemplateScalarModel interfaces, but in freemarker.core.Expression.getStringValue(TemplateModel, Expression, Environment), TemplateNumberModel always takes priority before TemplateScalarModel. This results in one global format for all number types.

Quick and dirty solution is to move the above mentioned condition with TemplateScalarModel before the one with TemplateNumberModel. However, we aren't sure why the sequence of the conditions looks as it does and if the change wouldn't cause some other problem. Any thoughts on this?

Is there any way how to treat a number as number (to support comparison and arithmetics) but format it in completely custom way?

Thank you very much,
Martin Smerek


NOTICE: This email and any attachments may contain confidential and proprietary information of NetSuite Inc. and is for the sole use of the intended recipient for the stated purpose. Any improper use or distribution is prohibited. If you are not the intended recipient, please notify the sender; do not review, copy or distribute; and promptly delete or destroy all transmitted information. Please note that all communications and information transmitted through this email system may be monitored by NetSuite or its agents and that all incoming email is automatically scanned by a third party spam and filtering service.
------------------------------------------------------------------------------
Minimize network downtime and maximize team effectiveness.
Reduce network management and security costs.Learn how to hire
the most talented Cisco Certified professionals. Visit the
Employer Resources Portal
http://www.cisco.com/web/learning/employer_resources/index.html
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Number types with custom formatting

Daniel Dekany
Friday, April 5, 2013, 12:10:21 AM, Smerek, Martin wrote:

> Hi all,
>
> We'd like to use FreeMarker for our project. We have special
> formatting for different number types (internal id, multiple
> currencies, quantity) and we'd like to offer our users a comfort of
> automatic formatting without making them to call build-ins, e.g. use simply
>
> ${transaction.total}
> ${transaction.quantity}
>
> instead of
>
> ${transaction.total?string.currency}
> ${transaction.quantity?string.number}
>
> We tried to implement both TemplateNumberModel and
> TemplateScalarModel interfaces, but in
> freemarker.core.Expression.getStringValue(TemplateModel, Expression,
> Environment), TemplateNumberModel always takes priority before
> TemplateScalarModel. This results in one global format for all number types.
>
> Quick and dirty solution is to move the above mentioned condition
> with TemplateScalarModel before the one with TemplateNumberModel.
> However, we aren't sure why the sequence of the conditions looks as
> it does and if the change wouldn't cause some other problem. Any
> thoughts on this?

I don't know why the order is like this (I don't remember, if I was
here at all) but I think it's good as it is. TemplateModel-s meant to
deal with the MVC "M" part, not with "V", and it's the template (and
partially the FreeMarker settings) meant to control how are things
rendered. That practically means that some models may give string
values that are just last-resort values as far as displaying goes,
like toString() return values. It would be quite annoying/confusing if
the template uses that ignoring the number_format and date_format
settings and the current locale and time-zone. But I understand where
are you coming from, but see that later.

Regarding what will break if for now you just reorder those
conditions... If you are only using stock object-wrappings plus your
wrappings (which are designed to do the right thing when
TemplateScalarModel takes precedence), and you aren't using Jython or
Rhion, I don't think you will run into problems. That is, until
someone at the company later upgrades FreeMarker and thus breaks
things, or want to upgrade FreeMarker but won't because he has no
time/courage for re-patching it.

> Is there any way how to treat a number as number (to support
> comparison and arithmetics) but format it in completely custom way?

Nope... I think, the best would be if you can attach this
meta-information, the application domain type, to the number (or date)
value, and then, you can register formatters for application domain
types in the FreeMarker settings. So the model says "this number is a
price", and the FreeMarker settings say "format prices like this".

For this I need to add some new classes/interfaces to freemarker.template:

   class ApplicationDomainType(final String name)

and

  public interface ApplicationDomainTypeAwareTemplateModel extends TemplateModel {
      ApplicationDomainType getApplicationDomainType();
  }

and

  interface ApplicationDomainTypeFormatter {
      String format(
            ApplicationDomainTypeAwareTemplateModel value,
            Environment env,  // so you can get the locale, time-zone, etc.
            TemplateModel format  // used only for val?string(format)
            )
      throws TemplateModelException;
  }

and of course I have to add this to Configurable:

  addApplicationDomainTypeFormatter(ApplicationDomainType, ApplicationDomainTypeFormatter)
  ... etc.

Would that solve the problem? Any opinions?

Then, there's the question, when can I add this. I have a list of
things that must be done for 2.3.19, like even important bugfixes, but
I don't have much time in the near future. So, certainly 2.3.19 will
be released around the end of May, and I think I can fit this in.
That's almost 2 months away.

--
Thanks,
 Daniel Dekany


> Thank you very much,
> Martin Smerek
>
>
> NOTICE: This email and any attachments may contain confidential and
> proprietary information of NetSuite Inc. and is for the sole use of
> the intended recipient for the stated purpose. Any improper use or
> distribution is prohibited. If you are not the intended recipient,
> please notify the sender; do not review, copy or distribute; and
> promptly delete or destroy all transmitted information. Please note
> that all communications and information transmitted through this
> email system may be monitored by NetSuite or its agents and that all
> incoming email is automatically scanned by a third party spam and filtering service.


------------------------------------------------------------------------------
Minimize network downtime and maximize team effectiveness.
Reduce network management and security costs.Learn how to hire
the most talented Cisco Certified professionals. Visit the
Employer Resources Portal
http://www.cisco.com/web/learning/employer_resources/index.html
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Number types with custom formatting

Smerek, Martin


Hi Daniel,

Thanks for your answer. Do I understand correctly that you would add something like

if (referentModel instanceof ApplicationDomainTypeAwareTemplateModel) {
return env.getApplicationDomainFormatter(referentModel. getApplicationDomainType()).format(referentModel, env, ...);
}

at the beginning of Expression.getStringValue(TemplateModel, Expression, Environment) method? If so, it would definitely solve our problem and it would be really helpful.

Would it also be possible to add ability to set the format using setting directive, e.g. if you have ApplicationDomainType with name "price", you could use something like <#setting application_domain_format_price="0.##">?

If it helps you, we're willing to implement the changes based on your design.

Thank you very much,
Martin

-----Original Message-----
From: Daniel Dekany [mailto:[hidden email]]
Sent: Saturday, April 06, 2013 9:02 PM
To: FreeMarker-user
Subject: Re: [FreeMarker-user] Number types with custom formatting

Friday, April 5, 2013, 12:10:21 AM, Smerek, Martin wrote:

> Hi all,
>
> We'd like to use FreeMarker for our project. We have special
> formatting for different number types (internal id, multiple
> currencies, quantity) and we'd like to offer our users a comfort of
> automatic formatting without making them to call build-ins, e.g. use
> simply
>
> ${transaction.total}
> ${transaction.quantity}
>
> instead of
>
> ${transaction.total?string.currency}
> ${transaction.quantity?string.number}
>
> We tried to implement both TemplateNumberModel and TemplateScalarModel
> interfaces, but in
> freemarker.core.Expression.getStringValue(TemplateModel, Expression,
> Environment), TemplateNumberModel always takes priority before
> TemplateScalarModel. This results in one global format for all number types.
>
> Quick and dirty solution is to move the above mentioned condition with
> TemplateScalarModel before the one with TemplateNumberModel.
> However, we aren't sure why the sequence of the conditions looks as it
> does and if the change wouldn't cause some other problem. Any thoughts
> on this?

I don't know why the order is like this (I don't remember, if I was here at all) but I think it's good as it is. TemplateModel-s meant to deal with the MVC "M" part, not with "V", and it's the template (and partially the FreeMarker settings) meant to control how are things rendered. That practically means that some models may give string values that are just last-resort values as far as displaying goes, like toString() return values. It would be quite annoying/confusing if the template uses that ignoring the number_format and date_format settings and the current locale and time-zone. But I understand where are you coming from, but see that later.

Regarding what will break if for now you just reorder those conditions... If you are only using stock object-wrappings plus your wrappings (which are designed to do the right thing when TemplateScalarModel takes precedence), and you aren't using Jython or Rhion, I don't think you will run into problems. That is, until someone at the company later upgrades FreeMarker and thus breaks things, or want to upgrade FreeMarker but won't because he has no time/courage for re-patching it.

> Is there any way how to treat a number as number (to support
> comparison and arithmetics) but format it in completely custom way?

Nope... I think, the best would be if you can attach this meta-information, the application domain type, to the number (or date) value, and then, you can register formatters for application domain types in the FreeMarker settings. So the model says "this number is a price", and the FreeMarker settings say "format prices like this".

For this I need to add some new classes/interfaces to freemarker.template:

class ApplicationDomainType(final String name)

and

public interface ApplicationDomainTypeAwareTemplateModel extends TemplateModel {
ApplicationDomainType getApplicationDomainType();
}

and

interface ApplicationDomainTypeFormatter {
String format(
ApplicationDomainTypeAwareTemplateModel value,
Environment env, // so you can get the locale, time-zone, etc.
TemplateModel format // used only for val?string(format)
)
throws TemplateModelException;
}

and of course I have to add this to Configurable:

addApplicationDomainTypeFormatter(ApplicationDomainType, ApplicationDomainTypeFormatter)
... etc.

Would that solve the problem? Any opinions?

Then, there's the question, when can I add this. I have a list of things that must be done for 2.3.19, like even important bugfixes, but I don't have much time in the near future. So, certainly 2.3.19 will be released around the end of May, and I think I can fit this in.
That's almost 2 months away.

--
Thanks,
Daniel Dekany


> Thank you very much,
> Martin Smerek
>
>
> NOTICE: This email and any attachments may contain confidential and
> proprietary information of NetSuite Inc. and is for the sole use of
> the intended recipient for the stated purpose. Any improper use or
> distribution is prohibited. If you are not the intended recipient,
> please notify the sender; do not review, copy or distribute; and
> promptly delete or destroy all transmitted information. Please note
> that all communications and information transmitted through this
> email system may be monitored by NetSuite or its agents and that all
> incoming email is automatically scanned by a third party spam and filtering service.


------------------------------------------------------------------------------
Minimize network downtime and maximize team effectiveness.
Reduce network management and security costs.Learn how to hire
the most talented Cisco Certified professionals. Visit the
Employer Resources Portal
http://www.cisco.com/web/learning/employer_resources/index.html
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user


NOTICE: This email and any attachments may contain confidential and proprietary information of NetSuite Inc. and is for the sole use of the intended recipient for the stated purpose. Any improper use or distribution is prohibited. If you are not the intended recipient, please notify the sender; do not review, copy or distribute; and promptly delete or destroy all transmitted information. Please note that all communications and information transmitted through this email system may be monitored by NetSuite or its agents and that all incoming email is automatically scanned by a third party spam and filtering service.
------------------------------------------------------------------------------
Precog is a next-generation analytics platform capable of advanced
analytics on semi-structured data. The platform includes APIs for building
apps and a phenomenal toolset for data science. Developers can use
our toolset for easy data analysis & visualization. Get a free account!
http://www2.precog.com/precogplatform/slashdotnewsletter
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Number types with custom formatting

Daniel Dekany
Tuesday, April 16, 2013, 3:08:04 PM, Smerek, Martin wrote:

>
>
> Hi Daniel,
>
> Thanks for your answer. Do I understand correctly that you would add something like
>
> if (referentModel instanceof
> ApplicationDomainTypeAwareTemplateModel) {
> return env.getApplicationDomainFormatter(referentModel.
> getApplicationDomainType()).format(referentModel, env, ...);
> }
>
> at the beginning of Expression.getStringValue(TemplateModel,
> Expression, Environment) method?

Yes. (Or something like that... like if there's no
ApplicationDomainTypeFormatter for the value, that it should fall back
to the current formatting model.)

> If so, it would definitely solve our problem and it would be really
> helpful.
>
> Would it also be possible to add ability to set the format using
> setting directive, e.g. if you have ApplicationDomainType with name
> "price", you could use something like <#setting
> application_domain_format_price="0.##">?
>
> If it helps you, we're willing to implement the changes based on your design.

I will do it, but thanks.

> Thank you very much,
> Martin
>
> -----Original Message-----
> From: Daniel Dekany [mailto:[hidden email]]
> Sent: Saturday, April 06, 2013 9:02 PM
> To: FreeMarker-user
> Subject: Re: [FreeMarker-user] Number types with custom formatting
>
> Friday, April 5, 2013, 12:10:21 AM, Smerek, Martin wrote:
>
>> Hi all,
>>
>> We'd like to use FreeMarker for our project. We have special
>> formatting for different number types (internal id, multiple
>> currencies, quantity) and we'd like to offer our users a comfort of
>> automatic formatting without making them to call build-ins, e.g. use
>> simply
>>
>> ${transaction.total}
>> ${transaction.quantity}
>>
>> instead of
>>
>> ${transaction.total?string.currency}
>> ${transaction.quantity?string.number}
>>
>> We tried to implement both TemplateNumberModel and TemplateScalarModel
>> interfaces, but in
>> freemarker.core.Expression.getStringValue(TemplateModel, Expression,
>> Environment), TemplateNumberModel always takes priority before
>> TemplateScalarModel. This results in one global format for all number types.
>>
>> Quick and dirty solution is to move the above mentioned condition with
>> TemplateScalarModel before the one with TemplateNumberModel.
>> However, we aren't sure why the sequence of the conditions looks as it
>> does and if the change wouldn't cause some other problem. Any thoughts
>> on this?
>
> I don't know why the order is like this (I don't remember, if I was
> here at all) but I think it's good as it is. TemplateModel-s meant
> to deal with the MVC "M" part, not with "V", and it's the template
> (and partially the FreeMarker settings) meant to control how are
> things rendered. That practically means that some models may give
> string values that are just last-resort values as far as displaying
> goes, like toString() return values. It would be quite
> annoying/confusing if the template uses that ignoring the
> number_format and date_format settings and the current locale and
> time-zone. But I understand where are you coming from, but see that later.
>
> Regarding what will break if for now you just reorder those
> conditions... If you are only using stock object-wrappings plus your
> wrappings (which are designed to do the right thing when
> TemplateScalarModel takes precedence), and you aren't using Jython
> or Rhion, I don't think you will run into problems. That is, until
> someone at the company later upgrades FreeMarker and thus breaks
> things, or want to upgrade FreeMarker but won't because he has no time/courage for re-patching it.
>
>> Is there any way how to treat a number as number (to support
>> comparison and arithmetics) but format it in completely custom way?
>
> Nope... I think, the best would be if you can attach this
> meta-information, the application domain type, to the number (or
> date) value, and then, you can register formatters for application
> domain types in the FreeMarker settings. So the model says "this
> number is a price", and the FreeMarker settings say "format prices like this".
>
> For this I need to add some new classes/interfaces to freemarker.template:
>
> class ApplicationDomainType(final String name)
>
> and
>
> public interface ApplicationDomainTypeAwareTemplateModel extends TemplateModel {
> ApplicationDomainType getApplicationDomainType();
> }
>
> and
>
> interface ApplicationDomainTypeFormatter {
> String format(
> ApplicationDomainTypeAwareTemplateModel value,
> Environment env, // so you can get the locale, time-zone, etc.
> TemplateModel format // used only for val?string(format)
> )
> throws TemplateModelException;
> }
>
> and of course I have to add this to Configurable:
>
> addApplicationDomainTypeFormatter(ApplicationDomainType, ApplicationDomainTypeFormatter)
> ... etc.
>
> Would that solve the problem? Any opinions?
>
> Then, there's the question, when can I add this. I have a list of
> things that must be done for 2.3.19, like even important bugfixes,
> but I don't have much time in the near future. So, certainly 2.3.19
> will be released around the end of May, and I think I can fit this in.
> That's almost 2 months away.
>
> --
> Thanks,
> Daniel Dekany
>
>
>> Thank you very much,
>> Martin Smerek
>>
>>
>> NOTICE: This email and any attachments may contain confidential and
>> proprietary information of NetSuite Inc. and is for the sole use of
>> the intended recipient for the stated purpose. Any improper use or
>> distribution is prohibited. If you are not the intended recipient,
>> please notify the sender; do not review, copy or distribute; and
>> promptly delete or destroy all transmitted information. Please note
>> that all communications and information transmitted through this
>> email system may be monitored by NetSuite or its agents and that all
>> incoming email is automatically scanned by a third party spam and filtering service.
>
>
> ------------------------------------------------------------------------------
> Minimize network downtime and maximize team effectiveness.
> Reduce network management and security costs.Learn how to hire
> the most talented Cisco Certified professionals. Visit the
> Employer Resources Portal
> http://www.cisco.com/web/learning/employer_resources/index.html 
> _______________________________________________
> FreeMarker-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user 
>
>
> NOTICE: This email and any attachments may contain confidential and
> proprietary information of NetSuite Inc. and is for the sole use of
> the intended recipient for the stated purpose. Any improper use or
> distribution is prohibited. If you are not the intended recipient,
> please notify the sender; do not review, copy or distribute; and
> promptly delete or destroy all transmitted information. Please note
> that all communications and information transmitted through this
> email system may be monitored by NetSuite or its agents and that all
> incoming email is automatically scanned by a third party spam and filtering service.
--
Thanks,
 Daniel Dekany


------------------------------------------------------------------------------
Precog is a next-generation analytics platform capable of advanced
analytics on semi-structured data. The platform includes APIs for building
apps and a phenomenal toolset for data science. Developers can use
our toolset for easy data analysis & visualization. Get a free account!
http://www2.precog.com/precogplatform/slashdotnewsletter
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Number types with custom formatting

Daniel Dekany
Wednesday, April 17, 2013, 10:18:13 PM, Daniel Dekany wrote:

> Tuesday, April 16, 2013, 3:08:04 PM, Smerek, Martin wrote:
[snip]
>> Would it also be possible to add ability to set the format using
>> setting directive, e.g. if you have ApplicationDomainType with name
>> "price", you could use something like <#setting
>> application_domain_format_price="0.##">?

Forgot to answer this last time... Sure, that's possible.

--
Thanks,
 Daniel Dekany


------------------------------------------------------------------------------
Precog is a next-generation analytics platform capable of advanced
analytics on semi-structured data. The platform includes APIs for building
apps and a phenomenal toolset for data science. Developers can use
our toolset for easy data analysis & visualization. Get a free account!
http://www2.precog.com/precogplatform/slashdotnewsletter
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user