[stringtemplate-interest] difficulty using format and null options together
John Snyders
jjsnyders at rcn.com
Thu Mar 20 18:55:29 PDT 2008
When you look at the implementation of all the options (format, null
etc.) in ASTExpr.java it seems very natural to handle the null option
first before format. The code checks for a null value a few steps before
it has a string to render.
It is documented that the null option is applied first then format then
separator.
In the case where the format option is transforming characters like
changing case I think it makes sense to apply null first. I can see that
in your case you clearly don't want to quote the NULL. The trouble is
that you need some out of band value to pass to format. In general you
don't have one and null is really your only 100% safe option. If you
change the format renderer to check for the string NULL and don't quote
it then there is a remote but possible chance that one of the non-null
strings could have the value "NULL" in which case it would not get
quoted when it should.
Changing ASTExpr so that it passes null to the format renderer could be
tricky and in addition I don't think that existing renderers ever expect
to be called with null.
I wonder if the following would work.
When the null option is used the string value is wrapped in a class such
as NullDefault. This simple class acts much like a string and toString
just renders the wrapped string. So because of the way ST default
rendering works it should just call toString and get the intended
default value. When the NullDefault object is passed to the format
renderer it will be able to distinguish it from a normal string. This
could still have some backward compatibility problems depending on if
people wrote robust renderers that called toString on objects they
didn't recognize.
Terence, what do you think? Is this worth doing or will it cause problems?
-John
Mark Wharton wrote:
> Hello,
>
> I'm having difficulty using format and null options together in generating an SQL statement with quoted values and NULL keyword. Here's a sample:
>
> String ts = "$values; format=\"quote\", null=\"NULL\", separator=\", \"$";
> StringTemplate t = new StringTemplate(ts);
> t.registerRenderer(String.class, new ValueRenderer());
> List values = new ArrayList();
> values.add("x");
> values.add("y");
> values.add(null);
> values.add("z");
> values.add(null);
> t.setAttribute("values", values);
> System.out.println("t: "+t.toString());
>
> Actual Result...
>
> t: "x", "y", "NULL", "z", "NULL"
>
> Desired Result...
>
> t: "x", "y", NULL, "z", NULL
>
> It looks like the null option is applied before the format option and consequently the NULL becomes quoted because it is a string when the renderer processes it. I don't see how it is possible to have non quoted NULL in the output for null values in the list when the format option is also used in the same expression.
>
> Thanks,
>
> Mark
>
> Mark Wharton
> + 8190 9834 2559
> contact at moonbase.com.au
>
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
>
>
More information about the stringtemplate-interest
mailing list