Chore: Enable Go linter gocritic (#26224)

* Chore: Enable gocritic linter

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
Arve Knudsen
2020-07-16 14:39:01 +02:00
committed by GitHub
parent 317c7b269c
commit d4e4cb4c71
43 changed files with 184 additions and 171 deletions
@@ -208,7 +208,6 @@ func TestAppInsightsPluginRoutes(t *testing.T) {
})
}
}
func TestInsightsDimensionsUnmarshalJSON(t *testing.T) {
a := []byte(`"foo"`)
b := []byte(`["foo"]`)
+21 -16
View File
@@ -314,16 +314,17 @@ func reverse(s string) string {
func interpolateFilterWildcards(value string) string {
matches := strings.Count(value, "*")
if matches == 2 && strings.HasSuffix(value, "*") && strings.HasPrefix(value, "*") {
switch {
case matches == 2 && strings.HasSuffix(value, "*") && strings.HasPrefix(value, "*"):
value = strings.Replace(value, "*", "", -1)
value = fmt.Sprintf(`has_substring("%s")`, value)
} else if matches == 1 && strings.HasPrefix(value, "*") {
case matches == 1 && strings.HasPrefix(value, "*"):
value = strings.Replace(value, "*", "", 1)
value = fmt.Sprintf(`ends_with("%s")`, value)
} else if matches == 1 && strings.HasSuffix(value, "*") {
case matches == 1 && strings.HasSuffix(value, "*"):
value = reverse(strings.Replace(reverse(value), "*", "", 1))
value = fmt.Sprintf(`starts_with("%s")`, value)
} else if matches != 0 {
case matches != 0:
value = string(wildcardRegexRe.ReplaceAllFunc([]byte(value), func(in []byte) []byte {
return []byte(strings.Replace(string(in), string(in), `\\`+string(in), 1))
}))
@@ -339,19 +340,21 @@ func buildFilterString(metricType string, filterParts []string) string {
filterString := ""
for i, part := range filterParts {
mod := i % 4
if part == "AND" {
switch {
case part == "AND":
filterString += " "
} else if mod == 2 {
case mod == 2:
operator := filterParts[i-1]
if operator == "=~" || operator == "!=~" {
switch {
case operator == "=~" || operator == "!=~":
filterString = reverse(strings.Replace(reverse(filterString), "~", "", 1))
filterString += fmt.Sprintf(`monitoring.regex.full_match("%s")`, part)
} else if strings.Contains(part, "*") {
case strings.Contains(part, "*"):
filterString += interpolateFilterWildcards(part)
} else {
default:
filterString += fmt.Sprintf(`"%s"`, part)
}
} else {
default:
filterString += part
}
}
@@ -398,11 +401,12 @@ func calculateAlignmentPeriod(alignmentPeriod string, intervalMs int64, duration
if alignmentPeriod == "cloud-monitoring-auto" || alignmentPeriod == "stackdriver-auto" { // legacy
alignmentPeriodValue := int(math.Max(float64(durationSeconds), 60.0))
if alignmentPeriodValue < 60*60*23 {
switch {
case alignmentPeriodValue < 60*60*23:
alignmentPeriod = "+60s"
} else if alignmentPeriodValue < 60*60*24*6 {
case alignmentPeriodValue < 60*60*24*6:
alignmentPeriod = "+300s"
} else {
default:
alignmentPeriod = "+3600s"
}
}
@@ -735,11 +739,12 @@ func calcBucketBound(bucketOptions cloudMonitoringBucketOptions, n int) string {
return bucketBound
}
if bucketOptions.LinearBuckets != nil {
switch {
case bucketOptions.LinearBuckets != nil:
bucketBound = strconv.FormatInt(bucketOptions.LinearBuckets.Offset+(bucketOptions.LinearBuckets.Width*int64(n-1)), 10)
} else if bucketOptions.ExponentialBuckets != nil {
case bucketOptions.ExponentialBuckets != nil:
bucketBound = strconv.FormatInt(int64(bucketOptions.ExponentialBuckets.Scale*math.Pow(bucketOptions.ExponentialBuckets.GrowthFactor, float64(n-1))), 10)
} else if bucketOptions.ExplicitBuckets != nil {
case bucketOptions.ExplicitBuckets != nil:
bucketBound = fmt.Sprintf("%g", bucketOptions.ExplicitBuckets.Bounds[n])
}
return bucketBound
+3
View File
@@ -29,18 +29,21 @@ var credsCacheLock sync.RWMutex
// Session factory.
// Stubbable by tests.
//nolint:gocritic
var newSession = func(cfgs ...*aws.Config) (*session.Session, error) {
return session.NewSession(cfgs...)
}
// STS service factory.
// Stubbable by tests.
//nolint:gocritic
var newSTSService = func(p client.ConfigProvider, cfgs ...*aws.Config) stsiface.STSAPI {
return sts.New(p, cfgs...)
}
// EC2Metadata service factory.
// Stubbable by tests.
//nolint:gocritic
var newEC2Metadata = func(p client.ConfigProvider, cfgs ...*aws.Config) *ec2metadata.EC2Metadata {
return ec2metadata.New(p, cfgs...)
}
+12 -9
View File
@@ -246,11 +246,12 @@ func (rp *responseParser) processMetrics(esAgg *simplejson.Json, target *Query,
bucket := simplejson.NewFromAny(v)
key := castToNullFloat(bucket.Get("key"))
var value null.Float
if statName == "std_deviation_bounds_upper" {
switch statName {
case "std_deviation_bounds_upper":
value = castToNullFloat(bucket.GetPath(metric.ID, "std_deviation_bounds", "upper"))
} else if statName == "std_deviation_bounds_lower" {
case "std_deviation_bounds_lower":
value = castToNullFloat(bucket.GetPath(metric.ID, "std_deviation_bounds", "lower"))
} else {
default:
value = castToNullFloat(bucket.GetPath(metric.ID, statName))
}
newSeries.Points = append(newSeries.Points, tsdb.TimePoint{value, key})
@@ -349,11 +350,12 @@ func (rp *responseParser) processAggregationDocs(esAgg *simplejson.Json, aggDef
}
var value null.Float
if statName == "std_deviation_bounds_upper" {
switch statName {
case "std_deviation_bounds_upper":
value = castToNullFloat(bucket.GetPath(metric.ID, "std_deviation_bounds", "upper"))
} else if statName == "std_deviation_bounds_lower" {
case "std_deviation_bounds_lower":
value = castToNullFloat(bucket.GetPath(metric.ID, "std_deviation_bounds", "lower"))
} else {
default:
value = castToNullFloat(bucket.GetPath(metric.ID, statName))
}
@@ -568,11 +570,12 @@ func getErrorFromElasticResponse(response *es.SearchResponse) *tsdb.QueryResult
reason := json.Get("reason").MustString()
rootCauseReason := json.Get("root_cause").GetIndex(0).Get("reason").MustString()
if rootCauseReason != "" {
switch {
case rootCauseReason != "":
result.ErrorString = rootCauseReason
} else if reason != "" {
case reason != "":
result.ErrorString = reason
} else {
default:
result.ErrorString = "Unknown elasticsearch error response"
}
+1 -1
View File
@@ -57,7 +57,7 @@ func TestInfluxDB(t *testing.T) {
testBodyValues := url.Values{}
testBodyValues.Add("q", query)
testBody := testBodyValues.Encode()
So(string(body[:]), ShouldEqual, testBody)
So(string(body), ShouldEqual, testBody)
})
})
+4 -3
View File
@@ -63,11 +63,12 @@ func (query *Query) renderTags() []string {
// quote value unless regex or number
var textValue string
if tag.Operator == "=~" || tag.Operator == "!~" {
switch tag.Operator {
case "=~", "!~":
textValue = tag.Value
} else if tag.Operator == "<" || tag.Operator == ">" {
case "<", ">":
textValue = tag.Value
} else {
default:
textValue = fmt.Sprintf("'%s'", strings.Replace(tag.Value, `\`, `\\`, -1))
}
+3 -1
View File
@@ -55,6 +55,8 @@ var engineCache = engineCacheType{
var sqlIntervalCalculator = tsdb.NewIntervalCalculator(nil)
//nolint:gocritic
// NewXormEngine is an xorm.Engine factory, that can be stubbed by tests.
var NewXormEngine = func(driverName string, connectionString string) (*xorm.Engine, error) {
return xorm.NewEngine(driverName, connectionString)
}
@@ -481,7 +483,7 @@ func ConvertSqlTimeColumnToEpochMs(values tsdb.RowValues, timeIndex int) {
values[timeIndex] = float64(value.UnixNano()) / float64(time.Millisecond)
case *time.Time:
if value != nil {
values[timeIndex] = float64((*value).UnixNano()) / float64(time.Millisecond)
values[timeIndex] = float64(value.UnixNano()) / float64(time.Millisecond)
}
case int64:
values[timeIndex] = int64(tsdb.EpochPrecisionToMs(float64(value)))
+11 -18
View File
@@ -114,29 +114,22 @@ func init() {
})
registerScenario(&Scenario{
Id: "predictable_pulse",
Name: "Predictable Pulse",
Handler: func(query *tsdb.Query, context *tsdb.TsdbQuery) *tsdb.QueryResult {
return getPredictablePulse(query, context)
},
Id: "predictable_pulse",
Name: "Predictable Pulse",
Handler: getPredictablePulse,
Description: PredictablePulseDesc,
})
registerScenario(&Scenario{
Id: "predictable_csv_wave",
Name: "Predictable CSV Wave",
Handler: func(query *tsdb.Query, context *tsdb.TsdbQuery) *tsdb.QueryResult {
return getPredictableCSVWave(query, context)
},
Id: "predictable_csv_wave",
Name: "Predictable CSV Wave",
Handler: getPredictableCSVWave,
})
registerScenario(&Scenario{
Id: "random_walk_table",
Name: "Random Walk Table",
Handler: func(query *tsdb.Query, context *tsdb.TsdbQuery) *tsdb.QueryResult {
return getRandomWalkTable(query, context)
},
Id: "random_walk_table",
Name: "Random Walk Table",
Handler: getRandomWalkTable,
})
registerScenario(&Scenario{
@@ -455,7 +448,7 @@ func getPredictablePulse(query *tsdb.Query, context *tsdb.TsdbQuery) *tsdb.Query
return queryRes
}
timeStep = timeStep * 1000 // Seconds to Milliseconds
timeStep *= 1000 // Seconds to Milliseconds
onFor := func(mod int64) (null.Float, error) { // How many items in the cycle should get the on value
var i int64
for i = 0; i < onCount; i++ {
@@ -505,7 +498,7 @@ func getPredictableCSVWave(query *tsdb.Query, context *tsdb.TsdbQuery) *tsdb.Que
values[i] = val
}
timeStep = timeStep * 1000 // Seconds to Milliseconds
timeStep *= 1000 // Seconds to Milliseconds
valuesLen := int64(len(values))
getValue := func(mod int64) (null.Float, error) {
var i int64