datapackage-go: github.com/frictionlessdata/datapackage-go/datapackage Index | Examples | Files

package datapackage

import "github.com/frictionlessdata/datapackage-go/datapackage"

Index

Examples

Package Files

package.go resource.go schema.go

type Package Uses

type Package struct {
    // contains filtered or unexported fields
}

Package represents a https://specs.frictionlessdata.io/data-package/

func FromReader Uses

func FromReader(r io.Reader, basePath string, loaders ...validator.RegistryLoader) (*Package, error)

FromReader creates a data package from an io.Reader.

func FromString Uses

func FromString(in string, basePath string, loaders ...validator.RegistryLoader) (*Package, error)

FromString creates a data package from a string representation of the package descriptor.

func Load Uses

func Load(path string, loaders ...validator.RegistryLoader) (*Package, error)

Load the data package descriptor from the specified URL or file path. If path has the ".zip" extension, it will be saved in local filesystem and decompressed before loading.

Code:

dir, _ := ioutil.TempDir("", "datapackage_exampleload")
defer os.RemoveAll(dir)
descriptorPath := filepath.Join(dir, "pkg.json")
descriptorContents := `{"resources": [{ 
		  "name": "res1",
		  "path": "data.csv",
		  "profile": "tabular-data-resource",
		  "schema": {"fields": [{"name":"name", "type":"string"}]}
		}]}`
ioutil.WriteFile(descriptorPath, []byte(descriptorContents), 0666)

resPath := filepath.Join(dir, "data.csv")
resContent := []byte("foo\nbar")
ioutil.WriteFile(resPath, resContent, 0666)

pkg, _ := Load(descriptorPath, validator.InMemoryLoader())
res := pkg.GetResource("res1")
people := []struct {
    Name string `tableheader:"name"`
}{}
res.Cast(&people)
fmt.Printf("%+v", people)

Output:

[{Name:foo} {Name:bar}]

Code:

dir, _ := ioutil.TempDir("", "datapackage_exampleload")
defer os.RemoveAll(dir)
descriptorPath := filepath.Join(dir, "pkg.json")
descriptorContents := `{"resources": [{ 
		  "name": "res1",
		  "path": "data.csv",
		  "profile": "tabular-data-resource",
		  "schema": {"fields": [{"name":"name", "type":"string"}]}
		}]}`
ioutil.WriteFile(descriptorPath, []byte(descriptorContents), 0666)

resPath := filepath.Join(dir, "data.csv")
resContent := []byte("foo\nbar")
ioutil.WriteFile(resPath, resContent, 0666)

pkg, _ := Load(descriptorPath, validator.InMemoryLoader())
contents, _ := pkg.GetResource("res1").ReadAll()
fmt.Println(contents)

Output:

[[foo] [bar]]

Code:

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    // If the request is for data, returns the content.
    switch {
    case r.RequestURI == "/data.csv":
        fmt.Fprintf(w, "foo\nbar")
    default:
        fmt.Fprintf(w, `{"resources": [{ 
				"name": "res1",
				"path": "data.csv",
				"profile": "tabular-data-resource",
				"schema": {"fields": [{"name":"name", "type":"string"}]}
			  }]}`)
    }
}))
defer ts.Close()
pkg, _ := Load(ts.URL, validator.InMemoryLoader())
contents, _ := pkg.GetResource("res1").ReadAll()
fmt.Println(contents)

Output:

[[foo] [bar]]

Code:

dir, _ := ioutil.TempDir("", "datapackage_exampleload")
defer os.RemoveAll(dir)
descriptorPath := filepath.Join(dir, "pkg.json")
descriptorContents := `{"resources": [{ 
		  "name": "res1",
		  "path": "schemaorg.json",
		  "format": "application/ld+json",
		  "profile": "data-resource"
		}]}`
ioutil.WriteFile(descriptorPath, []byte(descriptorContents), 0666)

resPath := filepath.Join(dir, "schemaorg.json")
resContent := []byte(`{"@context": {"@vocab": "http://schema.org/"}}`)
ioutil.WriteFile(resPath, resContent, 0666)

pkg, _ := Load(descriptorPath, validator.InMemoryLoader())
rc, _ := pkg.GetResource("res1").RawRead()
defer rc.Close()
contents, _ := ioutil.ReadAll(rc)
fmt.Println(string(contents))

Output:

{"@context": {"@vocab": "http://schema.org/"}}

func New Uses

func New(descriptor map[string]interface{}, basePath string, loaders ...validator.RegistryLoader) (*Package, error)

New creates a new data package based on the descriptor.

func (*Package) AddResource Uses

func (p *Package) AddResource(d map[string]interface{}) error

