github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/util/stringset/stringset.go (about)

     1  /*
     2  Copyright 2020 The Skaffold 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 stringset
    18  
    19  import "sort"
    20  
    21  type unit struct{}
    22  
    23  // StringSet helps to de-duplicate a set of strings.
    24  type StringSet map[string]unit
    25  
    26  // New returns a new StringSet object.
    27  func New() StringSet {
    28  	return make(map[string]unit)
    29  }
    30  
    31  // Insert adds strings to the set.
    32  func (s StringSet) Insert(strings ...string) {
    33  	for _, item := range strings {
    34  		s[item] = unit{}
    35  	}
    36  }
    37  
    38  // ToList returns the sorted list of inserted strings.
    39  func (s StringSet) ToList() []string {
    40  	var res []string
    41  	for item := range s {
    42  		res = append(res, item)
    43  	}
    44  	sort.Strings(res)
    45  	return res
    46  }
    47  
    48  // Delete deletes the specified string in the set
    49  // if set is nil or string is not present, its a no-op
    50  func (s StringSet) Delete(str string) {
    51  	delete(s, str)
    52  }
    53  
    54  // Contains checks if a specified string is present in the set.
    55  func (s StringSet) Contains(str string) bool {
    56  	_, ok := s[str]
    57  	return ok
    58  }