Documentation
¶
Overview ¶
Includes functions for actually writing data to files of various formats.
Utility package for writing data to files in different formats. To synchronize multiple places that can write to the same file, pass around a reference to the same `FileWriter` instance.
Index ¶
- func GetDefaultOutputDir() (string, error)
- func PrependDefaultOutputDir(path string) (string, error)
- func WriteToFile(path string, data any, otherData ...any) error
- type FileFormat
- type FileWriter
- func (writer *FileWriter) Close()
- func (writer *FileWriter) DetectFormat() FileFormat
- func (writer *FileWriter) GetPath() string
- func (writer *FileWriter) GetPathDir() string
- func (writer *FileWriter) SetPath(path string) error
- func (writer *FileWriter) Write(data any, otherData ...any) error
- func (writer *FileWriter) WriteMultiple(data any, otherData ...any) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetDefaultOutputDir ¶
Gets the default output directory relative to the project root (determined at runtime)
func PrependDefaultOutputDir ¶
If the provided path is not absolute, prepends the default output directory (determined at runtime) to it. If the path is already absolute, returns it unchanged. todo maybe rename to something like like PrepareFilePath with arg to create the dir (for use in `main`)
func WriteToFile ¶
Writes some piece of data to a file (overwriting if it already exists), with the file format automatically detected based on the file extension. This is a shortcut for creating a new FileWriter instance, writing to it, and closing it.
Types ¶
type FileFormat ¶
type FileFormat int
Represents the format of an output file.
const ( FormatUnknown FileFormat = iota FormatTxt FormatCSV FormatJSON )
Represents the different file file formats supported by the writer. Other packages that use this may choose to only support a subset of these formats.
func DetectFormat ¶
func DetectFormat(path string) FileFormat
Determine a file's format based on file extension. Returns FormatUnknown if the file extension is not recognized or not supported. todo consider making this more robust, maybe with a registry and/or dynamic String() method
type FileWriter ¶
type FileWriter struct {
// contains filtered or unexported fields
}
Provides a simple, thread-safe way to write data to files in different formats. File format is automatically detected based on the file extension. This struct provides thread-safe methods for writing data to a file concurrently using shared references to a FileWriter instance, but only one distinct `FileWriter` instance should refer to any particular file at a time.
func NewFileWriter ¶
func NewFileWriter(path string, append bool) (*FileWriter, error)
Creates a new FileWriter instance with the specified fields. If the path is not absolute, the file will be placed in the default output directory (which is determined at runtime).
func (*FileWriter) Close ¶
func (writer *FileWriter) Close()
Closes the output file and any associated resources, or do nothing if they are already closed. Sets the `file` field to `nil` to indicate that the file is no longer open. This should only be called when the FileWriter is no longer needed and all data has been written.
func (*FileWriter) DetectFormat ¶
func (writer *FileWriter) DetectFormat() FileFormat
Alias for DetectFormat function using the FileWriter's path.
func (*FileWriter) GetPath ¶
func (writer *FileWriter) GetPath() string
Gets the output file path for this FileWriter instance in a thread-safe manner.
func (*FileWriter) GetPathDir ¶ added in v1.1.1
func (writer *FileWriter) GetPathDir() string
Gets the parent directory of the output file path for this FileWriter instance in a thread-safe manner.
func (*FileWriter) SetPath ¶
func (writer *FileWriter) SetPath(path string) error
Sets the output file path and format for this FileWriter instance in a thread-safe manner, then opens the file and initializes related fields. Prepends the default output directory (determined at runtime) if the provided path isn't absolute.
func (*FileWriter) Write ¶
func (writer *FileWriter) Write(data any, otherData ...any) error
Writes data to the file associated with this FileWriter instance, with file format automatically detected. Writes are performed concurrently, so The provided arguments will have different type and structure requirements depending on the file format:
- For text files, `data` must be a string or []string where each element is a line of text, and `otherData` is ignored.
- For CSV files, `data` must be a []string representing a single record with each string being a field, and `otherData[0]` must be a string[] representing CSV headers that will be written if the file is empty.
- For JSON files, `data` is any value that can be marshaled to JSON, which will be appended to any existing data as an array. If `data` is a slice, `otherData[0]` must be a boolean indicating whether to flatten the slice elements (by one level, to avoid a nested array) if appending to any existing data.
func (*FileWriter) WriteMultiple ¶ added in v1.1.1
func (writer *FileWriter) WriteMultiple(data any, otherData ...any) error
Writes each element of `data` (which must be a slice) as a separate item to the file associated with this FileWriter instance, using the same logic as `Write` for each element. `otherData` is passed as-is to each underlying `Write` call.
See Write for details on supported formats and argument conventions.
Stops execution immediately on the first error encountered while writing any element. todo maybe remove this entirely if it isn't clearly better than looping over `Write` (except the log messages)