exchange

package module
v0.0.0-...-8e12e25 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2020 License: MIT Imports: 7 Imported by: 19

README

Exchange

Exchange allows a database to be exported to or imported from a file.

Data can be optionally validated during import and export.

Data can be optionally processed during import.

GoDoc

Usage

import (
  "github.com/qor/exchange"
  "github.com/qor/exchange/backends/csv"
)

func main() {
  // Define Resource
  product = exchange.NewResource(&Product{}, exchange.Config{PrimaryField: "Code"})
  // Define columns are exportable/importable
  product.Meta(&exchange.Meta{Name: "Code"})
  product.Meta(&exchange.Meta{Name: "Name"})
  product.Meta(&exchange.Meta{Name: "Price"})

  // Define context environment
  context := &qor.Context{DB: db}

  // Import products into database from file `products.csv`
  product.Import(csv.New("products.csv"), context)

  // Import products into database from csv reader
  product.Import(csv.New(reader), context)

  // Export products to writer
  product.Export(csv.New(writer), context)
}

Sample products.csv

Code, Name, Price
P001, Product P001, 100
P002, Product P002, 200
P003, Product P003, 300

Advanced Usages

  • Add Validations
product.AddValidator(func(result interface{}, metaValues *resource.MetaValues, context *qor.Context) error {
  if f, err := strconv.ParseFloat(fmt.Sprint(metaValues.Get("Price").Value), 64); err == nil {
    if f == 0 {
      return errors.New("product's price can't be 0")
    }
    return nil
  } else {
    return err
  }
})
  • Process data before import
product.AddProcessor(func(result interface{}, metaValues *resource.MetaValues, context *qor.Context) error {
  product := result.(*Product)
  product.Price = product.Price * 1.1 // Add 10% Tax
  return nil
})
  • Callbacks
// Importing callbacks
product.Import(csv.New("products.csv"), context, func(progress exchange.Progress) error {
    fmt.Printf("%v/%v Importing product %v\n", progress.Current, progress.Total, progress.Value.(*Product).Code))
})

// Exporting callbacks
product.Export(csv.New("products.csv"), context, func(progress exchange.Progress) error {
    fmt.Printf("%v/%v Exporting product %v\n", progress.Current, progress.Total, progress.Value.(*Product).Code))
})

License

Released under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cell

type Cell struct {
	Header string
	Value  interface{}
	Error  error
}

Cell is a data cell, which including its data and error that happened when importing/exporting data

type Config

type Config struct {
	// PrimaryField that used as primary field when searching resource from database
	PrimaryField string
	// Permission defined permission
	Permission *roles.Permission
	// WithoutHeader no header in the data file
	WithoutHeader      bool
	DisableTransaction bool
}

Config is exchange resource config

type Container

type Container interface {
	NewReader(*Resource, *qor.Context) (Rows, error)
	NewWriter(*Resource, *qor.Context) (Writer, error)
}

Container is an interface, any exporting/importing backends needs to implement this

type Meta

type Meta struct {
	resource.Meta
	Name       string
	Header     string
	Valuer     func(interface{}, *qor.Context) interface{}
	Setter     func(resource interface{}, metaValue *resource.MetaValue, context *qor.Context)
	Permission *roles.Permission
	// contains filtered or unexported fields
}

Meta defines importable/exportable fields

func (*Meta) GetMetas

func (meta *Meta) GetMetas() []resource.Metaor

GetMetas get defined sub metas

func (*Meta) GetResource

func (meta *Meta) GetResource() resource.Resourcer

GetResource get its resource

type Progress

type Progress struct {
	Current uint
	Total   uint
	Value   interface{}
	Errors  qor.Errors
	Cells   []Cell
}

Progress defined importing/exporting progress

type Resource

type Resource struct {
	*resource.Resource
	Config *Config
	Metas  []*Meta
}

Resource defined an exchange resource, which includes importing/exporting fields definitions

func NewResource

func NewResource(value interface{}, config ...Config) *Resource

NewResource new exchange Resource

func (*Resource) Export

func (res *Resource) Export(container Container, context *qor.Context, callbacks ...func(Progress) error) error

Export used export data from a exchange Resource

product.Export(csv.New("products.csv"), context)

func (*Resource) GetMeta

func (res *Resource) GetMeta(name string) *Meta

GetMeta get defined Meta from exchange Resource

func (*Resource) GetMetas

func (res *Resource) GetMetas([]string) []resource.Metaor

GetMetas get all defined Metas from exchange Resource

func (*Resource) Import

func (res *Resource) Import(container Container, context *qor.Context, callbacks ...func(Progress) error) error

Import used to import data into a exchange Resource

product.Import(csv.New("products.csv"), context)

func (*Resource) Meta

func (res *Resource) Meta(meta *Meta) *Meta

Meta define exporting/importing meta for exchange Resource

type Rows

type Rows interface {
	Header() []string
	ReadRow() (*resource.MetaValues, error)
	Next() bool
	Total() uint
}

Rows is an interface, backends need to implement this in order to read data from it

type Writer

type Writer interface {
	WriteHeader() error
	WriteRow(interface{}) (*resource.MetaValues, error)
	Flush() error
}

Writer is an interface, backends need to implement this in order to write data

Directories

Path Synopsis
backends
csv

Jump to

Keyboard shortcuts

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