blob: 4ebc1ebc511f52520c3a1b85e64c890e54efbff7 [file] [log] [blame]
sslobodrd046be82019-01-16 10:02:22 -05001/*
2Copyright 2014 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package conversion
18
19import (
20 "fmt"
21 "reflect"
22)
23
24// EnforcePtr ensures that obj is a pointer of some sort. Returns a reflect.Value
25// of the dereferenced pointer, ensuring that it is settable/addressable.
26// Returns an error if this is not possible.
27func EnforcePtr(obj interface{}) (reflect.Value, error) {
28 v := reflect.ValueOf(obj)
29 if v.Kind() != reflect.Ptr {
30 if v.Kind() == reflect.Invalid {
31 return reflect.Value{}, fmt.Errorf("expected pointer, but got invalid kind")
32 }
33 return reflect.Value{}, fmt.Errorf("expected pointer, but got %v type", v.Type())
34 }
35 if v.IsNil() {
36 return reflect.Value{}, fmt.Errorf("expected pointer, but got nil")
37 }
38 return v.Elem(), nil
39}