Skip to content

Commit

Permalink
apply change request
Browse files Browse the repository at this point in the history
  • Loading branch information
nourbalaha committed Aug 6, 2024
1 parent f433493 commit f17be6d
Showing 1 changed file with 22 additions and 38 deletions.
60 changes: 22 additions & 38 deletions server/pkg/exporters/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,29 @@ func CSVFromItems(pw *io.PipeWriter, items item.VersionedList, s *schema.Schema)
return pointFieldIsNotSupportedError
}

keys, nonGeoFields := buildCSVHeaders(s)
data := [][]string{}
data = append(data, keys)
for _, ver := range items {
row, ok := rowFromItem(ver.Value(), nonGeoFields)
if ok {
data = append(data, row)
}
}

if len(data) == 1 {
return noPointFieldError
}
w := csv.NewWriter(pw)
go func() {
defer pw.Close()

err := convertToCSV(pw, data)
if err != nil {
return err
}
keys, nonGeoFields := buildCSVHeaders(s)
if err := w.Write(keys); err != nil {
pw.CloseWithError(err)
return
}
for _, ver := range items {
row, ok := rowFromItem(ver.Value(), nonGeoFields)
if ok {
if err := w.Write(row); err != nil {
pw.CloseWithError(err)
return
}
}
}
w.Flush()
if err := w.Error(); err != nil {
pw.CloseWithError(err)
}
}()

return nil
}
Expand Down Expand Up @@ -98,27 +103,6 @@ func extractFirstPointField(itm *item.Item) ([]float64, error) {
return nil, noPointFieldError
}

func convertToCSV(pw *io.PipeWriter, data [][]string) error {
w := csv.NewWriter(pw)

go func() {
defer pw.Close()

for _, row := range data {
if err := w.Write(row); err != nil {
pw.CloseWithError(err)
return
}
}
w.Flush()
if err := w.Error(); err != nil {
pw.CloseWithError(err)
}
}()

return nil
}

func float64ToString(f float64) string {
return strconv.FormatFloat(f, 'f', -1, 64)
}
Expand Down

0 comments on commit f17be6d

Please sign in to comment.