Commit 3fdf4be5 by Arve Knudsen Committed by GitHub

Chore: Enable more go-ruleguard rules (#29781)

* Chore: Enable more go-ruleguard rules

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
parent e33c7cfb
......@@ -112,16 +112,14 @@ func (b *InProcBus) DispatchCtx(ctx context.Context, msg Msg) error {
func (b *InProcBus) Dispatch(msg Msg) error {
var msgName = reflect.TypeOf(msg).Elem().Name()
var handler = b.handlersWithCtx[msgName]
withCtx := true
handler := b.handlersWithCtx[msgName]
if handler == nil {
withCtx = false
handler = b.handlers[msgName]
}
if handler == nil {
return ErrHandlerNotFound
if handler == nil {
return ErrHandlerNotFound
}
}
var params = []reflect.Value{}
......
......@@ -5,7 +5,6 @@ import (
"math"
"reflect"
"runtime"
"time"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana/pkg/expr/mathexp/parse"
......@@ -130,7 +129,7 @@ func (e *State) walkUnary(node *parse.UnaryNode) (Results, error) {
}
func (e *State) unarySeries(s Series, op string) (Series, error) {
newSeries := NewSeries(e.RefID, s.GetLabels(), s.TimeIdx, s.TimeIsNullable, s.ValueIdx, s.ValueIsNullabe, s.Len())
newSeries := NewSeries(e.RefID, s.GetLabels(), s.TimeIdx, s.TimeIsNullable, s.ValueIdx, s.ValueIsNullable, s.Len())
for i := 0; i < s.Len(); i++ {
t, f := s.GetPoint(i)
if f == nil {
......@@ -432,7 +431,7 @@ func (e *State) biScalarNumber(labels data.Labels, op string, number Number, sca
}
func (e *State) biSeriesNumber(labels data.Labels, op string, s Series, scalarVal *float64, seriesFirst bool) (Series, error) {
newSeries := NewSeries(e.RefID, labels, s.TimeIdx, s.TimeIsNullable, s.ValueIdx, s.ValueIsNullabe, s.Len())
newSeries := NewSeries(e.RefID, labels, s.TimeIdx, s.TimeIsNullable, s.ValueIdx, s.ValueIsNullable, s.Len())
var err error
for i := 0; i < s.Len(); i++ {
nF := math.NaN()
......@@ -462,18 +461,21 @@ func (e *State) biSeriesNumber(labels data.Labels, op string, s Series, scalarVa
// biSeriesSeries performs a the binary operation for each value in the two series where the times
// are equal. If there are datapoints in A or B that do not share a time, they will be dropped.
func (e *State) biSeriesSeries(labels data.Labels, op string, aSeries, bSeries Series) (Series, error) {
bPoints := make(map[time.Time]*float64)
bPoints := make(map[string]*float64)
for i := 0; i < bSeries.Len(); i++ {
t, f := bSeries.GetPoint(i)
if t != nil {
bPoints[*t] = f
bPoints[t.UTC().String()] = f
}
}
newSeries := NewSeries(e.RefID, labels, aSeries.TimeIdx, aSeries.TimeIsNullable || bSeries.TimeIsNullable, aSeries.ValueIdx, aSeries.ValueIsNullabe || bSeries.ValueIsNullabe, 0)
newSeries := NewSeries(
e.RefID, labels, aSeries.TimeIdx, aSeries.TimeIsNullable || bSeries.TimeIsNullable, aSeries.ValueIdx,
aSeries.ValueIsNullable || bSeries.ValueIsNullable, 0,
)
for aIdx := 0; aIdx < aSeries.Len(); aIdx++ {
aTime, aF := aSeries.GetPoint(aIdx)
bF, ok := bPoints[*aTime]
bF, ok := bPoints[aTime.UTC().String()]
if !ok {
continue
}
......
......@@ -95,7 +95,10 @@ func perFloat(e *State, val Value, floatF func(x float64) float64) (Value, error
newVal = NewScalar(e.RefID, &nF)
case parse.TypeSeriesSet:
resSeries := val.(Series)
newSeries := NewSeries(e.RefID, resSeries.GetLabels(), resSeries.TimeIdx, resSeries.TimeIsNullable, resSeries.ValueIdx, resSeries.ValueIsNullabe, resSeries.Len())
newSeries := NewSeries(
e.RefID, resSeries.GetLabels(), resSeries.TimeIdx, resSeries.TimeIsNullable, resSeries.ValueIdx,
resSeries.ValueIsNullable, resSeries.Len(),
)
for i := 0; i < resSeries.Len(); i++ {
t, f := resSeries.GetPoint(i)
nF := math.NaN()
......
......@@ -14,7 +14,7 @@ func (s Series) Resample(refID string, interval time.Duration, downsampler strin
if newSeriesLength <= 0 {
return s, fmt.Errorf("the series cannot be sampled further; the time range is shorter than the interval")
}
resampled := NewSeries(refID, s.GetLabels(), s.TimeIdx, s.TimeIsNullable, s.ValueIdx, s.ValueIsNullabe, newSeriesLength+1)
resampled := NewSeries(refID, s.GetLabels(), s.TimeIdx, s.TimeIsNullable, s.ValueIdx, s.ValueIsNullable, newSeriesLength+1)
bookmark := 0
var lastSeen *float64
idx := 0
......
......@@ -11,11 +11,11 @@ import (
// Series has time.Time and ...? *float64 fields.
type Series struct {
Frame *data.Frame
TimeIsNullable bool
TimeIdx int
ValueIsNullabe bool
ValueIdx int
Frame *data.Frame
TimeIsNullable bool
TimeIdx int
ValueIsNullable bool
ValueIdx int
// TODO:
// - Multiple Value Fields
// - Value can be different number types
......@@ -43,7 +43,7 @@ func SeriesFromFrame(frame *data.Frame) (s Series, err error) {
foundValue = true
s.ValueIdx = i
case data.FieldTypeNullableFloat64:
s.ValueIsNullabe = true
s.ValueIsNullable = true
foundValue = true
s.ValueIdx = i
default:
......@@ -77,11 +77,11 @@ func NewSeries(refID string, labels data.Labels, timeIdx int, nullableTime bool,
}
return Series{
Frame: data.NewFrame("", fields...),
TimeIsNullable: nullableTime,
TimeIdx: timeIdx,
ValueIsNullabe: nullableValue,
ValueIdx: valueIdx,
Frame: data.NewFrame("", fields...),
TimeIsNullable: nullableTime,
TimeIdx: timeIdx,
ValueIsNullable: nullableValue,
ValueIdx: valueIdx,
}
}
......@@ -115,7 +115,7 @@ func (s Series) SetPoint(pointIdx int, t *time.Time, f *float64) (err error) {
}
s.Frame.Fields[s.TimeIdx].Set(pointIdx, *t)
}
if s.ValueIsNullabe {
if s.ValueIsNullable {
s.Frame.Fields[s.ValueIdx].Set(pointIdx, f)
} else {
if f == nil {
......@@ -136,7 +136,7 @@ func (s Series) AppendPoint(pointIdx int, t *time.Time, f *float64) (err error)
}
s.Frame.Fields[s.TimeIdx].Append(*t)
}
if s.ValueIsNullabe {
if s.ValueIsNullable {
s.Frame.Fields[s.ValueIdx].Append(f)
} else {
if f == nil {
......@@ -163,7 +163,7 @@ func (s Series) GetTime(pointIdx int) *time.Time {
// GetValue returns the float value at the specified index.
func (s Series) GetValue(pointIdx int) *float64 {
if s.ValueIsNullabe {
if s.ValueIsNullable {
return s.Frame.Fields[s.ValueIdx].At(pointIdx).(*float64)
}
f := s.Frame.Fields[s.ValueIdx].At(pointIdx).(float64)
......
......@@ -92,10 +92,10 @@ func TestSeriesFromFrame(t *testing.T) {
data.NewField("value", nil, []float64{}),
},
},
TimeIdx: 0,
TimeIsNullable: false,
ValueIdx: 1,
ValueIsNullabe: false,
TimeIdx: 0,
TimeIsNullable: false,
ValueIdx: 1,
ValueIsNullable: false,
},
},
{
......@@ -117,10 +117,10 @@ func TestSeriesFromFrame(t *testing.T) {
data.NewField("time", nil, []*time.Time{unixTimePointer(5, 0)}),
},
},
TimeIdx: 1,
TimeIsNullable: true,
ValueIdx: 0,
ValueIsNullabe: true,
TimeIdx: 1,
TimeIsNullable: true,
ValueIdx: 0,
ValueIsNullable: true,
},
},
{
......
......@@ -35,8 +35,8 @@ func unconvert(m fluent.Matcher) {
m.Match("float32($x)").Where(m["x"].Type.Is("float32") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
m.Match("float64($x)").Where(m["x"].Type.Is("float64") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
// m.Match("byte($x)").Where(m["x"].Type.Is("byte")).Report("unnecessary conversion").Suggest("$x")
// m.Match("rune($x)").Where(m["x"].Type.Is("rune")).Report("unnecessary conversion").Suggest("$x")
m.Match("byte($x)").Where(m["x"].Type.Is("byte")).Report("unnecessary conversion").Suggest("$x")
m.Match("rune($x)").Where(m["x"].Type.Is("rune")).Report("unnecessary conversion").Suggest("$x")
m.Match("bool($x)").Where(m["x"].Type.Is("bool") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
m.Match("int8($x)").Where(m["x"].Type.Is("int8") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
......@@ -52,7 +52,6 @@ func unconvert(m fluent.Matcher) {
m.Match("time.Duration($x)").Where(m["x"].Type.Is("time.Duration") && !m["x"].Text.Matches("^[0-9]*$")).Report("unnecessary conversion").Suggest("$x")
}
/*
// Don't use == or != with time.Time
// https://github.com/dominikh/go-tools/issues/47 : Wontfix
func timeeq(m fluent.Matcher) {
......@@ -60,7 +59,6 @@ func timeeq(m fluent.Matcher) {
m.Match("$t0 != $t1").Where(m["t0"].Type.Is("time.Time")).Report("using != with time.Time")
m.Match(`map[$k]$v`).Where(m["k"].Type.Is("time.Time")).Report("map with time.Time keys are easy to misuse")
}
*/
// Wrong err in error check
func wrongerr(m fluent.Matcher) {
......@@ -225,28 +223,24 @@ func ifreturn(m fluent.Matcher) {
}
func oddifsequence(m fluent.Matcher) {
/*
m.Match("if $x { $*_ }; if $x {$*_ }").Report("odd sequence of if test")
m.Match("if $x { $*_ }; if $x {$*_ }").Report("odd sequence of if test")
m.Match("if $x == $y { $*_ }; if $y == $x {$*_ }").Report("odd sequence of if tests")
m.Match("if $x != $y { $*_ }; if $y != $x {$*_ }").Report("odd sequence of if tests")
m.Match("if $x == $y { $*_ }; if $y == $x {$*_ }").Report("odd sequence of if tests")
m.Match("if $x != $y { $*_ }; if $y != $x {$*_ }").Report("odd sequence of if tests")
m.Match("if $x < $y { $*_ }; if $y > $x {$*_ }").Report("odd sequence of if tests")
m.Match("if $x <= $y { $*_ }; if $y >= $x {$*_ }").Report("odd sequence of if tests")
m.Match("if $x < $y { $*_ }; if $y > $x {$*_ }").Report("odd sequence of if tests")
m.Match("if $x <= $y { $*_ }; if $y >= $x {$*_ }").Report("odd sequence of if tests")
m.Match("if $x > $y { $*_ }; if $y < $x {$*_ }").Report("odd sequence of if tests")
m.Match("if $x >= $y { $*_ }; if $y <= $x {$*_ }").Report("odd sequence of if tests")
*/
m.Match("if $x > $y { $*_ }; if $y < $x {$*_ }").Report("odd sequence of if tests")
m.Match("if $x >= $y { $*_ }; if $y <= $x {$*_ }").Report("odd sequence of if tests")
}
// odd sequence of nested if tests
func nestedifsequence(m fluent.Matcher) {
/*
m.Match("if $x < $y { if $x >= $y {$*_ }; $*_ }").Report("odd sequence of nested if tests")
m.Match("if $x <= $y { if $x > $y {$*_ }; $*_ }").Report("odd sequence of nested if tests")
m.Match("if $x > $y { if $x <= $y {$*_ }; $*_ }").Report("odd sequence of nested if tests")
m.Match("if $x >= $y { if $x < $y {$*_ }; $*_ }").Report("odd sequence of nested if tests")
*/
m.Match("if $x < $y { if $x >= $y {$*_ }; $*_ }").Report("odd sequence of nested if tests")
m.Match("if $x <= $y { if $x > $y {$*_ }; $*_ }").Report("odd sequence of nested if tests")
m.Match("if $x > $y { if $x <= $y {$*_ }; $*_ }").Report("odd sequence of nested if tests")
m.Match("if $x >= $y { if $x < $y {$*_ }; $*_ }").Report("odd sequence of nested if tests")
}
// odd sequence of assignments
......@@ -315,7 +309,6 @@ func floateq(m fluent.Matcher) {
m.Match("switch $x { $*_ }", "switch $*_; $x { $*_ }").
Where(m["x"].Type.Is("float64")).
Report("floating point as switch expression")
}
*/
......@@ -432,7 +425,6 @@ func nilerr(m fluent.Matcher) {
}
/*
func mailaddress(m fluent.Matcher) {
m.Match(
"fmt.Sprintf(`\"%s\" <%s>`, $NAME, $EMAIL)",
......@@ -447,7 +439,6 @@ func mailaddress(m fluent.Matcher) {
Report("use net/mail Address.String() instead of fmt.Sprintf()").
Suggest("(&mail.Address{Name:$NAME, Address:$EMAIL}).String()")
}
*/
func errnetclosed(m fluent.Matcher) {
m.Match(
......@@ -459,7 +450,6 @@ func errnetclosed(m fluent.Matcher) {
}
/*
func httpheaderadd(m fluent.Matcher) {
m.Match(
`$H.Add($KEY, $VALUE)`,
......@@ -468,7 +458,6 @@ func httpheaderadd(m fluent.Matcher) {
Report("use http.Header.Set method instead of Add to overwrite all existing header values").
Suggest(`$H.Set($KEY, $VALUE)`)
}
*/
func hmacnew(m fluent.Matcher) {
m.Match("hmac.New(func() hash.Hash { return $x }, $_)",
......
......@@ -11,6 +11,7 @@ import (
"html/template"
"io"
"net"
"net/mail"
"strconv"
"strings"
......@@ -175,10 +176,11 @@ func (ns *NotificationService) buildEmailMessage(cmd *models.SendEmailCommand) (
subject = subjectBuffer.String()
}
addr := mail.Address{Name: ns.Cfg.Smtp.FromName, Address: ns.Cfg.Smtp.FromAddress}
return &Message{
To: cmd.To,
SingleEmail: cmd.SingleEmail,
From: fmt.Sprintf("%s <%s>", ns.Cfg.Smtp.FromName, ns.Cfg.Smtp.FromAddress),
From: addr.String(),
Subject: subject,
Body: buffer.String(),
EmbeddedFiles: cmd.EmbeddedFiles,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment