Skip to content
This repository has been archived by the owner on May 25, 2023. It is now read-only.

Make IPSetBuilder setters return errors, add Must variants. #177

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions inlining_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ func TestInlining(t *testing.T) {
return nil
})
for _, want := range []string{
"(*IPSetBuilder).Add",
"(*IPSetBuilder).Clone",
"(*IPSetBuilder).IPSet",
"(*IPSetBuilder).Remove",
"(*IPSetBuilder).RemoveRange",
"(*IPSetBuilder).MustAdd",
"(*IPSetBuilder).MustAddPrefix",
"(*IPSetBuilder).MustAddRange",
"(*IPSetBuilder).MustRemove",
"(*IPSetBuilder).MustRemovePrefix",
"(*IPSetBuilder).MustRemoveRange",
"(*IPSet).Ranges",
"(*uint128).halves",
"IP.BitLen",
Expand Down
81 changes: 69 additions & 12 deletions ipset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

package netaddr

import "sort"
import (
"errors"
"fmt"
"sort"
)

// IPSetBuilder builds an immutable IPSet.
//
Expand Down Expand Up @@ -158,43 +162,72 @@ func (s *IPSetBuilder) Clone() *IPSetBuilder {
}

// Add adds ip to s.
func (s *IPSetBuilder) Add(ip IP) { s.AddRange(IPRange{ip, ip}) }
func (s *IPSetBuilder) Add(ip IP) error {
if ip.IsZero() {
return errors.New("cannot add zero IP to IPSetBuilder")
}
if ip.Zone() != "" {
return errors.New("cannot add IP with zone to IPSetBuilder")
}
return s.AddRange(IPRangeFrom(ip, ip))
}

// AddPrefix adds all IPs in p to s.
func (s *IPSetBuilder) AddPrefix(p IPPrefix) { s.AddRange(p.Range()) }
func (s *IPSetBuilder) AddPrefix(p IPPrefix) error {
if !p.Valid() {
return fmt.Errorf("invalid IPPrefix %q", p)
}
return s.AddRange(p.Range())
}

// AddRange adds r to s.
// If r is not Valid, AddRange does nothing.
func (s *IPSetBuilder) AddRange(r IPRange) {
func (s *IPSetBuilder) AddRange(r IPRange) error {
if !r.Valid() {
return
return fmt.Errorf("invalid IPRange %q", r)
}
// If there are any removals (s.out), then we need to compact the set
// first to get the order right.
if len(s.out) > 0 {
s.normalize()
}
s.in = append(s.in, r)
return nil
}

// AddSet adds all IPs in b to s.
func (s *IPSetBuilder) AddSet(b *IPSet) {
for _, r := range b.rr {
s.AddRange(r)
s.MustAddRange(r)
}
}

// Remove removes ip from s.
func (s *IPSetBuilder) Remove(ip IP) { s.RemoveRange(IPRange{ip, ip}) }
func (s *IPSetBuilder) Remove(ip IP) error {
if ip.IsZero() {
return errors.New("cannot remove zero IP from IPSetBuilder")
}
if ip.Zone() != "" {
return errors.New("cannot remove IP with zone from IPSetBuilder")
}
return s.RemoveRange(IPRangeFrom(ip, ip))
}

// RemovePrefix removes all IPs in p from s.
func (s *IPSetBuilder) RemovePrefix(p IPPrefix) { s.RemoveRange(p.Range()) }
func (s *IPSetBuilder) RemovePrefix(p IPPrefix) error {
if !p.Valid() {
return fmt.Errorf("invalid IPPrefix %q", p)
}
return s.RemoveRange(p.Range())
}

// RemoveRange removes all IPs in r from s.
func (s *IPSetBuilder) RemoveRange(r IPRange) {
if r.Valid() {
s.out = append(s.out, r)
func (s *IPSetBuilder) RemoveRange(r IPRange) error {
if !r.Valid() {
return fmt.Errorf("invalid IPRange %q", r)
}
s.out = append(s.out, r)
return nil
}

// RemoveSet removes all IPs in o from s.
Expand All @@ -204,6 +237,30 @@ func (s *IPSetBuilder) RemoveSet(b *IPSet) {
}
}

func panicErr(err error) {
if err != nil {
panic(err)
}
}

// MustAdd adds ip to s. Panics if IP is the zero value.
func (s *IPSetBuilder) MustAdd(ip IP) { panicErr(s.Add(ip)) }

// MustAddPrefix adds all IPs in p to s. Panics if p.Valid is false.
func (s *IPSetBuilder) MustAddPrefix(p IPPrefix) { panicErr(s.AddPrefix(p)) }

// MustAddRange adds all IPs in r to s. Panics if r.Valid is false.
func (s *IPSetBuilder) MustAddRange(r IPRange) { panicErr(s.AddRange(r)) }

// MustRemove removes ip from s. Panics if IP is the zero value.
func (s *IPSetBuilder) MustRemove(ip IP) { panicErr(s.Remove(ip)) }

// MustRemovePrefix removes all IPs in p from s. Panics if p.Valid is false.
func (s *IPSetBuilder) MustRemovePrefix(p IPPrefix) { panicErr(s.RemovePrefix(p)) }

// MustRemoveRange removes all IPs in r from s. Panics if r.Valid is false.
func (s *IPSetBuilder) MustRemoveRange(r IPRange) { panicErr(s.RemoveRange(r)) }

// removeBuilder removes all IPs in b from s.
func (s *IPSetBuilder) removeBuilder(b *IPSetBuilder) {
b.normalize()
Expand Down Expand Up @@ -377,6 +434,6 @@ func (s *IPSet) RemoveFreePrefix(bitLen uint8) (p IPPrefix, newSet *IPSet, ok bo

var b IPSetBuilder
b.AddSet(s)
b.RemovePrefix(prefix)
b.MustRemovePrefix(prefix)
return prefix, b.IPSet(), true
}
Loading