map bool -> map struct{} (#2386)

This clear out the remaining map[x]bool usage and moves the bool to an
empty struct.

Two note worthy other changes:

* EnableChaos in the server is now also exported to make it show up in
  the documentation.
* The auto plugin is left as is, because there the boolean is
  explicitaly set to false to signal 'to-be-deleted' and the key is left
  as-is.

Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
Miek Gieben
2018-12-10 10:17:15 +00:00
committed by Yong Tang
parent c788649a00
commit 9abbf4a4a0
9 changed files with 64 additions and 63 deletions

View File

@@ -70,7 +70,7 @@ func NewServer(addr string, group []*Config) (*Server, error) {
if site.registry != nil { if site.registry != nil {
// this config is already computed with the chain of plugin // this config is already computed with the chain of plugin
// set classChaos in accordance with previously registered plugins // set classChaos in accordance with previously registered plugins
for name := range enableChaos { for name := range EnableChaos {
if _, ok := site.registry[name]; ok { if _, ok := site.registry[name]; ok {
s.classChaos = true s.classChaos = true
break break
@@ -97,7 +97,7 @@ func NewServer(addr string, group []*Config) (*Server, error) {
} }
} }
// Unblock CH class queries when any of these plugins are loaded. // Unblock CH class queries when any of these plugins are loaded.
if _, ok := enableChaos[stack.Name()]; ok { if _, ok := EnableChaos[stack.Name()]; ok {
s.classChaos = true s.classChaos = true
} }
} }
@@ -381,12 +381,11 @@ type (
loopKey struct{} // loopKey is the context key for counting self loops loopKey struct{} // loopKey is the context key for counting self loops
) )
// enableChaos is a map with plugin names for which we should open CH class queries as // EnableChaos is a map with plugin names for which we should open CH class queries as we block these by default.
// we block these by default. var EnableChaos = map[string]struct{}{
var enableChaos = map[string]bool{ "chaos": struct{}{},
"chaos": true, "forward": struct{}{},
"forward": true, "proxy": struct{}{},
"proxy": true,
} }
// Quiet mode will not show any informative output on initialization. // Quiet mode will not show any informative output on initialization.

View File

@@ -257,14 +257,14 @@ var (
) )
// flagsBlacklist removes flags with these names from our flagset. // flagsBlacklist removes flags with these names from our flagset.
var flagsBlacklist = map[string]bool{ var flagsBlacklist = map[string]struct{}{
"logtostderr": true, "logtostderr": struct{}{},
"alsologtostderr": true, "alsologtostderr": struct{}{},
"v": true, "v": struct{}{},
"stderrthreshold": true, "stderrthreshold": struct{}{},
"vmodule": true, "vmodule": struct{}{},
"log_backtrace_at": true, "log_backtrace_at": struct{}{},
"log_dir": true, "log_dir": struct{}{},
} }
var flagsToKeep []*flag.Flag var flagsToKeep []*flag.Flag

View File

