Simplify plugin/pkg/fall (#1358)

* Simplify plugin/pkg/fall

* Remove unused import

* Fix fall_test

* Get fall coverage to 100% just because

* gofmt. sigh.
This commit is contained in:
John Belamaric
2018-01-07 14:51:32 -05:00
committed by GitHub
parent c6febe6250
commit c59f5f6e86
13 changed files with 117 additions and 142 deletions

View File

@@ -7,71 +7,52 @@ import (
// F can be nil to allow for no fallthrough, empty allow all zones to fallthrough or
// contain a zone list that is checked.
type F []string
// New returns a new F.
func New() *F { return new(F) }
type F struct {
Zones []string
}
// Through will check if we should fallthrough for qname. Note that we've named the
// variable in each plugin "Fall", so this then reads Fall.Through().
func (f *F) Through(qname string) bool {
if f == nil {
return false
}
if len(*f) == 0 {
return true
}
zone := plugin.Zones(*f).Matches(qname)
return zone != ""
func (f F) Through(qname string) bool {
return plugin.Zones(f.Zones).Matches(qname) != ""
}
// SetZones will set zones in f.
func (f *F) SetZones(zones []string) {
// setZones will set zones in f.
func (f *F) setZones(zones []string) {
for i := range zones {
zones[i] = plugin.Host(zones[i]).Normalize()
}
*f = zones
f.Zones = zones
}
// Example returns an F with example.org. as the zone name.
var Example = func() *F {
f := F([]string{"example.org."})
return &f
}()
// Zero returns a zero valued F.
var Zero = func() *F {
f := F([]string{})
return &f
// SetZonesFromArgs sets zones in f to the passed value or to "." if the slice is empty.
func (f *F) SetZonesFromArgs(zones []string) {
if len(zones) == 0 {
f.setZones(Root().Zones)
return
}
f.setZones(zones)
}
// IsNil returns true is f is nil.
func (f *F) IsNil() bool { return f == nil }
// IsZero returns true is f is zero (and not nil).
func (f *F) IsZero() bool {
if f == nil {
// Equal returns true if f and g are equal.
func (f F) Equal(g F) bool {
if len(f.Zones) != len(g.Zones) {
return false
}
return len(*f) == 0
}
// Equal returns true if f and g are equal. Only useful in tests, The (possible) zones
// are *not* checked.
func (f *F) Equal(g *F) bool {
if f.IsNil() {
if g.IsNil() {
return true
for i := range f.Zones {
if f.Zones[i] != g.Zones[i] {
return false
}
return false
}
if f.IsZero() {
if g.IsZero() {
return true
}
}
if len(*f) != len(*g) {
return false
}
return true
}
// Zero returns a zero valued F.
var Zero = func() F {
return F{[]string{}}
}
// Root returns F set to only ".".
var Root = func() F {
return F{[]string{"."}}
}

View File

@@ -2,41 +2,58 @@ package fall
import "testing"
func TestIsNil(t *testing.T) {
var f *F
if !f.IsNil() {
t.Errorf("F should be nil")
func TestEqual(t *testing.T) {
var z F
f := F{Zones: []string{"example.com."}}
g := F{Zones: []string{"example.net."}}
h := F{Zones: []string{"example.com."}}
if !f.Equal(h) {
t.Errorf("%v should equal %v", f, h)
}
if z.Equal(f) {
t.Errorf("%v should not be equal to %v", z, f)
}
if f.Equal(g) {
t.Errorf("%v should not be equal to %v", f, g)
}
}
func TestIsZero(t *testing.T) {
f := New()
if !f.IsZero() {
func TestZero(t *testing.T) {
var f F
if !f.Equal(Zero()) {
t.Errorf("F should be zero")
}
}
func TestFallThroughExample(t *testing.T) {
if !Example.Through("example.org.") {
t.Errorf("example.org. should fall through")
func TestSetZonesFromArgs(t *testing.T) {
var f F
f.SetZonesFromArgs([]string{})
if !f.Equal(Root()) {
t.Errorf("F should have the root zone")
}
if Example.Through("example.net.") {
t.Errorf("example.net. should not fall through")
f.SetZonesFromArgs([]string{"example.com", "example.net."})
expected := F{Zones: []string{"example.com.", "example.net."}}
if !f.Equal(expected) {
t.Errorf("F should be %v but is %v", expected, f)
}
}
func TestFallthrough(t *testing.T) {
var fall *F
var fall F
if fall.Through("foo.com.") {
t.Errorf("Expected false, got true for nil fallthrough")
t.Errorf("Expected false, got true for zero fallthrough")
}
fall = New()
fall.SetZonesFromArgs([]string{})
if !fall.Through("foo.net.") {
t.Errorf("Expected true, got false for all zone fallthrough")
}
fall.SetZones([]string{"foo.com", "bar.com"})
fall.SetZonesFromArgs([]string{"foo.com", "bar.com"})
if fall.Through("foo.net.") {
t.Errorf("Expected false, got true for non-matching fallthrough zone")