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

feat!: use CSVWriter interface instead of csv.Writer in SafeCSVWriter #233

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ var selfCSVWriter = DefaultCSVWriter

// DefaultCSVWriter is the default SafeCSVWriter used to format CSV (cf. csv.NewWriter)
func DefaultCSVWriter(out io.Writer) *SafeCSVWriter {
writer := NewSafeCSVWriter(csv.NewWriter(out))
originalWriter := csv.NewWriter(out)

// As only one rune can be defined as a CSV separator, we are going to trim
// the custom tag separator and use the first rune.
if runes := []rune(strings.TrimSpace(TagSeparator)); len(runes) > 0 {
writer.Comma = runes[0]
originalWriter.Comma = runes[0]
}

return writer
return NewSafeCSVWriter(originalWriter)
}

// SetCSVWriter sets the SafeCSVWriter used to format CSV.
Expand Down
6 changes: 3 additions & 3 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,9 @@ func TestRenamedTypesMarshal(t *testing.T) {
}

SetCSVWriter(func(out io.Writer) *SafeCSVWriter {
csvout := NewSafeCSVWriter(csv.NewWriter(out))
csvout.Comma = ';'
return csvout
writer := csv.NewWriter(out)
writer.Comma = ';'
return NewSafeCSVWriter(writer)
})
// Switch back to default for tests executed after this
defer SetCSVWriter(DefaultCSVWriter)
Expand Down
11 changes: 5 additions & 6 deletions safe_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gocsv

//Wraps around SafeCSVWriter and makes it thread safe.
import (
"encoding/csv"
"sync"
)

Expand All @@ -13,26 +12,26 @@ type CSVWriter interface {
}

type SafeCSVWriter struct {
*csv.Writer
CSVWriter
m sync.Mutex
}

func NewSafeCSVWriter(original *csv.Writer) *SafeCSVWriter {
func NewSafeCSVWriter(original CSVWriter) *SafeCSVWriter {
return &SafeCSVWriter{
Writer: original,
CSVWriter: original,
}
}

//Override write
func (w *SafeCSVWriter) Write(row []string) error {
w.m.Lock()
defer w.m.Unlock()
return w.Writer.Write(row)
return w.CSVWriter.Write(row)
}

//Override flush
func (w *SafeCSVWriter) Flush() {
w.m.Lock()
w.Writer.Flush()
w.CSVWriter.Flush()
w.m.Unlock()
}