From 4cf44dd1d41e0f1065172c9553d2362061a1a48b Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Thu, 11 Mar 2021 20:36:54 +0300 Subject: [PATCH] Pull request: all: rm var shadowing, vol. 2 Updates #2803. Squashed commit of the following: commit bf7186378164f19ea9e21ec832526792efa2f9c3 Author: Ainar Garipov Date: Thu Mar 11 19:48:17 2021 +0300 all: rm var shadowing, vol. 2 --- internal/dnsfilter/dnsfilter.go | 2 +- internal/dnsfilter/safebrowsing.go | 4 +++- internal/querylog/decode.go | 9 ++++++--- internal/querylog/http.go | 20 ++++++++++++-------- internal/querylog/qlogfile_test.go | 3 ++- internal/querylog/qlogreader_test.go | 8 ++++---- internal/querylog/search.go | 5 +++-- 7 files changed, 31 insertions(+), 20 deletions(-) diff --git a/internal/dnsfilter/dnsfilter.go b/internal/dnsfilter/dnsfilter.go index b82ce69c..0e05de1a 100644 --- a/internal/dnsfilter/dnsfilter.go +++ b/internal/dnsfilter/dnsfilter.go @@ -850,7 +850,7 @@ func New(c *Config, blockFilters []Filter) *DNSFilter { d.BlockedServices = bsvcs if blockFilters != nil { - err := d.initFiltering(nil, blockFilters) + err = d.initFiltering(nil, blockFilters) if err != nil { log.Error("Can't initialize filtering subsystem: %s", err) d.Close() diff --git a/internal/dnsfilter/safebrowsing.go b/internal/dnsfilter/safebrowsing.go index 9142c7c4..7b9744c4 100644 --- a/internal/dnsfilter/safebrowsing.go +++ b/internal/dnsfilter/safebrowsing.go @@ -229,7 +229,9 @@ func (c *sbCtx) processTXT(resp *dns.Msg) (bool, [][]byte) { if !matched { var hash32 [32]byte copy(hash32[:], hash) - hashHost, ok := c.hashToHost[hash32] + + var hashHost string + hashHost, ok = c.hashToHost[hash32] if ok { log.Debug("%s: matched %s by %s/%s", c.svc, c.host, hashHost, t) matched = true diff --git a/internal/querylog/decode.go b/internal/querylog/decode.go index 3e9a5f33..d0307179 100644 --- a/internal/querylog/decode.go +++ b/internal/querylog/decode.go @@ -280,7 +280,8 @@ func decodeResultRules(dec *json.Decoder, ent *logEntry) { i := 0 for { - keyToken, err := dec.Token() + var keyToken json.Token + keyToken, err = dec.Token() if err != nil { if err != io.EOF { log.Debug("decodeResultRules err: %s", err) @@ -405,7 +406,8 @@ func decodeResultDNSRewriteResult(dec *json.Decoder, ent *logEntry) { // code for a longer time than planned. switch key { case "RCode": - vToken, err := dec.Token() + var vToken json.Token + vToken, err = dec.Token() if err != nil { if err != io.EOF { log.Debug("decodeResultDNSRewriteResult err: %s", err) @@ -418,7 +420,8 @@ func decodeResultDNSRewriteResult(dec *json.Decoder, ent *logEntry) { ent.Result.DNSRewriteResult = &dnsfilter.DNSRewriteResult{} } - if n, ok := vToken.(json.Number); ok { + var n json.Number + if n, ok = vToken.(json.Number); ok { rcode64, _ := n.Int64() ent.Result.DNSRewriteResult.RCode = rules.RCode(rcode64) } diff --git a/internal/querylog/http.go b/internal/querylog/http.go index 8948f9f6..b235c2fe 100644 --- a/internal/querylog/http.go +++ b/internal/querylog/http.go @@ -150,10 +150,9 @@ func (l *queryLog) parseSearchCriteria(q url.Values, name string, ct criteriaTyp } // parseSearchParams - parses "searchParams" from the HTTP request's query string -func (l *queryLog) parseSearchParams(r *http.Request) (*searchParams, error) { - p := newSearchParams() +func (l *queryLog) parseSearchParams(r *http.Request) (p *searchParams, err error) { + p = newSearchParams() - var err error q := r.URL.Query() olderThan := q.Get("older_than") if len(olderThan) != 0 { @@ -163,11 +162,14 @@ func (l *queryLog) parseSearchParams(r *http.Request) (*searchParams, error) { } } - if limit, err := strconv.ParseInt(q.Get("limit"), 10, 64); err == nil { - p.limit = int(limit) + var limit64 int64 + if limit64, err = strconv.ParseInt(q.Get("limit"), 10, 64); err == nil { + p.limit = int(limit64) } - if offset, err := strconv.ParseInt(q.Get("offset"), 10, 64); err == nil { - p.offset = int(offset) + + var offset64 int64 + if offset64, err = strconv.ParseInt(q.Get("offset"), 10, 64); err == nil { + p.offset = int(offset64) // If we don't use "olderThan" and use offset/limit instead, we should change the default behavior // and scan all log records until we found enough log entries @@ -180,7 +182,9 @@ func (l *queryLog) parseSearchParams(r *http.Request) (*searchParams, error) { } for k, v := range paramNames { - ok, c, err := l.parseSearchCriteria(q, k, v) + var ok bool + var c searchCriteria + ok, c, err = l.parseSearchCriteria(q, k, v) if err != nil { return nil, err } diff --git a/internal/querylog/qlogfile_test.go b/internal/querylog/qlogfile_test.go index b74111fc..83615a0a 100644 --- a/internal/querylog/qlogfile_test.go +++ b/internal/querylog/qlogfile_test.go @@ -225,7 +225,8 @@ func TestQLogFile_SeekTS_bad(t *testing.T) { t.Run(tc.name, func(t *testing.T) { assert.NotEqualValues(t, 0, tc.ts) - _, depth, err := q.SeekTS(tc.ts) + var depth int + _, depth, err = q.SeekTS(tc.ts) assert.NotEmpty(t, l.num) require.NotNil(t, err) if tc.leq { diff --git a/internal/querylog/qlogreader_test.go b/internal/querylog/qlogreader_test.go index 31622866..0dce5004 100644 --- a/internal/querylog/qlogreader_test.go +++ b/internal/querylog/qlogreader_test.go @@ -112,11 +112,11 @@ func TestQLogReader_Seek(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - timestamp, err := time.Parse(time.RFC3339Nano, tc.time) + ts, err := time.Parse(time.RFC3339Nano, tc.time) require.Nil(t, err) - err = r.SeekTS(timestamp.UnixNano()) - assert.True(t, errors.Is(err, tc.want)) + err = r.SeekTS(ts.UnixNano()) + assert.True(t, errors.Is(err, tc.want), err) }) } } @@ -146,7 +146,7 @@ func TestQLogReader_ReadNext(t *testing.T) { require.Nil(t, err) for i := 1; i < tc.start; i++ { - _, err := r.ReadNext() + _, err = r.ReadNext() require.Nil(t, err) } diff --git a/internal/querylog/search.go b/internal/querylog/search.go index f23a42b6..b114c4e9 100644 --- a/internal/querylog/search.go +++ b/internal/querylog/search.go @@ -118,8 +118,9 @@ func (l *queryLog) searchFiles(params *searchParams) ([]*logEntry, time.Time, in // The idea is to make search calls faster so that the UI could handle it and show something // This behavior can be overridden if "maxFileScanEntries" is set to 0 for total < params.maxFileScanEntries || params.maxFileScanEntries <= 0 { - entry, ts, err := l.readNextEntry(r, params) - + var entry *logEntry + var ts int64 + entry, ts, err = l.readNextEntry(r, params) if err == io.EOF { // there's nothing to read anymore break