tableschema-go

module
Version: v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2017 License: MIT

README

Build Status Coverage Status Go Report Card Gitter chat GoDoc

tableschema-go

A Go library for working with Table Schema.

Main Features

  • table package defines Table and Iterator interfaces, which are used to manipulate and/or explore tabular data;
  • csv package contains implementation of Table and Iterator interfaces to deal with CSV format;
  • schema package contains classes and funcions for working with table schemas:

Getting started

Installation

This package uses semantic versioning 2.0.0.

Using dep
$ dep init
$ dep ensure -add github.com/frictionlessdata/tableschema-go@0.1
Using govendor
$ govendor init
$ govendor fetch github.com/frictionlessdata/tableschema-go@0.1

Examples

Code examples in this readme requires Go 1.8+. You can find more examples in the examples directory.

import (
    "fmt"
    "github.com/github.com/frictionlessdata/tableschema-go/csv"
)
// struct representing each row of the table.
type person struct {
    Name string
    Age uint16
}
func main() {
    t, _ := csv.New(csv.FileSource("data.csv"), csv.LoadHeaders())  // load table
    t.Infer()  // infer the table schema
    t.Schema.SaveToFile("schema.json")  // save inferred schema to file
    data := []person
    t.CastAll(&data)  // casts the table data into the data slice.
}

Documentation

Table

A table is a core concept in a tabular data world. It represents a data with a metadata (Table Schema). Let's see how we could use it in practice.

Consider we have some local CSV file, data.csv:

city,location
london,"51.50,-0.11"
paris,"48.85,2.30"
rome,N/A

To read its contents we use csv.New to create a table and use the File Source.

    table, _ := csv.New(csv.FromFile("data.csv"), csv.LoadHeaders())
    table.Headers // ["city", "location"]
    table.All() // [[london 51.50,-0.11], [paris 48.85,2.30], [rome N/A]]

As we could see our locations are just a strings. But it should be geopoints. Also Rome's location is not available but it's also just a N/A string instead of go's zero value. First we have to infer Table Schema:

    table.Infer()
    fmt.Println(table.Schema)
	// "fields": [
	//     {"name": "city", "type": "string", "format": "default"},
	//     {"name": "location", "type": "geopoint", "format": "default"},
	// ],
	// "missingValues": []
    // ...

Then we could create a struct and automatically cast the table data to schema types. It is like json.Unmarshal, but for table rows. First thing we need is to create the struct which will represent each row.

type Location struct {
    City string
    Location schema.GeoPoint
}

Then we are ready to cast the table.

var locations []Location
table.CastAll(&locations)
// Fails with cast error: "Invalid geopoint:\"N/A\""

The problem is that the library does not know that N/A is not an empty value. For those cases, there is a missingValues property in Table Schema specification. As a first try we set missingValues to N/A in table.Schema.

table.Schema.MissingValues = []string{"N/A"}
var locations []Location
table.CastAll(&locations)
fmt.Println(rows)
// [{london {51.5 -0.11}} {paris {48.85 2.3}} {rome {0 0}}]

And because there are no errors on data reading we could be sure that our data is valid againt our schema. Let's save it:

table.Schema.SaveToFile("schema.json")

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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