Skip to content

Commit

Permalink
Fixing static analyzer issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
cinar committed Dec 21, 2023
1 parent b8b737e commit 0069819
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 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#L287>)
## func [AppendOrWriteToCsvFile](<https://github.com/cinar/indicator/blob/v2/helper/csv.go#L285>)

```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#L274>)
## func [ReadFromCsvFile](<https://github.com/cinar/indicator/blob/v2/helper/csv.go#L272>)

```go
func ReadFromCsvFile[T any](fileName string, hasHeader bool) (<-chan *T, error)
Expand Down Expand Up @@ -851,7 +851,7 @@ AppendToFile appends the provided rows of data to the end of the specified file,
func (c *Csv[T]) ReadFromFile(fileName string) (<-chan *T, error)
```

ReadFromFile parses the CSV data from the provided file name, maps the data to corresponding struct fields, and delivers the resulting snapshots through the channel.
ReadFromFile parses the CSV data from the provided file name, maps the data to corresponding struct fields, and delivers the resulting rows through the channel.

<a name="Csv[T].ReadFromReader"></a>
### func \(\*Csv\[T\]\) [ReadFromReader](<https://github.com/cinar/indicator/blob/v2/helper/csv.go#L91>)
Expand Down
16 changes: 7 additions & 9 deletions helper/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (c *Csv[T]) ReadFromReader(reader io.Reader) <-chan *T {

// ReadFromFile parses the CSV data from the provided file name,
// maps the data to corresponding struct fields, and delivers
// the resulting snapshots through the channel.
// the resulting rows through the channel.
func (c *Csv[T]) ReadFromFile(fileName string) (<-chan *T, error) {
file, err := os.Open(filepath.Clean(fileName))
if err != nil {
Expand All @@ -168,13 +168,13 @@ 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.writeToFile(fileName, os.O_APPEND, rows)
return c.writeToFileWithFlag(fileName, os.O_APPEND, rows)
}

// 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.writeToFile(fileName, os.O_CREATE, rows)
return c.writeToFileWithFlag(fileName, os.O_CREATE, rows)
}

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

// writeToFile writes the provided rows of data to a file with the given name, using the
// 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]) writeToFile(fileName string, flag int, rows <-chan *T) error {
file, err := os.OpenFile(filepath.Clean(fileName), flag|os.O_WRONLY, 0644)
func (c *Csv[T]) writeToFileWithFlag(fileName string, flag int, rows <-chan *T) error {
file, err := os.OpenFile(filepath.Clean(fileName), flag|os.O_WRONLY, 0600)
if err != nil {
return err
}

defer file.Close()

writeHeader := c.hasHeader && (flag == os.O_CREATE)

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

return file.Sync()
return file.Close()
}

// writeToWriter writes the provided rows of data to the specified writer, with the option
Expand Down

0 comments on commit 0069819

Please sign in to comment.