Skip to content

Separator not emitted for nulls until a value is hit #303

@rrs

Description

@rrs

We use string templates for a variety of data exports. One of which is simple CSV.

If we have an input from a table of data where as long as the first value in each row is non null then the CSV is correctly formed. When the first value is a null a separator is not emitted until there is a value.

Imagine the following table of data

Name, Age, Color
Ron, 10, Pink
Paul,15,Brown

and the template

  printRow(row) ::= <%
    <items.Columns:{ k | <row.(k)>}; separator=","><\n>
  %>
  
  outputTemplate(items) ::= <%
    <items.Columns; separator=","><\n>
    <items.Rows:printRow()>
  %>

this will output
Name, Age, Color
Ron, 10, Pink
Paul,15,Brown

if however the input data was
Name, Age, Color
Ron, 10, Pink
null,15,Brown

Then the output would be

Name, Age, Color
Ron, 10, Pink
15,Brown

and not

Name, Age, Color
Ron, 10, Pink
,15,Brown

I can see it is because of this section of code

image

because the seenAValue is false until the first value in the enumerable is non null.

In this case there is a work around, which is to substitute nulls with "" which will generate for the example above

Name, Age, Color
Ron, 10, Pink
"",15,Brown

However I was looking at the possibility of adding another option like emitAll="true" or something like that which would be used along with the separator option to still output, something like

boolean needSeparator = (seenAValue || (emitAll && separator!=null)) &&
               separator!=null &&            // we have a separator and
               (iterValue!=null ||           // either we have a value
                   options[Option.NULL.ordinal()]!=null); // or no value but null option

However I do not have a clear idea of the impact this could have and was hoping someone for familiar with the code could advise me

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions