1/*
2Copyright 2018 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 pointer
18
19import (
20	"fmt"
21	"reflect"
22)
23
24// AllPtrFieldsNil tests whether all pointer fields in a struct are nil.  This is useful when,
25// for example, an API struct is handled by plugins which need to distinguish
26// "no plugin accepted this spec" from "this spec is empty".
27//
28// This function is only valid for structs and pointers to structs.  Any other
29// type will cause a panic.  Passing a typed nil pointer will return true.
30func AllPtrFieldsNil(obj interface{}) bool {
31	v := reflect.ValueOf(obj)
32	if !v.IsValid() {
33		panic(fmt.Sprintf("reflect.ValueOf() produced a non-valid Value for %#v", obj))
34	}
35	if v.Kind() == reflect.Ptr {
36		if v.IsNil() {
37			return true
38		}
39		v = v.Elem()
40	}
41	for i := 0; i < v.NumField(); i++ {
42		if v.Field(i).Kind() == reflect.Ptr && !v.Field(i).IsNil() {
43			return false
44		}
45	}
46	return true
47}
48
49// Int32Ptr returns a pointer to an int32
50func Int32Ptr(i int32) *int32 {
51	return &i
52}
53
54// Int32PtrDerefOr dereference the int32 ptr and returns it if not nil,
55// else returns def.
56func Int32PtrDerefOr(ptr *int32, def int32) int32 {
57	if ptr != nil {
58		return *ptr
59	}
60	return def
61}
62
63// Int64Ptr returns a pointer to an int64
64func Int64Ptr(i int64) *int64 {
65	return &i
66}
67
68// Int64PtrDerefOr dereference the int64 ptr and returns it if not nil,
69// else returns def.
70func Int64PtrDerefOr(ptr *int64, def int64) int64 {
71	if ptr != nil {
72		return *ptr
73	}
74	return def
75}
76
77// BoolPtr returns a pointer to a bool
78func BoolPtr(b bool) *bool {
79	return &b
80}
81
82// BoolPtrDerefOr dereference the bool ptr and returns it if not nil,
83// else returns def.
84func BoolPtrDerefOr(ptr *bool, def bool) bool {
85	if ptr != nil {
86		return *ptr
87	}
88	return def
89}
90
91// StringPtr returns a pointer to the passed string.
92func StringPtr(s string) *string {
93	return &s
94}
95
96// StringPtrDerefOr dereference the string ptr and returns it if not nil,
97// else returns def.
98func StringPtrDerefOr(ptr *string, def string) string {
99	if ptr != nil {
100		return *ptr
101	}
102	return def
103}
104
105// Float32Ptr returns a pointer to the passed float32.
106func Float32Ptr(i float32) *float32 {
107	return &i
108}
109
110// Float32PtrDerefOr dereference the float32 ptr and returns it if not nil,
111// else returns def.
112func Float32PtrDerefOr(ptr *float32, def float32) float32 {
113	if ptr != nil {
114		return *ptr
115	}
116	return def
117}
118
119// Float64Ptr returns a pointer to the passed float64.
120func Float64Ptr(i float64) *float64 {
121	return &i
122}
123
124// Float64PtrDerefOr dereference the float64 ptr and returns it if not nil,
125// else returns def.
126func Float64PtrDerefOr(ptr *float64, def float64) float64 {
127	if ptr != nil {
128		return *ptr
129	}
130	return def
131}
132