Skip to content

Commit

Permalink
Add schema to Transformer funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Mar 23, 2018
1 parent bcb8076 commit 34fad5e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
8 changes: 8 additions & 0 deletions store/proxy/proxy_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
ejson "encoding/json"
"net/http"
"strings"
"time"

"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
Expand Down Expand Up @@ -113,6 +114,11 @@ func (p *Store) getUser(apiContext *types.APIContext) string {
}

func (p *Store) doAuthed(apiContext *types.APIContext, request *rest.Request) rest.Result {
start := time.Now()
defer func() {
logrus.Debug("GET:", time.Now().Sub(start), p.resourcePlural)
}()

for _, header := range authHeaders {
request.SetHeader(header, apiContext.Request.Header[http.CanonicalHeaderKey(header)]...)
}
Expand Down Expand Up @@ -157,7 +163,9 @@ func (p *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *ty
req := p.common(namespace, k8sClient.Get())

resultList := &unstructured.UnstructuredList{}
start := time.Now()
err = req.Do().Into(resultList)
logrus.Debug("LIST:", time.Now().Sub(start), p.resourcePlural)
if err != nil {
return nil, err
}
Expand Down
20 changes: 10 additions & 10 deletions store/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"github.com/rancher/norman/types/convert"
)

type TransformerFunc func(apiContext *types.APIContext, data map[string]interface{}, opt *types.QueryOptions) (map[string]interface{}, error)
type TransformerFunc func(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, opt *types.QueryOptions) (map[string]interface{}, error)

type ListTransformerFunc func(apiContext *types.APIContext, data []map[string]interface{}, opt *types.QueryOptions) ([]map[string]interface{}, error)
type ListTransformerFunc func(apiContext *types.APIContext, schema *types.Schema, data []map[string]interface{}, opt *types.QueryOptions) ([]map[string]interface{}, error)

type StreamTransformerFunc func(apiContext *types.APIContext, data chan map[string]interface{}, opt *types.QueryOptions) (chan map[string]interface{}, error)
type StreamTransformerFunc func(apiContext *types.APIContext, schema *types.Schema, data chan map[string]interface{}, opt *types.QueryOptions) (chan map[string]interface{}, error)

type Store struct {
Store types.Store
Expand All @@ -30,7 +30,7 @@ func (s *Store) ByID(apiContext *types.APIContext, schema *types.Schema, id stri
if s.Transformer == nil {
return data, nil
}
return s.Transformer(apiContext, data, &types.QueryOptions{
return s.Transformer(apiContext, schema, data, &types.QueryOptions{
Options: map[string]string{
"ByID": "true",
},
Expand All @@ -44,11 +44,11 @@ func (s *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *t
}

if s.StreamTransformer != nil {
return s.StreamTransformer(apiContext, c, opt)
return s.StreamTransformer(apiContext, schema, c, opt)
}

return convert.Chan(c, func(data map[string]interface{}) map[string]interface{} {
item, err := s.Transformer(apiContext, data, opt)
item, err := s.Transformer(apiContext, schema, data, opt)
if err != nil {
return nil
}
Expand All @@ -63,7 +63,7 @@ func (s *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *ty
}

if s.ListTransformer != nil {
return s.ListTransformer(apiContext, data, opt)
return s.ListTransformer(apiContext, schema, data, opt)
}

if s.Transformer == nil {
Expand All @@ -72,7 +72,7 @@ func (s *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *ty

var result []map[string]interface{}
for _, item := range data {
item, err := s.Transformer(apiContext, item, opt)
item, err := s.Transformer(apiContext, schema, item, opt)
if err != nil {
return nil, err
}
Expand All @@ -92,7 +92,7 @@ func (s *Store) Create(apiContext *types.APIContext, schema *types.Schema, data
if s.Transformer == nil {
return data, nil
}
return s.Transformer(apiContext, data, nil)
return s.Transformer(apiContext, schema, data, nil)
}

func (s *Store) Update(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, id string) (map[string]interface{}, error) {
Expand All @@ -103,7 +103,7 @@ func (s *Store) Update(apiContext *types.APIContext, schema *types.Schema, data
if s.Transformer == nil {
return data, nil
}
return s.Transformer(apiContext, data, nil)
return s.Transformer(apiContext, schema, data, nil)
}

func (s *Store) Delete(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
Expand Down

0 comments on commit 34fad5e

Please sign in to comment.