...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/slice/slice.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/slice

     1  // Copyright 2022 Google LLC
     2  //
     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  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package slice
    16  
    17  import "sort"
    18  
    19  func StringSliceContains(l []string, s string) bool {
    20  	for _, elem := range l {
    21  		if elem == s {
    22  			return true
    23  		}
    24  	}
    25  	return false
    26  }
    27  
    28  // IncludeString inserts the given string to the sorted slice if not already
    29  // included. It is assumed that the slice is already in sorted order.
    30  func IncludeString(l []string, s string) []string {
    31  	i := sort.Search(
    32  		len(l),
    33  		func(i int) bool {
    34  			return l[i] >= s
    35  		},
    36  	)
    37  	if i < len(l) && l[i] == s {
    38  		// string is already in slice
    39  		return l
    40  	}
    41  	l = append(l, "")
    42  	copy(l[i+1:], l[i:])
    43  	l[i] = s
    44  	return l
    45  }
    46  
    47  func RemoveStringFromStringSlice(l []string, s string) []string {
    48  	newSlice := make([]string, 0)
    49  	for _, elem := range l {
    50  		if elem != s {
    51  			newSlice = append(newSlice, elem)
    52  		}
    53  	}
    54  	return newSlice
    55  }
    56  
    57  func ConcatStringSlices(slices ...[]string) []string {
    58  	newSlice := make([]string, 0)
    59  	for _, s := range slices {
    60  		newSlice = append(newSlice, s...)
    61  	}
    62  	return newSlice
    63  }
    64  
    65  // IsListOfStringInterfaceMaps returns true if the given list of interface{}
    66  // values is actually a list of map[string]interface{} values. Returns false if
    67  // the given list is empty.
    68  func IsListOfStringInterfaceMaps(list []interface{}) bool {
    69  	if len(list) == 0 {
    70  		return false
    71  	}
    72  	for _, e := range list {
    73  		_, ok := e.(map[string]interface{})
    74  		if !ok {
    75  			return false
    76  		}
    77  	}
    78  	return true
    79  }
    80  
    81  func Reverse(slice []string) {
    82  	length := len(slice)
    83  	midIndex := length / 2
    84  
    85  	for i := 0; i < midIndex; i++ {
    86  		j := length - i - 1
    87  
    88  		slice[i], slice[j] = slice[j], slice[i]
    89  	}
    90  }
    91  

View as plain text