Skip to content

Commit b8f9e9c

Browse files
committed
use csv encoding from stdlib
1 parent ea3f22a commit b8f9e9c

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

internal/export/cv.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package export
22

33
import (
4+
"encoding/csv"
45
"fmt"
56
"io"
67
"time"
@@ -12,19 +13,25 @@ var _ apis.Exporter = &CVSExporter{}
1213

1314
type CVSExporter struct{}
1415

16+
func NewCVSCExporter() *CVSExporter {
17+
return &CVSExporter{}
18+
}
19+
1520
func (c *CVSExporter) Write(shifts []apis.Shift, writer io.Writer) error {
16-
if _, err := fmt.Fprint(writer, "from,to,primary,secondary\n"); err != nil {
21+
csvWriter := csv.NewWriter(writer)
22+
defer csvWriter.Flush()
23+
24+
if err := csvWriter.Write([]string{"Start", "End", "Primary", "Secondary"}); err != nil {
1725
return fmt.Errorf("write csv header: %w", err)
1826
}
27+
1928
for i := range shifts {
2029
s := shifts[i]
21-
if _, err := fmt.Fprintf(writer, "\"%s\",\"%s\",\"%s\",\"%s\"\n", s.Start.Format(time.DateOnly), s.End.Format(time.DateOnly), s.Primary, s.Secondary); err != nil {
30+
record := []string{s.Start.Format(time.DateOnly), s.End.Format(time.DateOnly), string(s.Primary), string(s.Secondary)}
31+
if err := csvWriter.Write(record); err != nil {
2232
return fmt.Errorf("write csv data: %w", err)
2333
}
2434
}
25-
return nil
26-
}
2735

28-
func NewCVSCExporter() *CVSExporter {
29-
return &CVSExporter{}
36+
return nil
3037
}

internal/export/cv_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ func TestCVSExporter_Write(t *testing.T) {
3636
},
3737
},
3838
},
39-
wantWriter: `from,to,primary,secondary
40-
"2024-12-25","2024-12-26","a","b"
41-
"2024-12-26","2024-12-27","c","d"
39+
wantWriter: `Start,End,Primary,Secondary
40+
2024-12-25,2024-12-26,a,b
41+
2024-12-26,2024-12-27,c,d
4242
`,
4343
wantErr: false,
4444
},
@@ -47,15 +47,15 @@ func TestCVSExporter_Write(t *testing.T) {
4747
args: args{
4848
plan: []apis.Shift{},
4949
},
50-
wantWriter: "from,to,primary,secondary\n",
50+
wantWriter: "Start,End,Primary,Secondary\n",
5151
wantErr: false,
5252
},
5353
{
5454
name: "When plan is nil, print CSV header",
5555
args: args{
5656
plan: nil,
5757
},
58-
wantWriter: "from,to,primary,secondary\n",
58+
wantWriter: "Start,End,Primary,Secondary\n",
5959
wantErr: false,
6060
},
6161
}

0 commit comments

Comments
 (0)