Skip to content

Commit

Permalink
Add meta to exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Oct 21, 2015
1 parent 0451812 commit b52cdac
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
8 changes: 5 additions & 3 deletions exchange/backends/csv/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type CSV struct {
}

func (c *CSV) Rows(res *exchange.Resource) (exchange.Rows, error) {
var rows = Rows{CSV: c}
var rows = Rows{CSV: c, Resource: res}

csvfile, err := os.Open(c.Filename)
if err == nil {
Expand All @@ -40,11 +40,13 @@ func (csv *CSV) WriteLog(string) {

type Rows struct {
*CSV
current int
total int
Resource *exchange.Resource
current int
total int
}

func (rows Rows) Columns() []string {
metas := rows.Resource.GetMetas([]string{})
if rows.total > 0 {
return rows.records[0]
}
Expand Down
21 changes: 10 additions & 11 deletions exchange/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ func New(config qor.Config) *Exchange {
type Resource struct {
resource.Resource
Config *Config
metas []*Meta
}

type Config struct {
Permission *roles.Permission
}

type Meta struct {
Name string
}

func NewResource(value interface{}, config ...Config) *Resource {
res := Resource{Resource: *resource.New(value)}
if len(config) > 0 {
Expand All @@ -38,15 +35,17 @@ func NewResource(value interface{}, config ...Config) *Resource {
return &res
}

func (exchange *Exchange) AddResource(value interface{}, config ...Config) {
res := Resource{Resource: *resource.New(value)}
if len(config) > 0 {
res.Config = &config[0]
}
exchange.resources = append(exchange.resources, &res)
func (res *Resource) Meta(meta Meta) *Meta {
res.metas = append(res.metas, &meta)
return &meta
}

func (res *Resource) Meta(meta Meta) {
func (res *Resource) GetMetas([]string) []resource.Metaor {
metas := []resource.Metaor{}
for _, meta := range res.metas {
metas = append(metas, meta)
}
return metas
}

func (res *Resource) Import(container Container, context *qor.Context) error {
Expand Down
46 changes: 46 additions & 0 deletions exchange/meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package exchange

import (
"github.com/qor/qor"
"github.com/qor/qor/resource"
"github.com/qor/qor/roles"
)

type Meta struct {
Name string
Valuer func(interface{}, *qor.Context) interface{}
Setter func(resource interface{}, metaValue *resource.MetaValue, context *qor.Context)
Permission *roles.Permission
}

func (meta *Meta) GetName() string {
return meta.Name
}

func (meta *Meta) GetFieldName() string {
return meta.Name
}

func (meta *Meta) GetMetas() []resource.Metaor {
// FIXME
return []resource.Metaor{}
}

func (meta *Meta) GetResource() resource.Resourcer {
return nil
}

func (meta *Meta) GetValuer() func(interface{}, *qor.Context) interface{} {
return meta.Valuer
}

func (meta *Meta) GetSetter() func(resource interface{}, metaValue *resource.MetaValue, context *qor.Context) {
return meta.Setter
}

func (meta *Meta) HasPermission(mode roles.PermissionMode, context *qor.Context) bool {
if meta.Permission == nil {
return true
}
return meta.Permission.HasPermission(mode, context.Roles...)
}

0 comments on commit b52cdac

Please sign in to comment.