Add IsList and List conversion

This commit is contained in:
Darren Shepherd 2019-08-12 15:15:09 -07:00
parent 674f957b8b
commit 68d1d582c3
3 changed files with 34 additions and 4 deletions

1
go.mod
View File

@ -16,6 +16,7 @@ require (
github.com/rancher/wrangler-api v0.1.5-0.20190619170228-c3525df45215
github.com/sirupsen/logrus v1.4.2
github.com/urfave/cli v1.20.0
golang.org/x/sync v0.0.0-20190423024810-112230192c58
google.golang.org/appengine v1.5.0 // indirect
k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b
k8s.io/apiextensions-apiserver v0.0.0-20190409022649-727a075fdec8

View File

@ -119,6 +119,10 @@ func (r *APIRequest) WithContext(ctx context.Context) *APIRequest {
return &result
}
func (r *APIRequest) Context() context.Context {
return r.Request.Context()
}
func (r *APIRequest) GetUser() string {
user, ok := request.UserFrom(r.Request.Context())
if ok {
@ -152,7 +156,7 @@ func (r *APIRequest) FilterObject(opts *QueryOptions, schema *Schema, obj APIObj
}
func (r *APIRequest) Filter(opts *QueryOptions, schema *Schema, obj APIObject) APIObject {
if _, ok := obj.ListCheck(); ok {
if obj.IsList() {
return r.FilterList(opts, schema, obj)
}
return r.FilterObject(opts, schema, obj)
@ -258,15 +262,40 @@ func (a *APIObject) List() data.List {
return result
}
func (a *APIObject) IsList() bool {
_, ret := a.listCheck(false)
return ret
}
func (a *APIObject) ListCheck() (data.List, bool) {
return a.listCheck(true)
}
func (a *APIObject) listCheck(doConvert bool) (data.List, bool) {
if a == nil {
return nil, false
}
if result, ok := a.Object.(data.List); ok {
return result, true
}
result, ok := a.Object.([]map[string]interface{})
return result, ok
if result, ok := a.Object.([]map[string]interface{}); ok {
return result, true
}
if result, ok := a.Object.([]interface{}); ok {
if !doConvert {
return nil, true
}
mapResult := make(data.List, 0, len(result))
for _, obj := range result {
asMap, err := convert.EncodeToMap(obj)
if err != nil {
return nil, false
}
mapResult = append(mapResult, asMap)
}
return mapResult, true
}
return nil, false
}
func (a *APIObject) First() APIObject {

2
vendor/modules.txt vendored
View File

@ -42,6 +42,7 @@ github.com/pkg/errors
github.com/rancher/norman/pkg/authorization
github.com/rancher/norman/pkg/types
github.com/rancher/norman/pkg/types/convert
github.com/rancher/norman/pkg/store/empty
github.com/rancher/norman/pkg/types/values
github.com/rancher/norman/pkg/api/builtin
github.com/rancher/norman/pkg/store/proxy
@ -52,7 +53,6 @@ github.com/rancher/norman/pkg/httperror
github.com/rancher/norman/pkg/types/slice
github.com/rancher/norman/pkg/data
github.com/rancher/norman/pkg/types/definition
github.com/rancher/norman/pkg/store/empty
github.com/rancher/norman/pkg/store/schema
github.com/rancher/norman/pkg/types/convert/merge
github.com/rancher/norman/pkg/api/access