Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

omit fields on write #135

Open
shaunc opened this issue Nov 2, 2019 · 2 comments
Open

omit fields on write #135

shaunc opened this issue Nov 2, 2019 · 2 comments

Comments

@shaunc
Copy link

shaunc commented Nov 2, 2019

When I annotate fields with csv:"-" fields are not omitted on write. Should they be? If not, can we have another tag for omitting on write? csv:",omit" and/or csv:",omitempty"?

UPDATE I guess omitempty is not sensible for streaming use, as we have to decide ahead of time for whole file whether to omit a column. But omit (or just "-" if this is what is intended) would be great.

@graywolf336
Copy link

Just ran into this issue where I was under the impression that fields would be omitted from writing when the tag csv:"-" is on a field.

@c9845
Copy link

c9845 commented Feb 21, 2020

The csv:"-" tag works as expected for me; the field is not marshaled into the CSV. Note that I base what I expect on the encoding/json package. I am using the Marshal, MarshalBytes, and MarshalCSV funcs.

I believe the issue with getting the csv:",omitempty" tag to work arises because while JSON is outputted as "object by object" with key/values populated on a per-object basis, a CSV's rows all must have the same columns (and headers) even if a column only has a value in one row. For example, if you have 100 rows of a CSV where the first 90 rows have a column named "Note" set to "" (blank) while the last 10 rows have a value set to "this is a note", well when marshaling happens the marshal func would need to keep track of each column and whether or not it has been used and then determine at the end of marshaling to "omitempty" any unused column (in this case no, show the column). If the last 10 rows were also blank, the marshal func would have to go back through the marshaled data and remove the unused column.

That being said, I'm sure there is a way to keep track of every column's name, omitempty status, and if a value was populated for this column then running through the marshaled data and removing columns with empty values (if omitempty is set). Something like:

type columnName string
type omitemptyData struct {
  omitemptySetOnColumn bool
  anyRowsHaveValue bool
}
type omitemptyChecker map[columnName]omitemptyData

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants