1
0
mirror of https://github.com/rancher/norman.git synced 2025-07-14 15:45:46 +00:00
norman/store/transform/transform.go

106 lines
2.8 KiB
Go
Raw Normal View History

2017-12-23 06:14:23 +00:00
package transform
2017-11-21 20:46:30 +00:00
import "github.com/rancher/norman/types"
type TransformerFunc func(apiContext *types.APIContext, data map[string]interface{}) (map[string]interface{}, error)
type ListTransformerFunc func(apiContext *types.APIContext, data []map[string]interface{}) ([]map[string]interface{}, error)
2017-11-28 21:28:25 +00:00
type StreamTransformerFunc func(apiContext *types.APIContext, data chan map[string]interface{}) (chan map[string]interface{}, error)
2017-12-23 06:14:23 +00:00
type Store struct {
2017-11-28 21:28:25 +00:00
Store types.Store
Transformer TransformerFunc
ListTransformer ListTransformerFunc
StreamTransformer StreamTransformerFunc
2017-11-21 20:46:30 +00:00
}
2017-12-23 06:14:23 +00:00
func (t *Store) ByID(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
2017-11-21 20:46:30 +00:00
data, err := t.Store.ByID(apiContext, schema, id)
if err != nil {
return nil, err
}
if t.Transformer == nil {
return data, nil
}
return t.Transformer(apiContext, data)
}
2017-12-28 15:47:10 +00:00
func (t *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
2017-11-28 21:28:25 +00:00
c, err := t.Store.Watch(apiContext, schema, opt)
if err != nil {
return nil, err
}
if t.StreamTransformer != nil {
return t.StreamTransformer(apiContext, c)
}
result := make(chan map[string]interface{})
go func() {
for item := range c {
item, err := t.Transformer(apiContext, item)
if err == nil && item != nil {
result <- item
}
}
close(result)
}()
return result, nil
}
2017-12-28 15:47:10 +00:00
func (t *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
2017-11-21 20:46:30 +00:00
data, err := t.Store.List(apiContext, schema, opt)
if err != nil {
return nil, err
}
if t.ListTransformer != nil {
return t.ListTransformer(apiContext, data)
}
if t.Transformer == nil {
return data, nil
}
var result []map[string]interface{}
for _, item := range data {
item, err := t.Transformer(apiContext, item)
if err != nil {
return nil, err
}
2017-11-28 21:28:25 +00:00
if item != nil {
result = append(result, item)
}
2017-11-21 20:46:30 +00:00
}
return result, nil
}
2017-12-23 06:14:23 +00:00
func (t *Store) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
2017-11-21 20:46:30 +00:00
data, err := t.Store.Create(apiContext, schema, data)
if err != nil {
return nil, err
}
if t.Transformer == nil {
return data, nil
}
return t.Transformer(apiContext, data)
}
2017-12-23 06:14:23 +00:00
func (t *Store) Update(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, id string) (map[string]interface{}, error) {
2017-11-21 20:46:30 +00:00
data, err := t.Store.Update(apiContext, schema, data, id)
if err != nil {
return nil, err
}
if t.Transformer == nil {
return data, nil
}
return t.Transformer(apiContext, data)
}
2017-12-23 06:14:23 +00:00
func (t *Store) Delete(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
2017-11-21 20:46:30 +00:00
return t.Store.Delete(apiContext, schema, id)
}