antlr / antlr/stringtemplate4

Separator not emitted for nulls until a value is hit

Open
#303 4 comments 0 reactions 0 assignees View on GitHub
type:bug
Dominant language
Java
Stars
1k
Forks
234
PR merge metrics
No merged PRs in 30d

Description

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) ::= <%
}; separator=","><\n>
%>

outputTemplate(items) ::= <%
<\n>

%>
```

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](https://user-images.githubusercontent.com/1444082/186497444-ad22a676-0d15-45af-a63e-377c2b4056b8.png)

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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.