github.com/gofunct/common@v0.0.0-20190131174352-fd058c7fbf22/pkg/net/ipnet.go (about)

     1  /*
     2  Copyright 2016 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 net
    18  
    19  import (
    20  	"net"
    21  	"strings"
    22  )
    23  
    24  // IPNetSet maps string to net.IPNet.
    25  type IPNetSet map[string]*net.IPNet
    26  
    27  // ParseIPNets parses string slice to IPNetSet.
    28  func ParseIPNets(specs ...string) (IPNetSet, error) {
    29  	ipnetset := make(IPNetSet)
    30  	for _, spec := range specs {
    31  		spec = strings.TrimSpace(spec)
    32  		_, ipnet, err := net.ParseCIDR(spec)
    33  		if err != nil {
    34  			return nil, err
    35  		}
    36  		k := ipnet.String() // In case of normalization
    37  		ipnetset[k] = ipnet
    38  	}
    39  	return ipnetset, nil
    40  }
    41  
    42  // Insert adds items to the set.
    43  func (s IPNetSet) Insert(items ...*net.IPNet) {
    44  	for _, item := range items {
    45  		s[item.String()] = item
    46  	}
    47  }
    48  
    49  // Delete removes all items from the set.
    50  func (s IPNetSet) Delete(items ...*net.IPNet) {
    51  	for _, item := range items {
    52  		delete(s, item.String())
    53  	}
    54  }
    55  
    56  // Has returns true if and only if item is contained in the set.
    57  func (s IPNetSet) Has(item *net.IPNet) bool {
    58  	_, contained := s[item.String()]
    59  	return contained
    60  }
    61  
    62  // HasAll returns true if and only if all items are contained in the set.
    63  func (s IPNetSet) HasAll(items ...*net.IPNet) bool {
    64  	for _, item := range items {
    65  		if !s.Has(item) {
    66  			return false
    67  		}
    68  	}
    69  	return true
    70  }
    71  
    72  // Difference returns a set of objects that are not in s2
    73  // For example:
    74  // s1 = {a1, a2, a3}
    75  // s2 = {a1, a2, a4, a5}
    76  // s1.Difference(s2) = {a3}
    77  // s2.Difference(s1) = {a4, a5}
    78  func (s IPNetSet) Difference(s2 IPNetSet) IPNetSet {
    79  	result := make(IPNetSet)
    80  	for k, i := range s {
    81  		_, found := s2[k]
    82  		if found {
    83  			continue
    84  		}
    85  		result[k] = i
    86  	}
    87  	return result
    88  }
    89  
    90  // StringSlice returns a []string with the String representation of each element in the set.
    91  // Order is undefined.
    92  func (s IPNetSet) StringSlice() []string {
    93  	a := make([]string, 0, len(s))
    94  	for k := range s {
    95  		a = append(a, k)
    96  	}
    97  	return a
    98  }
    99  
   100  // IsSuperset returns true if and only if s1 is a superset of s2.
   101  func (s IPNetSet) IsSuperset(s2 IPNetSet) bool {
   102  	for k := range s2 {
   103  		_, found := s[k]
   104  		if !found {
   105  			return false
   106  		}
   107  	}
   108  	return true
   109  }
   110  
   111  // Equal returns true if and only if s1 is equal (as a set) to s2.
   112  // Two sets are equal if their membership is identical.
   113  // (In practice, this means same elements, order doesn't matter)
   114  func (s IPNetSet) Equal(s2 IPNetSet) bool {
   115  	return len(s) == len(s2) && s.IsSuperset(s2)
   116  }
   117  
   118  // Len returns the size of the set.
   119  func (s IPNetSet) Len() int {
   120  	return len(s)
   121  }