sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/pointers.go (about)

     1  /*
     2  Copyright 2023 The Kubernetes 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 azure
    18  
    19  import "k8s.io/utils/ptr"
    20  
    21  // StringSlice returns a string slice value for the passed string slice pointer. It returns a nil
    22  // slice if the pointer is nil.
    23  func StringSlice(s *[]string) []string {
    24  	if s != nil {
    25  		return *s
    26  	}
    27  	return nil
    28  }
    29  
    30  // PtrSlice returns a slice of pointers from a pointer to a slice. It returns nil if the
    31  // pointer is nil or the slice pointed to is empty.
    32  func PtrSlice[T any](p *[]T) []*T {
    33  	if p == nil || len(*p) == 0 {
    34  		return nil
    35  	}
    36  	s := make([]*T, 0, len(*p))
    37  	for _, v := range *p {
    38  		s = append(s, ptr.To(v))
    39  	}
    40  	return s
    41  }
    42  
    43  // AliasOrNil returns a pointer to a string-derived type from a passed string pointer,
    44  // or nil if the pointer is nil or an empty string.
    45  func AliasOrNil[T ~string](s *string) *T {
    46  	if s == nil || *s == "" {
    47  		return nil
    48  	}
    49  	return ptr.To(T(*s))
    50  }
    51  
    52  // StringMapPtr converts a map[string]string into a map[string]*string. It returns nil if the map is nil.
    53  func StringMapPtr(m map[string]string) map[string]*string {
    54  	if m == nil {
    55  		return nil
    56  	}
    57  	msp := make(map[string]*string, len(m))
    58  	for k, v := range m {
    59  		msp[k] = ptr.To(v)
    60  	}
    61  	return msp
    62  }