From 0069819696b647d90ca899b632f4919ec969cfe5 Mon Sep 17 00:00:00 2001 From: Onur Cinar Date: Wed, 20 Dec 2023 23:30:08 -0800 Subject: [PATCH] Fixing static analyzer issues. --- helper/README.md | 6 +++--- helper/csv.go | 16 +++++++--------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/helper/README.md b/helper/README.md index 303654a..c9fa77e 100644 --- a/helper/README.md +++ b/helper/README.md @@ -152,7 +152,7 @@ fmt.Println(actual) // [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] ``` -## func [AppendOrWriteToCsvFile]() +## func [AppendOrWriteToCsvFile]() ```go func AppendOrWriteToCsvFile[T any](fileName string, hasHeader bool, rows <-chan *T) error @@ -553,7 +553,7 @@ fmt.Println(helper.ChanToSlice(squared)) // [4, 9, 25, 100] ``` -## func [ReadFromCsvFile]() +## func [ReadFromCsvFile]() ```go func ReadFromCsvFile[T any](fileName string, hasHeader bool) (<-chan *T, error) @@ -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. ### func \(\*Csv\[T\]\) [ReadFromReader]() diff --git a/helper/csv.go b/helper/csv.go index 12f5527..3763b6d 100644 --- a/helper/csv.go +++ b/helper/csv.go @@ -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 { @@ -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. @@ -201,16 +201,14 @@ 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) @@ -218,7 +216,7 @@ func (c *Csv[T]) writeToFile(fileName string, flag int, rows <-chan *T) error { return err } - return file.Sync() + return file.Close() } // writeToWriter writes the provided rows of data to the specified writer, with the option