GitHub - plagioriginal/csvparser: A fast, easy-of-use and dependency free custom...
source link: https://github.com/plagioriginal/csvparser
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
csvparser
This package provides a fast and easy-of-use custom mapping from .csv data into Golang structs.
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") parser.AddColumnParser("name", nameHandler) parser.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 a hook
You can add an 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.
parser := csvparser.NewCsvParserFromBytes[WhateverStruct](input) children := make([]Person, 0) parser.WithHook(func(parsedPerson Person) { if parsedPerson.IsInSchool { children = append(children, parsedObject) } })
Recommend
-
508
Flutter is Google's SDK for crafting beautiful, fast user experiences for mobile, web, and desktop from a single codebase. Flutter works with existing code, is used by developers and organizations around the world, and is free and open source....
-
214
Dagger A fast dependency injector for Java and Android. Dagger is a compile-time framework for dependency injection. It uses no reflection or runtime bytecode generation, does all its analysis at compile-time, and generates plain J...
-
17
Fast Clojure dependency hot-loadingMay 6, 2020About ClojureClojure is great. I love it.But like most great things it has its downsides.One of which is the startup time.There are workarounds to...
-
21
let's make dependency walker fast again 2020-10-24...
-
8
sx is the command-line network scanner designed to follow the UNIX philosophy. The goal of this project is to create the fastest network scanner with clean and simple code.
-
3
Is Dependency Injection Into a Custom Exception a Good Idea? Investigating whether dependency injection into a custom Exception class a good idea. Published on 24 August 2021...
-
10
MeiliSearch Website | Roadmap | Blog |
-
2
Featured / April 05, 2022 Easy dependency integration in Kotlin/JS using the "Elm ports" technique TL;DR : Just starting with Kotlin/JS, I found it hard to use large NPM libraries like F...
-
5
depgrapher: Fast Dependency Analysis for Javascript...
-
8
CommonPHP Dependency Injection CommonPHP\DependencyInjection is a simple yet powerful dependency injection (DI) container for PHP applications, enabling the means to dynamically create new objects, invoke methods, call functi...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK