Use one copy of EnforcePtr

This commit is contained in:
Clayton Coleman
2014-10-09 18:38:34 -04:00
parent d488e238dd
commit 13992837ea
3 changed files with 6 additions and 17 deletions

View File

@@ -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())
}

View File

@@ -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
}
@@ -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
}