Skip to content

Commit

Permalink
Use one copy of EnforcePtr
Browse files Browse the repository at this point in the history
  • Loading branch information
smarterclayton committed Oct 14, 2014
1 parent d488e23 commit 1399283
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pkg/conversion/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import (
//
func (s *Scheme) EncodeToVersion(obj interface{}, destVersion string) (data []byte, err error) {
obj = maybeCopy(obj)
v, _ := enforcePtr(obj) // maybeCopy guarantees a pointer
v, _ := EnforcePtr(obj) // maybeCopy guarantees a pointer
if _, registered := s.typeToVersion[v.Type()]; !registered {
return nil, fmt.Errorf("type %v is not registered and it will be impossible to Decode it, therefore Encode will refuse to encode it.", v.Type())
}
Expand Down
13 changes: 1 addition & 12 deletions pkg/conversion/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (s *Scheme) DataVersionAndKind(data []byte) (version, kind string, err erro
// ObjectVersionAndKind returns the API version and kind of the go object,
// or an error if it's not a pointer or is unregistered.
func (s *Scheme) ObjectVersionAndKind(obj interface{}) (apiVersion, kind string, err error) {
v, err := enforcePtr(obj)
v, err := EnforcePtr(obj)
if err != nil {
return "", "", err
}
Expand Down Expand Up @@ -261,14 +261,3 @@ func maybeCopy(obj interface{}) interface{} {
v2.Elem().Set(v)
return v2.Interface()
}

// enforcePtr ensures that obj is a pointer of some sort. Returns a reflect.Value
// of the dereferenced pointer, ensuring that it is settable/addressable.
// Returns an error if this is not possible.
func enforcePtr(obj interface{}) (reflect.Value, error) {
v := reflect.ValueOf(obj)
if v.Kind() != reflect.Ptr {
return reflect.Value{}, fmt.Errorf("expected pointer, but got %v", v.Type().Name())
}
return v.Elem(), nil
}
8 changes: 4 additions & 4 deletions pkg/runtime/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (s *Scheme) Convert(in, out interface{}) error {
// FindTypeMeta takes an arbitary api type, returns pointer to its TypeMeta field.
// obj must be a pointer to an api type.
func FindTypeMeta(obj Object) (TypeMetaInterface, error) {
v, err := enforcePtr(obj)
v, err := conversion.EnforcePtr(obj)
if err != nil {
return nil, err
}
Expand All @@ -231,11 +231,11 @@ func FindTypeMeta(obj Object) (TypeMetaInterface, error) {
if v.Kind() != reflect.Struct {
return nil, fmt.Errorf("expected struct, but got %v: %v (%#v)", v.Kind(), name, v.Interface())
}
jsonBase := v.FieldByName("TypeMeta")
if !jsonBase.IsValid() {
typeMeta := v.FieldByName("TypeMeta")
if !typeMeta.IsValid() {
return nil, fmt.Errorf("struct %v lacks embedded JSON type", name)
}
g, err := newGenericTypeMeta(jsonBase)
g, err := newGenericTypeMeta(typeMeta)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 1399283

Please sign in to comment.