knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/ptr/value.go (about)

     1  /*
     2  Copyright 2021 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package ptr
    18  
    19  import "time"
    20  
    21  // Int32Value is a helper for turning pointers to integers into values for use
    22  // in API types that want int32.
    23  func Int32Value(i *int32) int32 {
    24  	if i == nil {
    25  		return 0
    26  	}
    27  	return *i
    28  }
    29  
    30  // Int64Value is a helper for turning pointers to integers into values for use
    31  // in API types that want int64.
    32  func Int64Value(i *int64) int64 {
    33  	if i == nil {
    34  		return 0
    35  	}
    36  	return *i
    37  }
    38  
    39  // Float32Value is a helper for turning pointers to floats into values for use
    40  // in API types that want float32.
    41  func Float32Value(f *float32) float32 {
    42  	if f == nil {
    43  		return 0
    44  	}
    45  	return *f
    46  }
    47  
    48  // Float64Value is a helper for turning pointers to floats into values for use
    49  // in API types that want float64.
    50  func Float64Value(f *float64) float64 {
    51  	if f == nil {
    52  		return 0
    53  	}
    54  	return *f
    55  }
    56  
    57  // BoolValue is a helper for turning pointers to bools into values for use in
    58  // API types that want bool.
    59  func BoolValue(b *bool) bool {
    60  	if b == nil {
    61  		return false
    62  	}
    63  	return *b
    64  }
    65  
    66  // StringValue is a helper for turning pointers to strings into values for use
    67  // in API types that want string.
    68  func StringValue(s *string) string {
    69  	if s == nil {
    70  		return ""
    71  	}
    72  	return *s
    73  }
    74  
    75  // DurationValue is a helper for turning *time.Duration into values for use in
    76  // API types that want time.Duration.
    77  func DurationValue(t *time.Duration) time.Duration {
    78  	if t == nil {
    79  		return 0
    80  	}
    81  	return *t
    82  }
    83  
    84  // TimeValue is a helper for turning *time.Time into values for use in API
    85  // types that want API types that want time.Time.
    86  func TimeValue(t *time.Time) time.Time {
    87  	if t == nil {
    88  		return time.Time{}
    89  	}
    90  	return *t
    91  }