@@ -129,9 +129,9 @@ func TestKubernetesXFRNotAllowed(t *testing.T) {
// difference shows what we're missing when comparing two RR slices // difference shows what we're missing when comparing two RR slices
func difference(testRRs []dns.RR, gotRRs []dns.RR) []dns.RR { func difference(testRRs []dns.RR, gotRRs []dns.RR) []dns.RR {
expectedRRs := map[string]bool{} expectedRRs := map[string]struct{}{}
for _, rr := range testRRs { for _, rr := range testRRs {
expectedRRs[rr.String()] = true expectedRRs[rr.String()] = struct{}{}
} }
foundRRs := []dns.RR{} foundRRs := []dns.RR{}

View File

@@ -55,7 +55,9 @@ func (l Logger) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
class := response.Classify(tpe) class := response.Classify(tpe)
// If we don't set up a class in config, the default "all" will be added // If we don't set up a class in config, the default "all" will be added
// and we shouldn't have an empty rule.Class. // and we shouldn't have an empty rule.Class.
if rule.Class[response.All] || rule.Class[class] { _, ok := rule.Class[response.All]
_, ok1 := rule.Class[class]
if ok || ok1 {
rep := replacer.New(ctx, r, rrw, CommonLogEmptyValue) rep := replacer.New(ctx, r, rrw, CommonLogEmptyValue)
clog.Infof(rep.Replace(rule.Format)) clog.Infof(rep.Replace(rule.Format))
} }
@@ -72,7 +74,7 @@ func (l Logger) Name() string { return "log" }
// Rule configures the logging plugin. // Rule configures the logging plugin.
type Rule struct { type Rule struct {
NameScope string NameScope string
Class map[response.Class]bool Class map[response.Class]struct{}
Format string Format string
} }

View File

@@ -21,7 +21,7 @@ func TestLoggedStatus(t *testing.T) {
rule := Rule{ rule := Rule{
NameScope: ".", NameScope: ".",
Format: DefaultLogFormat, Format: DefaultLogFormat,
Class: map[response.Class]bool{response.All: true}, Class: map[response.Class]struct{}{response.All: struct{}{}},
} }
var f bytes.Buffer var f bytes.Buffer
@@ -53,7 +53,7 @@ func TestLoggedClassDenial(t *testing.T) {
rule := Rule{ rule := Rule{
NameScope: ".", NameScope: ".",
Format: DefaultLogFormat, Format: DefaultLogFormat,
Class: map[response.Class]bool{response.Denial: true}, Class: map[response.Class]struct{}{response.Denial: struct{}{}},
} }
var f bytes.Buffer var f bytes.Buffer
@@ -82,7 +82,7 @@ func TestLoggedClassError(t *testing.T) {
rule := Rule{ rule := Rule{
NameScope: ".", NameScope: ".",
Format: DefaultLogFormat, Format: DefaultLogFormat,
Class: map[response.Class]bool{response.Error: true}, Class: map[response.Class]struct{}{response.Error: struct{}{}},
} }
var f bytes.Buffer var f bytes.Buffer

View File

@@ -40,13 +40,13 @@ func logParse(c *caddy.Controller) ([]Rule, error) {
rules = append(rules, Rule{ rules = append(rules, Rule{
NameScope: ".", NameScope: ".",
Format: DefaultLogFormat, Format: DefaultLogFormat,
Class: make(map[response.Class]bool), Class: make(map[response.Class]struct{}),
}) })
} else if len(args) == 1 { } else if len(args) == 1 {
rules = append(rules, Rule{ rules = append(rules, Rule{
NameScope: dns.Fqdn(args[0]), NameScope: dns.Fqdn(args[0]),
Format: DefaultLogFormat, Format: DefaultLogFormat,
Class: make(map[response.Class]bool), Class: make(map[response.Class]struct{}),
}) })
} else { } else {
// Name scope, and maybe a format specified // Name scope, and maybe a format specified
@@ -64,7 +64,7 @@ func logParse(c *caddy.Controller) ([]Rule, error) {
rules = append(rules, Rule{ rules = append(rules, Rule{
NameScope: dns.Fqdn(args[0]), NameScope: dns.Fqdn(args[0]),
Format: format, Format: format,
Class: make(map[response.Class]bool), Class: make(map[response.Class]struct{}),
}) })
} }
@@ -82,14 +82,14 @@ func logParse(c *caddy.Controller) ([]Rule, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
rules[len(rules)-1].Class[cls] = true rules[len(rules)-1].Class[cls] = struct{}{}
} }
default: default:
return nil, c.ArgErr() return nil, c.ArgErr()
} }
} }
if len(rules[len(rules)-1].Class) == 0 { if len(rules[len(rules)-1].Class) == 0 {
rules[len(rules)-1].Class[response.All] = true rules[len(rules)-1].Class[response.All] = struct{}{}
} }
} }

View File

