pointer.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package pointer
  14. import (
  15. "fmt"
  16. "reflect"
  17. )
  18. // AllPtrFieldsNil tests whether all pointer fields in a struct are nil. This is useful when,
  19. // for example, an API struct is handled by plugins which need to distinguish
  20. // "no plugin accepted this spec" from "this spec is empty".
  21. //
  22. // This function is only valid for structs and pointers to structs. Any other
  23. // type will cause a panic. Passing a typed nil pointer will return true.
  24. func AllPtrFieldsNil(obj interface{}) bool {
  25. v := reflect.ValueOf(obj)
  26. if !v.IsValid() {
  27. panic(fmt.Sprintf("reflect.ValueOf() produced a non-valid Value for %#v", obj))
  28. }
  29. if v.Kind() == reflect.Ptr {
  30. if v.IsNil() {
  31. return true
  32. }
  33. v = v.Elem()
  34. }
  35. for i := 0; i < v.NumField(); i++ {
  36. if v.Field(i).Kind() == reflect.Ptr && !v.Field(i).IsNil() {
  37. return false
  38. }
  39. }
  40. return true
  41. }
  42. // Int returns a pointer to an int
  43. func Int(i int) *int {
  44. return &i
  45. }
  46. var IntPtr = Int // for back-compat
  47. // IntDeref dereferences the int ptr and returns it if not nil, or else
  48. // returns def.
  49. func IntDeref(ptr *int, def int) int {
  50. if ptr != nil {
  51. return *ptr
  52. }
  53. return def
  54. }
  55. var IntPtrDerefOr = IntDeref // for back-compat
  56. // Int32 returns a pointer to an int32.
  57. func Int32(i int32) *int32 {
  58. return &i
  59. }
  60. var Int32Ptr = Int32 // for back-compat
  61. // Int32Deref dereferences the int32 ptr and returns it if not nil, or else
  62. // returns def.
  63. func Int32Deref(ptr *int32, def int32) int32 {
  64. if ptr != nil {
  65. return *ptr
  66. }
  67. return def
  68. }
  69. var Int32PtrDerefOr = Int32Deref // for back-compat
  70. // Int32Equal returns true if both arguments are nil or both arguments
  71. // dereference to the same value.
  72. func Int32Equal(a, b *int32) bool {
  73. if (a == nil) != (b == nil) {
  74. return false
  75. }
  76. if a == nil {
  77. return true
  78. }
  79. return *a == *b
  80. }
  81. // Int64 returns a pointer to an int64.
  82. func Int64(i int64) *int64 {
  83. return &i
  84. }
  85. var Int64Ptr = Int64 // for back-compat
  86. // Int64Deref dereferences the int64 ptr and returns it if not nil, or else
  87. // returns def.
  88. func Int64Deref(ptr *int64, def int64) int64 {
  89. if ptr != nil {
  90. return *ptr
  91. }
  92. return def
  93. }
  94. var Int64PtrDerefOr = Int64Deref // for back-compat
  95. // Int64Equal returns true if both arguments are nil or both arguments
  96. // dereference to the same value.
  97. func Int64Equal(a, b *int64) bool {
  98. if (a == nil) != (b == nil) {
  99. return false
  100. }
  101. if a == nil {
  102. return true
  103. }
  104. return *a == *b
  105. }
  106. // Bool returns a pointer to a bool.
  107. func Bool(b bool) *bool {
  108. return &b
  109. }
  110. var BoolPtr = Bool // for back-compat
  111. // BoolDeref dereferences the bool ptr and returns it if not nil, or else
  112. // returns def.
  113. func BoolDeref(ptr *bool, def bool) bool {
  114. if ptr != nil {
  115. return *ptr
  116. }
  117. return def
  118. }
  119. var BoolPtrDerefOr = BoolDeref // for back-compat
  120. // BoolEqual returns true if both arguments are nil or both arguments
  121. // dereference to the same value.
  122. func BoolEqual(a, b *bool) bool {
  123. if (a == nil) != (b == nil) {
  124. return false
  125. }
  126. if a == nil {
  127. return true
  128. }
  129. return *a == *b
  130. }
  131. // String returns a pointer to a string.
  132. func String(s string) *string {
  133. return &s
  134. }
  135. var StringPtr = String // for back-compat
  136. // StringDeref dereferences the string ptr and returns it if not nil, or else
  137. // returns def.
  138. func StringDeref(ptr *string, def string) string {
  139. if ptr != nil {
  140. return *ptr
  141. }
  142. return def
  143. }
  144. var StringPtrDerefOr = StringDeref // for back-compat
  145. // StringEqual returns true if both arguments are nil or both arguments
  146. // dereference to the same value.
  147. func StringEqual(a, b *string) bool {
  148. if (a == nil) != (b == nil) {
  149. return false
  150. }
  151. if a == nil {
  152. return true
  153. }
  154. return *a == *b
  155. }
  156. // Float32 returns a pointer to the a float32.
  157. func Float32(i float32) *float32 {
  158. return &i
  159. }
  160. var Float32Ptr = Float32
  161. // Float32Deref dereferences the float32 ptr and returns it if not nil, or else
  162. // returns def.
  163. func Float32Deref(ptr *float32, def float32) float32 {
  164. if ptr != nil {
  165. return *ptr
  166. }
  167. return def
  168. }
  169. var Float32PtrDerefOr = Float32Deref // for back-compat
  170. // Float32Equal returns true if both arguments are nil or both arguments
  171. // dereference to the same value.
  172. func Float32Equal(a, b *float32) bool {
  173. if (a == nil) != (b == nil) {
  174. return false
  175. }
  176. if a == nil {
  177. return true
  178. }
  179. return *a == *b
  180. }
  181. // Float64 returns a pointer to the a float64.
  182. func Float64(i float64) *float64 {
  183. return &i
  184. }
  185. var Float64Ptr = Float64
  186. // Float64Deref dereferences the float64 ptr and returns it if not nil, or else
  187. // returns def.
  188. func Float64Deref(ptr *float64, def float64) float64 {
  189. if ptr != nil {
  190. return *ptr
  191. }
  192. return def
  193. }
  194. var Float64PtrDerefOr = Float64Deref // for back-compat
  195. // Float64Equal returns true if both arguments are nil or both arguments
  196. // dereference to the same value.
  197. func Float64Equal(a, b *float64) bool {
  198. if (a == nil) != (b == nil) {
  199. return false
  200. }
  201. if a == nil {
  202. return true
  203. }
  204. return *a == *b
  205. }