1
0
mirror of https://github.com/rancher/norman.git synced 2025-04-27 19:15:07 +00:00

Make queryopts be a pointer

This commit is contained in:
Darren Shepherd 2017-12-28 08:47:10 -07:00
parent 02738370b1
commit eec4103473
13 changed files with 40 additions and 30 deletions

View File

@ -30,7 +30,7 @@ func ByID(context *types.APIContext, version *types.APIVersion, typeName string,
return convert.ToObj(item, into)
}
func List(context *types.APIContext, version *types.APIVersion, typeName string, opts types.QueryOptions, into interface{}) error {
func List(context *types.APIContext, version *types.APIVersion, typeName string, opts *types.QueryOptions, into interface{}) error {
schema := context.Schemas.Schema(version, typeName)
if schema == nil {
return fmt.Errorf("failed to find schema " + typeName)

View File

@ -52,7 +52,7 @@ func (a *APIRootStore) ByID(apiContext *types.APIContext, schema *types.Schema,
return nil, nil
}
func (a *APIRootStore) List(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) ([]map[string]interface{}, error) {
func (a *APIRootStore) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
var roots []map[string]interface{}
for _, version := range apiContext.Schemas.Versions() {

View File

@ -20,7 +20,7 @@ func ListHandler(request *types.APIContext) error {
if request.ID == "" {
opts := parse.QueryOptions(request, request.Schema)
data, err = store.List(request, request.Schema, opts)
data, err = store.List(request, request.Schema, &opts)
} else if request.Link == "" {
data, err = store.ByID(request, request.Schema, request.ID)
} else {

View File

@ -7,11 +7,11 @@ import (
"github.com/rancher/norman/types/convert"
)
func QueryFilter(opts types.QueryOptions, data []map[string]interface{}) []map[string]interface{} {
func QueryFilter(opts *types.QueryOptions, data []map[string]interface{}) []map[string]interface{} {
return ApplyQueryOptions(opts, data)
}
func ApplyQueryOptions(options types.QueryOptions, data []map[string]interface{}) []map[string]interface{} {
func ApplyQueryOptions(options *types.QueryOptions, data []map[string]interface{}) []map[string]interface{} {
data = ApplyQueryConditions(options.Conditions, data)
data = ApplySort(options.Sort, data)
return ApplyPagination(options.Pagination, data)

View File

@ -115,7 +115,7 @@ func handler(apiContext *types.APIContext) error {
func streamStore(ctx context.Context, eg *errgroup.Group, apiContext *types.APIContext, schema *types.Schema, result chan map[string]interface{}) {
eg.Go(func() error {
opts := parse.QueryOptions(apiContext, schema)
events, err := schema.Store.Watch(apiContext, schema, opts)
events, err := schema.Store.Watch(apiContext, schema, &opts)
if err != nil {
return err
}

View File

@ -68,7 +68,7 @@ func (c *Store) Delete(apiContext *types.APIContext, schema *types.Schema, id st
return store.Delete(apiContext, schema, id)
}
func (c *Store) List(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) ([]map[string]interface{}, error) {
func (c *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
store, ok := c.schemaStores[key(schema)]
if !ok {
return nil, nil
@ -76,7 +76,7 @@ func (c *Store) List(apiContext *types.APIContext, schema *types.Schema, opt typ
return store.List(apiContext, schema, opt)
}
func (c *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
func (c *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
store, ok := c.schemaStores[key(schema)]
if !ok {
return nil, nil

View File

@ -27,6 +27,6 @@ func (e *Store) Update(apiContext *types.APIContext, schema *types.Schema, data
return nil, nil
}
func (e *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
func (e *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
return nil, nil
}

View File

@ -79,7 +79,7 @@ func (p *Store) byID(apiContext *types.APIContext, schema *types.Schema, id stri
return p.singleResult(apiContext, schema, req)
}
func (p *Store) List(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) ([]map[string]interface{}, error) {
func (p *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
namespace := getNamespace(apiContext, opt)
req := p.common(namespace, p.k8sClient.Get())
@ -99,7 +99,7 @@ func (p *Store) List(apiContext *types.APIContext, schema *types.Schema, opt typ
return apiContext.AccessControl.FilterList(apiContext, result, p.authContext), nil
}
func (p *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
func (p *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
namespace := getNamespace(apiContext, opt)
req := p.common(namespace, p.k8sClient.Get())
@ -126,6 +126,9 @@ func (p *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt ty
for event := range watcher.ResultChan() {
data := event.Object.(*unstructured.Unstructured)
p.fromInternal(schema, data.Object)
if event.Type == watch.Deleted && data.Object != nil {
data.Object[".removed"] = true
}
result <- apiContext.AccessControl.Filter(apiContext, data.Object, p.authContext)
}
close(result)
@ -144,7 +147,7 @@ func (d *unstructuredDecoder) Decode(data []byte, defaults *schema.GroupVersionK
return into, defaults, ejson.Unmarshal(data, &into)
}
func getNamespace(apiContext *types.APIContext, opt types.QueryOptions) string {
func getNamespace(apiContext *types.APIContext, opt *types.QueryOptions) string {
if val, ok := apiContext.SubContext["namespaces"]; ok {
return convert.ToString(val)
}

View File

@ -34,11 +34,11 @@ func (s *Store) ByID(apiContext *types.APIContext, schema *types.Schema, id stri
return nil, nil
}
func (s *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
func (s *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
return nil, nil
}
func (s *Store) List(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) ([]map[string]interface{}, error) {
func (s *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
schemaMap := apiContext.Schemas.SchemasForVersion(*apiContext.Version)
schemas := make([]*types.Schema, 0, len(schemaMap))
schemaData := make([]map[string]interface{}, 0, len(schemaMap))

View File

@ -16,12 +16,19 @@ func NewSubTypeStore(subType string, store types.Store) *Store {
}
}
func (p *Store) List(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) ([]map[string]interface{}, error) {
func (p *Store) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
if data != nil {
data["kind"] = p.subType
}
return p.Store.Create(apiContext, schema, data)
}
func (p *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
opt.Conditions = append(opt.Conditions, types.NewConditionFromString("type", types.ModifierEQ, p.subType))
return p.Store.List(apiContext, schema, opt)
}
func (p *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
func (p *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
opt.Conditions = append(opt.Conditions, types.NewConditionFromString("type", types.ModifierEQ, p.subType))
return p.Store.Watch(apiContext, schema, opt)
}

View File

@ -26,7 +26,7 @@ func (t *Store) ByID(apiContext *types.APIContext, schema *types.Schema, id stri
return t.Transformer(apiContext, data)
}
func (t *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
func (t *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
c, err := t.Store.Watch(apiContext, schema, opt)
if err != nil {
return nil, err
@ -50,7 +50,7 @@ func (t *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt ty
return result, nil
}
func (t *Store) List(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) ([]map[string]interface{}, error) {
func (t *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
data, err := t.Store.List(apiContext, schema, opt)
if err != nil {
return nil, err

View File

@ -21,12 +21,12 @@ func (s *StoreWrapper) ByID(apiContext *types.APIContext, schema *types.Schema,
return nil, err
}
return apiContext.FilterObject(types.QueryOptions{
return apiContext.FilterObject(&types.QueryOptions{
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
}, data), nil
}
func (s *StoreWrapper) List(apiContext *types.APIContext, schema *types.Schema, opts types.QueryOptions) ([]map[string]interface{}, error) {
func (s *StoreWrapper) List(apiContext *types.APIContext, schema *types.Schema, opts *types.QueryOptions) ([]map[string]interface{}, error) {
opts.Conditions = append(opts.Conditions, apiContext.SubContextAttributeProvider.Query(apiContext, schema)...)
data, err := s.store.List(apiContext, schema, opts)
if err != nil {
@ -36,7 +36,7 @@ func (s *StoreWrapper) List(apiContext *types.APIContext, schema *types.Schema,
return apiContext.FilterList(opts, data), nil
}
func (s *StoreWrapper) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
func (s *StoreWrapper) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
c, err := s.store.Watch(apiContext, schema, opt)
if err != nil || c == nil {
return nil, err
@ -45,7 +45,7 @@ func (s *StoreWrapper) Watch(apiContext *types.APIContext, schema *types.Schema,
result := make(chan map[string]interface{})
go func() {
for item := range c {
item = apiContext.FilterObject(types.QueryOptions{
item = apiContext.FilterObject(&types.QueryOptions{
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
}, item)
if item != nil {
@ -85,7 +85,7 @@ func (s *StoreWrapper) Update(apiContext *types.APIContext, schema *types.Schema
return nil, err
}
return apiContext.FilterObject(types.QueryOptions{
return apiContext.FilterObject(&types.QueryOptions{
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
}, data), nil
}
@ -109,7 +109,7 @@ func validateGet(apiContext *types.APIContext, schema *types.Schema, id string)
return err
}
if apiContext.Filter(types.QueryOptions{
if apiContext.Filter(&types.QueryOptions{
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
}, existing) == nil {
return httperror.NewAPIError(httperror.NotFound, "failed to find "+id)

View File

@ -44,7 +44,7 @@ type ActionHandler func(actionName string, action *Action, request *APIContext)
type RequestHandler func(request *APIContext) error
type QueryFilter func(opts QueryOptions, data []map[string]interface{}) []map[string]interface{}
type QueryFilter func(opts *QueryOptions, data []map[string]interface{}) []map[string]interface{}
type Validator func(request *APIContext, data map[string]interface{}) error
@ -100,11 +100,11 @@ func (r *APIContext) WriteResponse(code int, obj interface{}) {
r.ResponseWriter.Write(r, code, obj)
}
func (r *APIContext) FilterList(opts QueryOptions, obj []map[string]interface{}) []map[string]interface{} {
func (r *APIContext) FilterList(opts *QueryOptions, obj []map[string]interface{}) []map[string]interface{} {
return r.QueryFilter(opts, obj)
}
func (r *APIContext) FilterObject(opts QueryOptions, obj map[string]interface{}) map[string]interface{} {
func (r *APIContext) FilterObject(opts *QueryOptions, obj map[string]interface{}) map[string]interface{} {
opts.Pagination = nil
result := r.QueryFilter(opts, []map[string]interface{}{obj})
if len(result) == 0 {
@ -113,7 +113,7 @@ func (r *APIContext) FilterObject(opts QueryOptions, obj map[string]interface{})
return result[0]
}
func (r *APIContext) Filter(opts QueryOptions, obj interface{}) interface{} {
func (r *APIContext) Filter(opts *QueryOptions, obj interface{}) interface{} {
switch v := obj.(type) {
case []map[string]interface{}:
return r.FilterList(opts, v)
@ -161,9 +161,9 @@ type URLBuilder interface {
type Store interface {
ByID(apiContext *APIContext, schema *Schema, id string) (map[string]interface{}, error)
List(apiContext *APIContext, schema *Schema, opt QueryOptions) ([]map[string]interface{}, error)
List(apiContext *APIContext, schema *Schema, opt *QueryOptions) ([]map[string]interface{}, error)
Create(apiContext *APIContext, schema *Schema, data map[string]interface{}) (map[string]interface{}, error)
Update(apiContext *APIContext, schema *Schema, data map[string]interface{}, id string) (map[string]interface{}, error)
Delete(apiContext *APIContext, schema *Schema, id string) (map[string]interface{}, error)
Watch(apiContext *APIContext, schema *Schema, opt QueryOptions) (chan map[string]interface{}, error)
Watch(apiContext *APIContext, schema *Schema, opt *QueryOptions) (chan map[string]interface{}, error)
}