A fast, easy-of-use and dependency free custom mapping from .csv data into Golang structs

csvparser

Go Report Card

This package provides a fast and easy-of-use custom mapping from .csv data into Golang structs.

Index

Pre-requisites

Since the library uses generics, it is necessary to have go1.18

Installation

go get github.com/plagioriginal/csvparser

Examples

Csv parsing from bytes

This will read the .csv data being sent, and will return an array of whatever you would like.

type Person struct {
	Name string
	Age int
	isInSchool bool
}
    
var input = []byte(`
name,age
frank,13
anabelle,70`)

parser := csvparser.NewCsvParserFromBytes[Person](input)
parser.AddColumnParser("name", func (value string, into *Person) error {
    into.Name = strings.Trim(value, " ")
    return nil
})
parser.AddColumnParser("age", func (value string, into *Person) error {
    value = strings.Trim(value, " ")
    age, err := strconv.Atoi(value)
    if err != nil {
        return err
    }
    into.Age = age
    if age < 18 {
	    into.IsInSchool = true	
    }
    return nil
})

// res is []Person type
res, err := parser.Parse()

Note: as long as there is a parser for the header that you want, the order of the .csv columns will not matter

What if the file doesn't have headers

When instantiating the parser, you can specify the headers of the file, in order, and the parser will handle everything for you. Just remember that the ParserHandlers need to be added.

var input = []byte(`
frank,13
anabelle,70`)

parser := csvparser.NewCsvParserFromBytes[Person](input, "name", "age").
	AddColumnParser("name", nameHandler).
	AddColumnParser("age", ageHandler)
...

Csv Parsing from multipart file / anything that applies the io.Reader

If you need to directly use something like a multipart file directly, you can do something like this:

func (h *OrderHandler) handlerFunc(w http.ResponseWriter, r *http.Request) {
    file, _, err := r.FormFile("file-key-in-request")
    if err != nil {
        ...
    }
    defer file.Close()
    parser := csvparser.NewCsvParserFromReader[WhateverStruct](file)
    ...
}

Adding hooks

After each successful parsing

You can add a hook that will run everytime something is parsed from the .csv file, so that you don't have to do another loop in the results in case you want to add more logic into it. To do this, use the function AfterEachParsingHook()

parser := csvparser.NewCsvParserFromBytes[Person](input)
children := make([]Person, 0)
parser.AfterEachParsingHook(func(person Person) {
    if parsedPerson.IsInSchool {
        children = append(children, person)
    }
})

On Error Hook

Use the OnError() function to handle the error of an invalid row yourself.

parser := csvparser.NewCsvParserFromBytes[Person](input).
    OnError(func(row []string, err error) {
        log.Printf("row %v has thrown the error: %v", row, err)
    })

Additional Settings

Terminate on row parsing error

You can choose if you want to throw an error on the parsing results if the input has an invalid row, or just continue and skip that record. By default, the behaviour is to skip the error.

However, you can make it stop and throw an error with the function TerminateOnParsingError():

res, err := csvparser.NewCsvParserFromBytes[Person](input).
    AddColumnParser("name", nameHandler).
    AddColumnParser("age", ageHandler).
    TerminateOnParsingError().
    Parse()
Owner
João Duarte
Software Engineer at Truphone SCNL
João Duarte
Similar Resources

network .md into .html with plaintext files

network .md into .html with plaintext files

plain network markdown files into html with plaintext files plain is a static-site generator operating on plaintext files containing a small set of co

Nov 13, 2022

Turns any junk text into a usable wordlist for brute-forcing.

haklistgen Turns any junk text into a usable wordlist for brute-forcing. Installation go get -u github.com/hakluke/haklistgen Usage Examples Scrape a

Nov 19, 2022

Script to inject Aliucord into a Discord ipa.

Aliucord-Patcher Script to patch Discord's ipa with a custom build of hermes, apply a custom icon and apply changes to Info.plist. Usage: go run cmds/

Jan 15, 2022

