2
0

pointer.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. // Int32Ptr returns a pointer to an int32
  43. func Int32Ptr(i int32) *int32 {
  44. return &i
  45. }
  46. // Int32PtrDerefOr dereference the int32 ptr and returns it if not nil,
  47. // else returns def.
  48. func Int32PtrDerefOr(ptr *int32, def int32) int32 {
  49. if ptr != nil {
  50. return *ptr
  51. }
  52. return def
  53. }
  54. // Int64Ptr returns a pointer to an int64
  55. func Int64Ptr(i int64) *int64 {
  56. return &i
  57. }
  58. // Int64PtrDerefOr dereference the int64 ptr and returns it if not nil,
  59. // else returns def.
  60. func Int64PtrDerefOr(ptr *int64, def int64) int64 {
  61. if ptr != nil {
  62. return *ptr
  63. }
  64. return def
  65. }
  66. // BoolPtr returns a pointer to a bool
  67. func BoolPtr(b bool) *bool {
  68. return &b
  69. }
  70. // BoolPtrDerefOr dereference the bool ptr and returns it if not nil,
  71. // else returns def.
  72. func BoolPtrDerefOr(ptr *bool, def bool) bool {
  73. if ptr != nil {
  74. return *ptr
  75. }
  76. return def
  77. }
  78. // StringPtr returns a pointer to the passed string.
  79. func StringPtr(s string) *string {
  80. return &s
  81. }
  82. // StringPtrDerefOr dereference the string ptr and returns it if not nil,
  83. // else returns def.
  84. func StringPtrDerefOr(ptr *string, def string) string {
  85. if ptr != nil {
  86. return *ptr
  87. }
  88. return def
  89. }
  90. // Float32Ptr returns a pointer to the passed float32.
  91. func Float32Ptr(i float32) *float32 {
  92. return &i
  93. }
  94. // Float32PtrDerefOr dereference the float32 ptr and returns it if not nil,
  95. // else returns def.
  96. func Float32PtrDerefOr(ptr *float32, def float32) float32 {
  97. if ptr != nil {
  98. return *ptr
  99. }
  100. return def
  101. }
  102. // Float64Ptr returns a pointer to the passed float64.
  103. func Float64Ptr(i float64) *float64 {
  104. return &i
  105. }
  106. // Float64PtrDerefOr dereference the float64 ptr and returns it if not nil,
  107. // else returns def.
  108. func Float64PtrDerefOr(ptr *float64, def float64) float64 {
  109. if ptr != nil {
  110. return *ptr
  111. }
  112. return def
  113. }