Skip to content

Commit

Permalink
CSV Writer: Allow different types in output
Browse files Browse the repository at this point in the history
Adjust the csvio.Writer so that it can handle records with the same
field names but different types.

Closes #4781
  • Loading branch information
mattnibs committed Nov 17, 2023
1 parent bedcc95 commit 7051f59
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
14 changes: 13 additions & 1 deletion zio/csvio/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (w *Writer) Write(rec *zed.Value) error {
if err := w.encoder.Write(hdr); err != nil {
return err
}
} else if rec.Type != w.first {
} else if rec.Type != w.first && !fieldsEqual(rec.Fields(), w.first.Fields) {
return ErrNotDataFrame
}
w.strings = w.strings[:0]
Expand Down Expand Up @@ -96,3 +96,15 @@ func formatValue(typ zed.Type, bytes zcode.Bytes) string {
}
return zson.FormatValue(zed.NewValue(typ, bytes))
}

func fieldsEqual(a, b []zed.Field) bool {
if len(a) != len(b) {
return false
}
for i, f := range a {
if f.Name != b[i].Name {
return false
}
}
return true
}
14 changes: 14 additions & 0 deletions zio/csvio/ztests/different-types.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
zed: '*'

input: |
{a:0}
{a:"foo"}
{a:127.0.0.1}
output-flags: -f csv

output: |
a
0
foo
127.0.0.1

0 comments on commit 7051f59

Please sign in to comment.