bluemonday: a fast golang HTML sanitizer (inspired by the OWASP Java HTML Sanitizer) to scrub user generated content of XSS

bluemonday bluemonday is a HTML sanitizer implemented in Go. It is fast and highly configurable. bluemonday takes untrusted user generated content as

Dec 1, 2022

Easy AWK-style text processing in Go

awk Description awk is a package for the Go programming language that provides an AWK-style text processing capability. The package facilitates splitt

Jul 25, 2022

A markdown parser written in Go. Easy to extend, standard(CommonMark) compliant, well structured.

goldmark A Markdown parser written in Go. Easy to extend, standards-compliant, well-structured. goldmark is compliant with CommonMark 0.29. Motivation

Nov 27, 2022

Small and fast FTS (full text search)

Microfts A small full text indexing and search tool focusing on speed and space. Initial tests seem to indicate that the database takes about twice as

Jul 30, 2022

Fast and secure steganography CLI for hiding text/files in images.

Fast and secure steganography CLI for hiding text/files in images.

indie CLI This complete README is hidden in the target.png file below without the original readme.png this could have also been a lie as none could ev

Mar 20, 2022

Parse data and test fixtures from markdown files, and patch them programmatically, too.

go-testmark Do you need test fixtures and example data for your project, in a language agnostic way? Do you want it to be easy to combine with documen

Oct 31, 2022
Comments
  • Version 1.1.0

    Version 1.1.0

    Change log:

    • feat: add OnError hook. Possibility of handling errors for each row with a callback function.
    • feat: allow method chaining.
    • feat: add option to terminate the parsing if a row throws an error, or to skip the row and continue the parsing.
    • style: renames and small doc changes
    • docs: update readme.MD
[Go] Package of validators and sanitizers for strings, numerics, slices and structs

govalidator A package of validators and sanitizers for strings, structs and collections. Based on validator.js. Installation Make sure that Go is inst

Dec 4, 2022
omniparser: a native Golang ETL streaming parser and transform library for CSV, JSON, XML, EDI, text, etc.
omniparser: a native Golang ETL streaming parser and transform library for CSV, JSON, XML, EDI, text, etc.

omniparser Omniparser is a native Golang ETL parser that ingests input data of various formats (CSV, txt, fixed length/width, XML, EDI/X12/EDIFACT, JS

Nov 30, 2022
🧑‍💻 Go XML generator without Structs™

exml ??‍?? Go XML generator without Structs™ Package exml allows XML documents to be generated without the usage of structs or maps. It is not intende

Nov 15, 2022
Your CSV pocket-knife (golang)

csvutil - Your CSV pocket-knife (golang) #WARNING I would advise against using this package. It was a language learning exercise from a time before "e

Oct 24, 2022
Sqly - An easy-to-use extension for sqlx, base on xml files and named query/exec

sqly An easy-to-use extension for sqlx ,base on xml files and named query/exec t

Jun 12, 2022
csvplus extends the standard Go encoding/csv package with fluent interface, lazy stream operations, indices and joins.

csvplus Package csvplus extends the standard Go encoding/csv package with fluent interface, lazy stream processing operations, indices and joins. The

Apr 9, 2022
golang program that simpily converts html into markdown

Simpily converts html to markdown Just a simple project I wrote in golang to convert html to markdown, surprisingly works decent for a lot of websites

Oct 23, 2021
Takes a full name and splits it into individual name parts

gonameparts gonameparts splits a human name into individual parts. This is useful when dealing with external data sources that provide names as a sing

Sep 27, 2022
Match regex group into go struct using struct tags and automatic parsing

regroup Simple library to match regex expression named groups into go struct using struct tags and automatic parsing Installing go get github.com/oris

Nov 5, 2022
Preventing 3rd Party DLLs from Injecting into your Malware

Doge-BlockDLLs Preventing 3rd Party DLLs from Injecting into your Malware ACG(Arbitrary Code Guard)的方式等大佬来实现 Ref https://www.ired.team/offensive-secur

Nov 9, 2022