[stringtemplate-interest] (c#) problem with separator clause

John Snyders jjsnyders at rcn.com
Sat Jun 23 18:42:48 PDT 2007


I use Java but this problem is with how you defined your template not 
the C# implementation.
Using STST to demonstrate:
gdn.js
{ Customers: [
  { CustomerName: "Martha" },
  { CustomerName: "Agnes" },
  { CustomerName: "Margaret" }] }
gdn.st:
Your example
$Customers:{$it.CustomerName; separator="|"$}$
What you intended
$Customers:{$it.CustomerName$}; separator="|"$

stst gdn gdn.js
Your example
MarthaAgnesMargaret
What you intended
Martha|Agnes|Margaret

In the first example the separator option is applied to the scalar value 
of it.CustomerName. Because it is not a list the separator does nothing. 
It is the same as $Customers:{$it.CustomerName$}$
The separator option needs to be applied to lists. This is why the 
second case works.
The case you tried with List<String> was not equivalent.
$Customers; separator="|"$ is the same as $Customers: {$it$}; 
separator="|"$ not
$Customers: {$it; separator="|"$}$

Hope this helps explain how StringTemplate works
-John

GDN wrote:
> Hi,
>  
> I have recently started play with String Template and of course I have 
> found many problems :-). First of them is issue with separator clause 
> if data is more complicated type. For example this test is not successful:
>  
>
>         [TestMethod]
>
>         public void SeparatorCustomersListTest()
>
>         {
>
>             List<OneCustomer> rows = new List<OneCustomer>();
>
>             rows.Add(new OneCustomer("Martha"));
>
>             rows.Add(new OneCustomer("Agnes"));
>
>             rows.Add(new OneCustomer("Margaret"));
>
>  
>
>             string template = @"$Customers:{" +
>
>                 "$it.CustomerName; separator=\"|\"$}$";
>
>  
>
>             StringTemplate translate = new StringTemplate(template);
>
>             translate.SetAttribute("Customers", rows);
>
>             string actual = translate.ToString();
>
>  
>
>             string expected = "Martha|Agnes|Margaret";
>
>             Assert.AreEqual<string>(expected, actual);
>
>         }
>
>  
>
> Actual is "MarthaAgnesMargaret" - separator is missed.
>
>  
>
> Where Customer type definition is:
>  
>
>     public class OneCustomer
>
>     {
>
>         string customerName;
>
>         public OneCustomer(string customerName)
>
>         {
>
>             this.customerName = customerName;
>
>         }
>
>  
>
>         public string CustomerName
>
>         {
>
>             get { return this.customerName; }
>
>         }
>
>     }
>
>  
> If I used List<string> (instead of List<OneCustomer>) result data was 
> so as I expected, example:
>  
>
>         [TestMethod]
>
>         public void SeperatorCustomersStringList()
>
>         {
>
>             List<string> rows = new List<string>();
>
>             rows.Add("Martha");
>
>             rows.Add("Agnes");
>
>             rows.Add("Margaret");
>
>  
>
>             string template = "$Customers; separator=\"|\"$";
>
>  
>
>             StringTemplate translate = new StringTemplate(template);
>
>             translate.SetAttribute("Customers", rows);
>
>             string actual = translate.ToString();
>
>  
>
>             string expected = "Martha|Agnes|Margaret";
>
>             Assert.AreEqual<string>(expected, actual);
>
>         }
>
>  
>
> In this test separator exists properly.
>  
> Is it problem or feature?
>  
> Regards,
> Gregory
> ------------------------------------------------------------------------
>
> _______________________________________________
> 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