Handle typed responses better

This commit is contained in:
Darren Shepherd 2019-08-12 16:46:57 -07:00
parent 4016d67f8b
commit 73646d17ea
4 changed files with 23 additions and 4 deletions

View File

@ -4,11 +4,13 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"reflect"
"github.com/rancher/norman/pkg/data" "github.com/rancher/norman/pkg/data"
"github.com/rancher/norman/pkg/parse" "github.com/rancher/norman/pkg/parse"
"github.com/rancher/norman/pkg/parse/builder" "github.com/rancher/norman/pkg/parse/builder"
"github.com/rancher/norman/pkg/types" "github.com/rancher/norman/pkg/types"
"github.com/rancher/norman/pkg/types/convert"
"github.com/rancher/norman/pkg/types/definition" "github.com/rancher/norman/pkg/types/definition"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -60,7 +62,15 @@ func (j *EncodingResponseWriter) VersionBody(apiOp *types.APIRequest, writer io.
case types.RawResource: case types.RawResource:
output = v output = v
default: default:
output = v mapData, err := convert.EncodeToMap(obj)
if err != nil {
return err
}
schema := apiOp.Schemas.SchemaFor(reflect.TypeOf(obj))
if schema != nil && mapData != nil {
mapData["type"] = schema.ID
}
output = j.convert(builder, apiOp, mapData)
} }
if list, ok := output.(*types.GenericCollection); ok && revision != "" { if list, ok := output.(*types.GenericCollection); ok && revision != "" {

View File

@ -199,8 +199,9 @@ func (s *Store) toAPIEvent(apiOp *types.APIRequest, schema *types.Schema, et wat
s.fromInternal(apiOp, schema, obj.Object) s.fromInternal(apiOp, schema, obj.Object)
return types.APIEvent{ return types.APIEvent{
Name: name, Name: name,
Object: types.ToAPI(obj.Object), Revision: obj.GetResourceVersion(),
Object: types.ToAPI(obj.Object),
} }
} }

View File

@ -33,6 +33,11 @@ func (s *Schemas) getTypeName(t reflect.Type) string {
return convert.LowerTitle(t.Name()) return convert.LowerTitle(t.Name())
} }
func (s *Schemas) SchemaFor(t reflect.Type) *Schema {
name := s.getTypeName(t)
return s.Schema(name)
}
func (s *Schemas) AddMapperForType(obj interface{}, mapper ...Mapper) *Schemas { func (s *Schemas) AddMapperForType(obj interface{}, mapper ...Mapper) *Schemas {
if len(mapper) == 0 { if len(mapper) == 0 {
return s return s
@ -79,6 +84,7 @@ func (s *Schemas) newSchemaFromType(t reflect.Type, typeName string) (*Schema, e
ResourceFields: map[string]Field{}, ResourceFields: map[string]Field{},
ResourceActions: map[string]Action{}, ResourceActions: map[string]Action{},
CollectionActions: map[string]Action{}, CollectionActions: map[string]Action{},
Attributes: map[string]interface{}{},
} }
s.processingTypes[t] = schema s.processingTypes[t] = schema

View File

@ -211,6 +211,7 @@ type WatchRequest struct {
type APIEvent struct { type APIEvent struct {
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
ResourceType string `json:"resourceType,omitempty"` ResourceType string `json:"resourceType,omitempty"`
Revision string `json:"revision,omitempty"`
Object APIObject `json:"-"` Object APIObject `json:"-"`
Error error `json:"-"` Error error `json:"-"`
// Data should be used // Data should be used
@ -247,7 +248,8 @@ func (a APIObject) IsNil() bool {
if a.Object == nil { if a.Object == nil {
return true return true
} }
return reflect.ValueOf(a.Object).IsNil() val := reflect.ValueOf(a.Object)
return val.Kind() == reflect.Ptr && val.IsNil()
} }
func (a *APIObject) List() data.List { func (a *APIObject) List() data.List {