parser

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 9, 2025 License: MIT Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package parser provides file parsing functionality for various tabular data formats. It supports CSV, TSV, LTSV, XLSX, and Parquet files, with optional compression (gzip, bzip2, xz, zstd).

This package is used internally by filesql but can also be used independently when you need direct access to parsed data without SQL functionality.

Memory Considerations

All parsing functions in this package load the entire dataset into memory. This design is intentional for simplicity and compatibility with formats that require random access (Parquet, XLSX), but has implications for large files:

  • CSV/TSV/LTSV: Entire file content is read into memory
  • XLSX: Entire workbook is loaded (Excel files can be large even with few rows)
  • Parquet: Entire file is read into memory for random access

For files larger than available memory, consider:

  • Using filesql's streaming API (ProcessInChunks) for CSV/TSV
  • Pre-filtering or splitting large files before processing
  • Increasing available memory for the process

Example usage

f, _ := os.Open("data.csv")
defer f.Close()
result, err := parser.Parse(f, parser.CSV)
if err != nil {
    log.Fatal(err)
}
fmt.Println("Columns:", result.Headers)
fmt.Println("Rows:", len(result.Records))

Type Conversion

Use ParseValue to convert string records to typed Go values based on ColumnType.

Index

Constants

View Source
const (
	ExtCSV     = ".csv"
	ExtTSV     = ".tsv"
	ExtLTSV    = ".ltsv"
	ExtParquet = ".parquet"
	ExtXLSX    = ".xlsx"
	ExtGZ      = ".gz"
	ExtBZ2     = ".bz2"
	ExtXZ      = ".xz"
	ExtZSTD    = ".zst"
)

File extensions

Variables

This section is empty.

Functions

func ParseValue

func ParseValue(value string, colType ColumnType) any

ParseValue converts a string value to the appropriate Go type based on ColumnType. This function is useful for converting string records from TableData to typed values.

Conversion rules:

  • TypeInteger: returns int64, or original string if parsing fails
  • TypeReal: returns float64, or original string if parsing fails
  • TypeDatetime: returns string (caller can parse with time.Parse if needed)
  • TypeText: returns string as-is
  • Empty values return nil

Types

type ColumnType

type ColumnType int

ColumnType represents the inferred type of a column.

const (
	// TypeText represents text/string column type.
	TypeText ColumnType = iota
	// TypeInteger represents integer column type.
	TypeInteger
	// TypeReal represents floating-point column type.
	TypeReal
	// TypeDatetime represents datetime column type.
	TypeDatetime
)

func (ColumnType) String

func (ct ColumnType) String() string

String returns the string representation of ColumnType.

type FileType

type FileType int

FileType represents supported file types including compression variants.

const (
	// CSV represents CSV file type.
	CSV FileType = iota
	// TSV represents TSV file type.
	TSV
	// LTSV represents LTSV (Labeled Tab-Separated Values) file type.
	LTSV
	// Parquet represents Apache Parquet file type.
	Parquet
	// XLSX represents Excel XLSX file type.
	XLSX

	// CSVGZ represents gzip-compressed CSV file type.
	CSVGZ
	// CSVBZ2 represents bzip2-compressed CSV file type.
	CSVBZ2
	// CSVXZ represents xz-compressed CSV file type.
	CSVXZ
	// CSVZSTD represents zstd-compressed CSV file type.
	CSVZSTD

	// TSVGZ represents gzip-compressed TSV file type.
	TSVGZ
	// TSVBZ2 represents bzip2-compressed TSV file type.
	TSVBZ2
	// TSVXZ represents xz-compressed TSV file type.
	TSVXZ
	// TSVZSTD represents zstd-compressed TSV file type.
	TSVZSTD

	// LTSVGZ represents gzip-compressed LTSV file type.
	LTSVGZ
	// LTSVBZ2 represents bzip2-compressed LTSV file type.
	LTSVBZ2
	// LTSVXZ represents xz-compressed LTSV file type.
	LTSVXZ
	// LTSVZSTD represents zstd-compressed LTSV file type.
	LTSVZSTD

	// ParquetGZ represents gzip-compressed Parquet file type.
	ParquetGZ
	// ParquetBZ2 represents bzip2-compressed Parquet file type.
	ParquetBZ2
	// ParquetXZ represents xz-compressed Parquet file type.
	ParquetXZ
	// ParquetZSTD represents zstd-compressed Parquet file type.
	ParquetZSTD

	// XLSXGZ represents gzip-compressed XLSX file type.
	XLSXGZ
	// XLSXBZ2 represents bzip2-compressed XLSX file type.
	XLSXBZ2
	// XLSXXZ represents xz-compressed XLSX file type.
	XLSXXZ
	// XLSXZSTD represents zstd-compressed XLSX file type.
	XLSXZSTD

	// Unsupported represents unsupported file type.
	Unsupported
)

func BaseFileType

func BaseFileType(ft FileType) FileType

BaseFileType returns the base file type without compression.

func DetectFileType

func DetectFileType(path string) FileType

DetectFileType detects file type from path extension, including compression variants.

func (FileType) String

func (ft FileType) String() string

String returns a human-readable string representation of the FileType.

type TableData

type TableData struct {
	// Headers contains the column names in order.
	Headers []string
	// Records contains the data rows. Each record is a slice of string values.
	Records [][]string
	// ColumnTypes contains the inferred types for each column.
	// The length matches Headers.
	ColumnTypes []ColumnType
}

TableData contains the parsed data from a file.

func Parse

func Parse(reader io.Reader, fileType FileType) (result *TableData, err error)

Parse reads data from an io.Reader and returns parsed results. The fileType parameter specifies the format and compression of the data.

Example:

f, _ := os.Open("data.csv.gz")
defer f.Close()
result, err := parser.Parse(f, parser.CSVGZ)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL