Skip to content

Commit

Permalink
Merge pull request #21 from frictionlessdata/Go11PlusReadColumn
Browse files Browse the repository at this point in the history
Add support to go 1.11 and go modules.
  • Loading branch information
danielfireman authored May 21, 2019
2 parents 52f13c3 + 73939f6 commit ebe1868
Show file tree
Hide file tree
Showing 166 changed files with 148 additions and 17,191 deletions.
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ go:
- 1.8.x
- 1.9.x
- 1.10.x

- 1.11.x
notificaitons:
email:
recipients: [email protected]
on_success: change
on_failure: always

env:
- GO111MODULE=on

before_install:
- curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
- dep ensure
- go get github.com/mattn/goveralls

script:
Expand Down
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 21 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@
# datapackage-go
A Go library for working with [Data Packages](http://specs.frictionlessdata.io/data-package/).

<!-- TOC -->

- [datapackage-go](#datapackage-go)
- [Install](#install)
- [Main Features](#main-features)
- [Loading and validating tabular data package descriptors](#loading-and-validating-tabular-data-package-descriptors)
- [Accessing data package resources](#accessing-data-package-resources)
- [Loading zip bundles](#loading-zip-bundles)
- [Creating a zip bundle with the data package.](#creating-a-zip-bundle-with-the-data-package)
- [CSV dialect support](#csv-dialect-support)
- [Loading multipart resources](#loading-multipart-resources)
- [Loading non-tabular resources](#loading-non-tabular-resources)
- [Manipulating data packages programatically](#manipulating-data-packages-programatically)

<!-- /TOC -->
- [Install](#install)
- [Main Features](#main-features)
- [Loading and validating tabular data package descriptors](#loading-and-validating-tabular-data-package-descriptors)
- [Accessing data package resources](#accessing-data-package-resources)
- [Loading zip bundles](#loading-zip-bundles)
- [Creating a zip bundle with the data package.](#creating-a-zip-bundle-with-the-data-package)
- [CSV dialect support](#csv-dialect-support)
- [Loading multipart resources](#loading-multipart-resources)
- [Loading non-tabular resources](#loading-non-tabular-resources)
- [Manipulating data packages programatically](#manipulating-data-packages-programatically)

## Install

Expand Down Expand Up @@ -95,7 +91,7 @@ fmt.Printf("+v", cities)
// [{City:london Year:2017 Population:8780000} {City:paris Year:2017 Population:2240000} {City:rome Year:2017 Population:2860000}]
```

Finally, if the data is to big to be loaded at once or if you would like to perform line-by-line processing, you could iterate through the resource contents:
If the data is to big to be loaded at once or if you would like to perform line-by-line processing, you could iterate through the resource contents:

```go
iter, _ := resource.Iter(csv.LoadHeaders())
Expand All @@ -109,6 +105,16 @@ for iter.Next() {
// {City:paris Year:2017 Population:2240000}
// {City:rome Year:2017 Population:2860000}]
```

Or you might want to process specific columns, for instance to perform an statical analysis:

```go
var population []float64
resource.CastColumn("population", &population, csv.LoadHeaders())
fmt.Println(ages)
// Output: [8780000 2240000 2860000]
```

### Loading zip bundles

It is very common to store the data in zip bundles containing the descriptor and data files. Those are natively supported by our the [datapackage.Load](https://godoc.org/github.com/frictionlessdata/datapackage-go/datapackage#Load) method. For example, lets say we have the following `package.zip` bundle:
Expand Down
19 changes: 19 additions & 0 deletions datapackage/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,25 @@ func (r *Resource) Cast(out interface{}, opts ...csv.CreationOpts) error {
return sch.CastTable(tbl, out)
}

// CastColumn casts a column from tabular resource contents.
// The out argument must necessarily be the address for a slice. The slice
// may be nil or previously allocated.
func (r *Resource) CastColumn(name string, out interface{}, opts ...csv.CreationOpts) error {
sch, err := r.GetSchema()
if err != nil {
return err
}
tab, err := r.GetTable(opts...)
if err != nil {
return err
}
col, err := tab.ReadColumn(name)
if err != nil {
return err
}
return sch.CastColumn(col, name, out)
}

// NewResourceWithDefaultRegistry creates a new Resource from the passed-in descriptor.
// It uses the default registry to validate the resource descriptor.
func NewResourceWithDefaultRegistry(d map[string]interface{}) (*Resource, error) {
Expand Down
86 changes: 83 additions & 3 deletions datapackage/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,27 @@ import (
"testing"

"github.com/frictionlessdata/datapackage-go/validator"
"github.com/frictionlessdata/tableschema-go/csv"
"github.com/frictionlessdata/tableschema-go/schema"
"github.com/matryer/is"
)

func ExampleResource_CastColumn() {
resStr := `
{
"name": "col",
"data": "name,age\nfoo,42\nbar,84",
"format": "csv",
"profile": "tabular-data-resource",
"schema": {"fields": [{"name": "name", "type": "string"},{"name": "age", "type": "integer"}]}
}`
res, _ := NewResourceFromString(resStr, validator.MustInMemoryRegistry())
var ages []float64
res.CastColumn("age", &ages, csv.LoadHeaders())
fmt.Println(ages)
// Output: [42 84]
}

func TestNewResourceWithDefaultRegistry(t *testing.T) {
res, _ := NewResourceWithDefaultRegistry(r1)
fmt.Println(res.Name())
Expand Down Expand Up @@ -152,9 +170,9 @@ func TestNew(t *testing.T) {
map[string]interface{}{"name": "foo", "path": "foo.csv", "schema": ts.URL},
validator.MustInMemoryRegistry(),
)
is.NoErr(err)
is.NoErr(err) // Resource should be created successfully.
sch, err := r.GetSchema()
is.Equal(sch.Fields[0].Type, "string")
is.Equal(sch.Fields[0].Type, schema.StringType)
})
t.Run("InvalidRemote", func(t *testing.T) {
_, err := NewResource(
Expand All @@ -177,7 +195,7 @@ func TestNew(t *testing.T) {
)
is.NoErr(err)
sch, err := r.GetSchema()
is.Equal(sch.Fields[0].Type, "string")
is.Equal(sch.Fields[0].Type, schema.StringType)
})
t.Run("InvalidLocal", func(t *testing.T) {
_, err := NewResource(
Expand Down Expand Up @@ -457,3 +475,65 @@ func TestResource_RawRead(t *testing.T) {
is.Equal(string(contents), "{\"foo\":\"1234\"}")
})
}

func TestResource_ReadColumn(t *testing.T) {
resStr := `
{
"name": "col",
"data": "name,age\nfoo,42\nbar,84",
"format": "csv",
"profile": "tabular-data-resource",
"schema": {"fields": [{"name": "name", "type": "string"},{"name": "age", "type": "integer"}]}
}`
t.Run("Valid", func(t *testing.T) {
is := is.New(t)
res, err := NewResourceFromString(resStr, validator.MustInMemoryRegistry())
is.NoErr(err)
var ages []float64
is.NoErr(res.CastColumn("age", &ages, csv.LoadHeaders()))
is.Equal(float64(42), ages[0])
is.Equal(float64(84), ages[1])
})
t.Run("NoSchema", func(t *testing.T) {
res := NewUncheckedResource(map[string]interface{}{})
var ages []float64
if res.CastColumn("age", &ages) == nil {
t.Fatal("want:err got:nil")
}
})
t.Run("NoData", func(t *testing.T) {
res := NewUncheckedResource(map[string]interface{}{
"schema": map[string]interface{}{},
})
var ages []float64
if res.CastColumn("age", &ages) == nil {
t.Fatal("want:err got:nil")
}
})
t.Run("HeaderNotFound", func(t *testing.T) {
is := is.New(t)
res, err := NewResourceFromString(resStr, validator.MustInMemoryRegistry())
is.NoErr(err)
var ages []float64
if res.CastColumn("foo", &ages) == nil {
t.Fatal("want:err got:nil")
}
})
t.Run("FieldNotFound", func(t *testing.T) {
is := is.New(t)
resStr := `
{
"name": "col",
"data": "name,age\nfoo,42\nbar,84",
"format": "csv",
"profile": "tabular-data-resource",
"schema": {"fields": [{"name": "name", "type": "string"},{"name": "Age", "type": "integer"}]}
}`
res, err := NewResourceFromString(resStr, validator.MustInMemoryRegistry())
is.NoErr(err)
var ages []float64
if res.CastColumn("age", &ages) == nil {
t.Fatal("want:err got:nil")
}
})
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/frictionlessdata/datapackage-go

require (
github.com/frictionlessdata/tableschema-go v0.1.5-0.20190521014818-f9bf38926664
github.com/matryer/is v1.2.0
github.com/santhosh-tekuri/jsonschema v1.2.4
github.com/satori/go.uuid v1.2.0
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
github.com/frictionlessdata/tableschema-go v0.1.5-0.20190521014818-f9bf38926664 h1:IvuZMJ6dH1ye2bWmM8Yla6jj1xIPBR/nZJlm6P4ZSD4=
github.com/frictionlessdata/tableschema-go v0.1.5-0.20190521014818-f9bf38926664/go.mod h1:OfuE6zbfQdlwx5q9vf5XWXEGJ0LYZcd9ML3zme5rP3k=
github.com/matryer/is v0.0.0-20170112134659-c0323ceb4e99/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
github.com/santhosh-tekuri/jsonschema v1.2.4 h1:hNhW8e7t+H1vgY+1QeEQpveR6D4+OwKPXCfD2aieJis=
github.com/santhosh-tekuri/jsonschema v1.2.4/go.mod h1:TEAUOeZSmIxTTuHatJzrvARHiuO9LYd+cIxzgEHCQI4=
github.com/satori/go.uuid v1.1.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
18 changes: 0 additions & 18 deletions vendor/github.com/frictionlessdata/tableschema-go/.gitignore

This file was deleted.

18 changes: 0 additions & 18 deletions vendor/github.com/frictionlessdata/tableschema-go/.travis.yml

This file was deleted.

This file was deleted.

21 changes: 0 additions & 21 deletions vendor/github.com/frictionlessdata/tableschema-go/Gopkg.lock

This file was deleted.

26 changes: 0 additions & 26 deletions vendor/github.com/frictionlessdata/tableschema-go/Gopkg.toml

This file was deleted.

21 changes: 0 additions & 21 deletions vendor/github.com/frictionlessdata/tableschema-go/LICENSE

This file was deleted.

Loading

0 comments on commit ebe1868

Please sign in to comment.