Skip to content

Commit

Permalink
zio/csvio: omit type decorator for union values
Browse files Browse the repository at this point in the history
To format a Zed union, zio/csvio.Writer.Write calls through formatValue
to zson.String, producing a string that includes an unwanted ZSON union
type decorator.  Remove that type decorator by calling zed.Value.Under
in Write.

Closes #4207.
  • Loading branch information
nwt committed Jan 26, 2023
1 parent 3ca4b8a commit 2e4eb0b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
13 changes: 6 additions & 7 deletions zio/csvio/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,14 @@ func (w *Writer) Write(rec *zed.Value) error {
for i, it := 0, rec.Bytes.Iter(); i < len(cols) && !it.Done(); i++ {
var s string
if zb := it.Next(); zb != nil {
typ := cols[i].Type
id := typ.ID()
switch {
case id == zed.IDBytes && len(zb) == 0:
// We want "" instead of "0x" from typ.Format.
val := zed.NewValue(cols[i].Type, zb).Under()
switch id := val.Type.ID(); {
case id == zed.IDBytes && len(val.Bytes) == 0:
// We want "" instead of "0x" for a zero-length value.
case id == zed.IDString:
s = string(zb)
s = string(val.Bytes)
default:
s = formatValue(typ, zb)
s = formatValue(val.Type, val.Bytes)
if zed.IsFloat(id) && strings.HasSuffix(s, ".") {
s = strings.TrimSuffix(s, ".")
}
Expand Down
12 changes: 12 additions & 0 deletions zio/csvio/ztests/union.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
zed: '*'

input: |
{a:1((int64,string))}
{a:"one"((int64,string))}
output-flags: -f csv

output: |
a
1
one

0 comments on commit 2e4eb0b

Please sign in to comment.