Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jabolopes/csvstruct
Import multiply-typed structured data from CSV to Go types
https://github.com/jabolopes/csvstruct
csv ecs entity-component-system go golang golang-library spreadsheet
Last synced: 8 days ago
JSON representation
Import multiply-typed structured data from CSV to Go types
- Host: GitHub
- URL: https://github.com/jabolopes/csvstruct
- Owner: jabolopes
- License: bsd-3-clause
- Created: 2024-07-29T18:58:20.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-08-09T23:33:56.000Z (5 months ago)
- Last Synced: 2024-08-10T00:36:46.234Z (5 months ago)
- Topics: csv, ecs, entity-component-system, go, golang, golang-library, spreadsheet
- Language: Go
- Homepage:
- Size: 30.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# csvstruct
[![PkgGoDev](https://pkg.go.dev/badge/github.com/jabolopes/go-ecs)](https://pkg.go.dev/github.com/jabolopes/csvstruct)
Import multiply-typed structured data from CSV to Go types.
Import spreadsheets:
![screenshot](https://github.com/user-attachments/assets/62e920eb-d92f-4419-910f-384bbbd6f6c0)
Into Go types:
```go
type Info struct {
Name string
Class string
}type Attributes struct {
HP int
Damage int
}type Prefab struct {
Info *Info
Attributes *Attributes
}
```Get typed data:
```go
Prefab{Info{"Alex", "Fighter"}, Attributes{100, 10}}
Prefab{Info{"Mary", "Queen"}, nil}
Prefab{Info{"Jayden", "Wizard"}, Attributes{90, 20}}
```When working with Google Sheets or Microsoft Excel, export data to CSV and
import it to your program using the csvstruct library.## Example
Let's assume the following CSS file:
```css
Info.Name,Info.Class,Attributes.HP,Attributes.Damage
Alex,Fighter,100,10
Jayden,Wizard,90,20
Mary,Queen,,
...
```The following program uses csvstruct library to import the data above:
```go
type Info struct {
Name string
Class string
}type Attributes struct {
HP int
Damage int
}type Prefab struct {
Info *Info
Attributes *Attributes
}reader := csvstruct.NewReader[Prefab](csv.NewReader(strings.NewReader(testData)))
var prefab Prefab
for {
err := reader.Read(&prefab)
if err == io.EOF {
break
}
if err != nil {
panic(err)
}fmt.Printf("%v\n", prefab.Info)
fmt.Printf("%v\n", prefab.Attributes)
}
```## Format
The CSV data must have the following format:
### Header
The first row of the CSV data is the header and it must be present.
Each header column contains the name of a component followed by a period `.` and
an optional field name, e.g., `MyComponent.MyField`.The `MyComponent.MyField` must be valid, i.e., `MyComponent` must be a valid
field name of the type `T` passed to `NewReader`, and `MyField` must be a valid
field of `MyComponent`.If a cell is not given, then it's field is default initialized according to the
default initialization of Go. For example, pointers are default initialized to
`nil` and value types are default initialized to `0`, empty structs, empty
arrays, etc.It's not required to put in the CSV header all the fields of
`MyComponent`. Rather, only the fields that should be imported by those CSV data
are present.### Data rows
The rows that follow a CSV header are data rows.
The CSV data can contain 0 or more data rows.
The data rows must contain in each cell data that is compatible with
the type of the field specified in the CSV header.For example, a field of type string can contain either an empty or
non-empty cell (without quotes) since that is compatible with the
string type.A field of type `Int` can either an empty or non-empty cell containing
a numerical value.Empty cells default initialize fields according to Go semantics.
### Multiple tables in the same CSV
It's possible to have multiple "tables" in the same CSV file. Tables are
separate by CSV header rows. The library caller must be able to determine when a
new CSV header is about to come up as the next row. In this case, the caller can
use `Reader.Clear` to start a new table of CSV data, followed by `Reader.Read`
to parse the new table.