@@ -18,42 +18,42 @@ func TestLogParse(t *testing.T) {
{`log`, false, []Rule{{ {`log`, false, []Rule{{
NameScope: ".", NameScope: ".",
Format: DefaultLogFormat, Format: DefaultLogFormat,
Class: map[response.Class]bool{response.All: true}, Class: map[response.Class]struct{}{response.All: struct{}{}},
}}}, }}},
{`log example.org`, false, []Rule{{ {`log example.org`, false, []Rule{{
NameScope: "example.org.", NameScope: "example.org.",
Format: DefaultLogFormat, Format: DefaultLogFormat,
Class: map[response.Class]bool{response.All: true}, Class: map[response.Class]struct{}{response.All: struct{}{}},
}}}, }}},
{`log example.org. {common}`, false, []Rule{{ {`log example.org. {common}`, false, []Rule{{
NameScope: "example.org.", NameScope: "example.org.",
Format: CommonLogFormat, Format: CommonLogFormat,
Class: map[response.Class]bool{response.All: true}, Class: map[response.Class]struct{}{response.All: struct{}{}},
}}}, }}},
{`log example.org {combined}`, false, []Rule{{ {`log example.org {combined}`, false, []Rule{{
NameScope: "example.org.", NameScope: "example.org.",
Format: CombinedLogFormat, Format: CombinedLogFormat,
Class: map[response.Class]bool{response.All: true}, Class: map[response.Class]struct{}{response.All: struct{}{}},
}}}, }}},
{`log example.org. {`log example.org.
log example.net {combined}`, false, []Rule{{ log example.net {combined}`, false, []Rule{{
NameScope: "example.org.", NameScope: "example.org.",
Format: DefaultLogFormat, Format: DefaultLogFormat,
Class: map[response.Class]bool{response.All: true}, Class: map[response.Class]struct{}{response.All: struct{}{}},
}, { }, {
NameScope: "example.net.", NameScope: "example.net.",
Format: CombinedLogFormat, Format: CombinedLogFormat,
Class: map[response.Class]bool{response.All: true}, Class: map[response.Class]struct{}{response.All: struct{}{}},
}}}, }}},
{`log example.org {host} {`log example.org {host}
log example.org {when}`, false, []Rule{{ log example.org {when}`, false, []Rule{{
NameScope: "example.org.", NameScope: "example.org.",
Format: "{host}", Format: "{host}",
Class: map[response.Class]bool{response.All: true}, Class: map[response.Class]struct{}{response.All: struct{}{}},
}, { }, {
NameScope: "example.org.", NameScope: "example.org.",
Format: "{when}", Format: "{when}",
Class: map[response.Class]bool{response.All: true}, Class: map[response.Class]struct{}{response.All: struct{}{}},
}}}, }}},
{`log example.org { {`log example.org {
@@ -61,28 +61,28 @@ func TestLogParse(t *testing.T) {
}`, false, []Rule{{ }`, false, []Rule{{
NameScope: "example.org.", NameScope: "example.org.",
Format: CommonLogFormat, Format: CommonLogFormat,
Class: map[response.Class]bool{response.All: true}, Class: map[response.Class]struct{}{response.All: struct{}{}},
}}}, }}},
{`log example.org { {`log example.org {
class denial class denial
}`, false, []Rule{{ }`, false, []Rule{{
NameScope: "example.org.", NameScope: "example.org.",
Format: CommonLogFormat, Format: CommonLogFormat,
Class: map[response.Class]bool{response.Denial: true}, Class: map[response.Class]struct{}{response.Denial: struct{}{}},
}}}, }}},
{`log { {`log {
class denial class denial
}`, false, []Rule{{ }`, false, []Rule{{
NameScope: ".", NameScope: ".",
Format: CommonLogFormat, Format: CommonLogFormat,
Class: map[response.Class]bool{response.Denial: true}, Class: map[response.Class]struct{}{response.Denial: struct{}{}},
}}}, }}},
{`log { {`log {
class denial error class denial error
}`, false, []Rule{{ }`, false, []Rule{{
NameScope: ".", NameScope: ".",
Format: CommonLogFormat, Format: CommonLogFormat,
Class: map[response.Class]bool{response.Denial: true, response.Error: true}, Class: map[response.Class]struct{}{response.Denial: struct{}{}, response.Error: struct{}{}},
}}}, }}},
{`log { {`log {
class denial class denial
@@ -90,7 +90,7 @@ func TestLogParse(t *testing.T) {
}`, false, []Rule{{ }`, false, []Rule{{
NameScope: ".", NameScope: ".",
Format: CommonLogFormat, Format: CommonLogFormat,
Class: map[response.Class]bool{response.Denial: true, response.Error: true}, Class: map[response.Class]struct{}{response.Denial: struct{}{}, response.Error: struct{}{}},
}}}, }}},
{`log { {`log {
class abracadabra class abracadabra

View File

@@ -26,7 +26,7 @@ type Metrics struct {
srv *http.Server srv *http.Server
zoneNames []string zoneNames []string
zoneMap map[string]bool zoneMap map[string]struct{}
zoneMu sync.RWMutex zoneMu sync.RWMutex
} }
@@ -35,7 +35,7 @@ func New(addr string) *Metrics {
met := &Metrics{ met := &Metrics{
Addr: addr, Addr: addr,
Reg: prometheus.NewRegistry(), Reg: prometheus.NewRegistry(),
zoneMap: make(map[string]bool), zoneMap: make(map[string]struct{}),
} }
// Add the default collectors // Add the default collectors
met.MustRegister(prometheus.NewGoCollector()) met.MustRegister(prometheus.NewGoCollector())
@@ -69,7 +69,7 @@ func (m *Metrics) MustRegister(c prometheus.Collector) {
// AddZone adds zone z to m. // AddZone adds zone z to m.
func (m *Metrics) AddZone(z string) { func (m *Metrics) AddZone(z string) {
m.zoneMu.Lock() m.zoneMu.Lock()
m.zoneMap[z] = true m.zoneMap[z] = struct{}{}
m.zoneNames = keys(m.zoneMap) m.zoneNames = keys(m.zoneMap)
m.zoneMu.Unlock() m.zoneMu.Unlock()
} }
@@ -140,7 +140,7 @@ func (m *Metrics) OnFinalShutdown() error {
return m.stopServer() return m.stopServer()
} }
func keys(m map[string]bool) []string { func keys(m map[string]struct{}) []string {
sx := []string{} sx := []string{}
for k := range m { for k := range m {
sx = append(sx, k) sx = append(sx, k)

View File

@@ -50,25 +50,25 @@ func WithServer(ctx context.Context) string {
return srv.(string) return srv.(string)
} }
var monitorType = map[uint16]bool{ var monitorType = map[uint16]struct{}{
dns.TypeAAAA: true, dns.TypeAAAA: struct{}{},
dns.TypeA: true, dns.TypeA: struct{}{},
dns.TypeCNAME: true, dns.TypeCNAME: struct{}{},
dns.TypeDNSKEY: true, dns.TypeDNSKEY: struct{}{},
dns.TypeDS: true, dns.TypeDS: struct{}{},
dns.TypeMX: true, dns.TypeMX: struct{}{},
dns.TypeNSEC3: true, dns.TypeNSEC3: struct{}{},
dns.TypeNSEC: true, dns.TypeNSEC: struct{}{},
dns.TypeNS: true, dns.TypeNS: struct{}{},
dns.TypePTR: true, dns.TypePTR: struct{}{},
dns.TypeRRSIG: true, dns.TypeRRSIG: struct{}{},
dns.TypeSOA: true, dns.TypeSOA: struct{}{},
dns.TypeSRV: true, dns.TypeSRV: struct{}{},
dns.TypeTXT: true, dns.TypeTXT: struct{}{},
// Meta Qtypes // Meta Qtypes
dns.TypeIXFR: true, dns.TypeIXFR: struct{}{},
dns.TypeAXFR: true, dns.TypeAXFR: struct{}{},
dns.TypeANY: true, dns.TypeANY: struct{}{},
} }
const other = "other" const other = "other"