AddResource adds a new resource to the package, updating its descriptor accordingly.

func (*Package) Descriptor Uses

func (p *Package) Descriptor() map[string]interface{}

Descriptor returns a deep copy of the underlying descriptor which describes the package.

func (*Package) GetResource Uses

func (p *Package) GetResource(name string) *Resource

GetResource return the resource which the passed-in name or nil if the resource is not part of the package.

func (*Package) RemoveResource Uses

func (p *Package) RemoveResource(name string)

RemoveResource removes the resource from the package, updating its descriptor accordingly.

func (*Package) ResourceNames Uses

func (p *Package) ResourceNames() []string

ResourceNames return a slice containing the name of the resources.

func (*Package) Resources Uses

func (p *Package) Resources() []*Resource

Resources returns a copy of data package resources.

func (*Package) SaveDescriptor Uses

func (p *Package) SaveDescriptor(path string) error

SaveDescriptor saves the data package descriptor to the passed-in file path. It create creates the named file with mode 0666 (before umask), truncating it if it already exists.

func (*Package) Update Uses

func (p *Package) Update(newDescriptor map[string]interface{}, loaders ...validator.RegistryLoader) error

Update the package with the passed-in descriptor. The package will only be updated if the the new descriptor is valid, otherwise the error will be returned.

func (*Package) Zip Uses

func (p *Package) Zip(path string) error

Zip saves a zip-compressed file containing the package descriptor and all resource data. It create creates the named file with mode 0666 (before umask), truncating it if it already exists.

type Resource Uses

type Resource struct {
    // contains filtered or unexported fields
}

Resource describes a data resource such as an individual file or table.

func NewResource Uses

func NewResource(d map[string]interface{}, registry validator.Registry) (*Resource, error)

NewResource creates a new Resource from the passed-in descriptor, if valid. The passed-in validator.Registry will be the source of profiles used in the validation.

func NewResourceFromString Uses

func NewResourceFromString(res string, registry validator.Registry) (*Resource, error)

NewResourceFromString creates a new Resource from the passed-in JSON descriptor, if valid. The passed-in validator.Registry will be the source of profiles used in the validation.

func NewResourceWithDefaultRegistry Uses

func NewResourceWithDefaultRegistry(d map[string]interface{}) (*Resource, error)

NewResourceWithDefaultRegistry creates a new Resource from the passed-in descriptor. It uses the default registry to validate the resource descriptor.

func NewUncheckedResource Uses

func NewUncheckedResource(d map[string]interface{}) *Resource

NewUncheckedResource returns an Resource instance based on the descriptor without any verification. The returned Resource might not be valid.

func (*Resource) Cast Uses

func (r *Resource) Cast(out interface{}, opts ...csv.CreationOpts) error

Cast resource contents. The result argument must necessarily be the address for a slice. The slice may be nil or previously allocated.

func (*Resource) Descriptor Uses

func (r *Resource) Descriptor() map[string]interface{}

Descriptor returns a copy of the underlying descriptor which describes the resource.

func (*Resource) GetSchema Uses

func (r *Resource) GetSchema() (schema.Schema, error)

GetSchema returns the schema associated to the resource, if present. The returned schema is based on a copy of the descriptor. Changes to it won't affect the data package descriptor structure.

func (*Resource) GetTable Uses

func (r *Resource) GetTable(opts ...csv.CreationOpts) (table.Table, error)

GetTable returns a table object to access the data. Returns an error if the resource is not tabular.

func (*Resource) Iter Uses

func (r *Resource) Iter(opts ...csv.CreationOpts) (table.Iterator, error)

Iter returns an Iterator to read the tabular resource. Iter returns an error if the table physical source can not be iterated. The iteration process always start at the beginning of the table.

func (*Resource) Name Uses

func (r *Resource) Name() string

Name returns the resource name.

func (*Resource) RawRead Uses

func (r *Resource) RawRead() (io.ReadCloser, error)

RawRead returns an io.ReaderCloser associated to the resource contents. It can be used to access the content of non-tabular resources.

func (*Resource) ReadAll Uses

func (r *Resource) ReadAll(opts ...csv.CreationOpts) ([][]string, error)

ReadAll reads all rows from the table and return it as strings.

func (*Resource) Tabular Uses

func (r *Resource) Tabular() bool

Tabular checks whether the resource is tabular.

func (*Resource) Update Uses

func (r *Resource) Update(d map[string]interface{}, loaders ...validator.RegistryLoader) error

Update the resource with the passed-in descriptor. The resource will only be updated if the the new descriptor is valid, otherwise the error will be returned.

Package datapackage imports 21 packages (graph). Updated 2018-04-24. Refresh now. Tools for package owners.