[stringtemplate-interest] question about toString and maps
Jonathan Buhacoff
jonathan at buhacoff.net
Mon Oct 19 14:01:02 PDT 2009
A little update - I added just a handful of lies to my copy of ASTExpr
this morning and added the LabledMap interface, and it's working great.
In my project I'm using that with Apache Ant and a properties file to
generate offline samples of my user interface.
On Oct 19, 2009, at 8:24 AM, Jonathan Buhacoff wrote:
> Hi, I'm trying to create a file with data that mimicks what my Java
> objects produce, for testing with my templates "offline".
>
> My templates take advantage of the reflection and the toString
> features.
>
> Normally I wouldn't be able to reproduce the toString() output from a
> properties file, so I created a special implementation of Map to
> produce the toString values. But... ST doesn't call toString() on the
> map, it calls values().
>
> Concrete example of my "online" processing:
>
> template.setAttribute("locale", (java.util.Locale)locale);
>
> $locale$ -> en_US because that's what locale.toString() returns
> $locale.language$ -> en
> $locale.country$ -> US
>
> Now with a property file, using my special map implementation:
>
> locale=en_US this entry is meant to produce the same as
> locale.toString()
> locale.language=en
> locale.country=US
>
> $locale$ -> USen because ST called values() on the map I created
> from the properties file
> $locale.language$ -> en
> $locale.country$ -> US
>
> I tried using the _default_ key, like this:
>
> locale._default_=en_US
>
> But it's being ignored because the map is iterable...
>
> I'd like to extend ASTExpr.rawGetObjectProperty() and
> convertAnything*ToIterator() to have an additional case for a map that
> is not iterable. I'm thinking it can implement a marker interface,
> like LabeledMap or NonIterator, so that any object that implements a
> Map can also choose to have this behavior of providing a label with
> toString() instead of having its values iterated wherever it appears
> solely by name in the template. The template author can still write
> map.keys and map.values to explicitly iterate the map so I think that
> would be ok.
>
> Any comments on this? Is there another way? If this sounds ok, I'll
> need to do it right away in my project. I could send a patch if
> you're interested in allowing this in the next version.
>
>
> --
> Jonathan Buhacoff
> jonathan at buhacoff.net
> --
>
>
>
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
More information about the stringtemplate-interest
mailing list