Skip to content

Commit

Permalink
Moved the logic to append and write.
Browse files Browse the repository at this point in the history
  • Loading branch information
cinar committed Dec 22, 2023
1 parent 050ce1a commit f99b4ec
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
6 changes: 3 additions & 3 deletions helper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fmt.Println(actual) // [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
```

<a name="AppendOrWriteToCsvFile"></a>
## func [AppendOrWriteToCsvFile](<https://github.com/cinar/indicator/blob/v2/helper/csv.go#L294>)
## func [AppendOrWriteToCsvFile](<https://github.com/cinar/indicator/blob/v2/helper/csv.go#L287>)

```go
func AppendOrWriteToCsvFile[T any](fileName string, hasHeader bool, rows <-chan *T) error
Expand Down Expand Up @@ -553,7 +553,7 @@ fmt.Println(helper.ChanToSlice(squared)) // [4, 9, 25, 100]
```

<a name="ReadFromCsvFile"></a>
## func [ReadFromCsvFile](<https://github.com/cinar/indicator/blob/v2/helper/csv.go#L281>)
## func [ReadFromCsvFile](<https://github.com/cinar/indicator/blob/v2/helper/csv.go#L274>)

```go
func ReadFromCsvFile[T any](fileName string, hasHeader bool) (<-chan *T, error)
Expand Down Expand Up @@ -863,7 +863,7 @@ func (c *Csv[T]) ReadFromReader(reader io.Reader) <-chan *T
ReadFromReader parses the CSV data from the provided reader, maps the data to corresponding struct fields, and delivers the resulting it through the channel.

<a name="Csv[T].WriteToFile"></a>
### func \(\*Csv\[T\]\) [WriteToFile](<https://github.com/cinar/indicator/blob/v2/helper/csv.go#L176>)
### func \(\*Csv\[T\]\) [WriteToFile](<https://github.com/cinar/indicator/blob/v2/helper/csv.go#L186>)

```go
func (c *Csv[T]) WriteToFile(fileName string, rows <-chan *T) error
Expand Down
51 changes: 22 additions & 29 deletions helper/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,33 @@ func (c *Csv[T]) ReadFromFile(fileName string) (<-chan *T, error) {
// file's column order matches the field order of the given row struct to ensure consistent
// data structure.
func (c *Csv[T]) AppendToFile(fileName string, rows <-chan *T) error {
return c.writeToFileWithFlag(fileName, true, rows)
file, err := os.OpenFile(filepath.Clean(fileName), os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
return err
}

err = c.writeToWriter(file, false, rows)
if err != nil {
return err
}

return file.Close()
}

// WriteToFile creates a new file with the given name and writes the provided rows
// of data to it, overwriting any existing content.
func (c *Csv[T]) WriteToFile(fileName string, rows <-chan *T) error {
return c.writeToFileWithFlag(fileName, false, rows)
file, err := os.OpenFile(filepath.Clean(fileName), os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return err
}

err = c.writeToWriter(file, true, rows)
if err != nil {
return err
}

return file.Close()
}

// updateColumnIndexes aligns column indices to match the order of column headers.
Expand All @@ -201,33 +221,6 @@ func (c *Csv[T]) updateColumnIndexes(csvReader *csv.Reader) error {
return nil
}

// writeToFileWithFlag writes the provided rows of data to a file with the given name, using the
// specified flag mode for precise control over file opening and writing behavior.
func (c *Csv[T]) writeToFileWithFlag(fileName string, appendRows bool, rows <-chan *T) error {
var file *os.File
var err error

// Gosec complains if flag is a variable.
if appendRows {
file, err = os.OpenFile(filepath.Clean(fileName), os.O_APPEND|os.O_WRONLY, 0600)
} else {
file, err = os.OpenFile(filepath.Clean(fileName), os.O_CREATE|os.O_WRONLY, 0600)
}

if err != nil {
return err
}

writeHeader := c.hasHeader && !appendRows

err = c.writeToWriter(file, writeHeader, rows)
if err != nil {
return err
}

return file.Close()
}

// writeToWriter writes the provided rows of data to the specified writer, with the option
// to include or exclude headers for flexibility in data presentation.
func (c *Csv[T]) writeToWriter(writer io.Writer, writeHeader bool, rows <-chan *T) error {
Expand Down

0 comments on commit f99b4ec

Please sign in to comment.