[stringtemplate-interest] question about toString and maps
Jonathan Buhacoff
jonathan at buhacoff.net
Mon Oct 19 08:24:09 PDT 2009
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
--
More information about the stringtemplate-interest
mailing list