Merge pull request #4513 from withshubh/develop

improve code quality
This commit is contained in:
Ming Deng 2021-03-03 19:11:21 +08:00 committed by GitHub
commit 877c565a9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 26 additions and 29 deletions

View File

@ -23,9 +23,10 @@
- Fix 4451: support QueryExecutor interface. [4461](https://github.com/beego/beego/pull/4461)
- Add some testing scripts [4461](https://github.com/beego/beego/pull/4461)
- Refactor httplib: Move debug code to a filter [4440](https://github.com/beego/beego/issues/4440)
- fix: code quality issues [4513](https://github.com/beego/beego/pull/4513)
## Fix Sonar
- [4473](https://github.com/beego/beego/pull/4473)
- [4474](https://github.com/beego/beego/pull/4474)
- [4479](https://github.com/beego/beego/pull/4479)
- [4479](https://github.com/beego/beego/pull/4479)

View File

@ -15,6 +15,7 @@
package httplib
import (
"bytes"
"errors"
"io/ioutil"
"net"
@ -66,7 +67,7 @@ func TestDoRequest(t *testing.T) {
}
func TestGet(t *testing.T) {
req := Get(getUrl)
b, err := req.Bytes()
if err != nil {
@ -222,7 +223,7 @@ func TestWithSetting(t *testing.T) {
}
func TestToJson(t *testing.T) {
req := Get(ipUrl)
resp, err := req.Response()
if err != nil {
@ -261,7 +262,7 @@ func TestToFile(t *testing.T) {
}
defer os.Remove(f)
b, err := ioutil.ReadFile(f)
if n := strings.Index(string(b), "origin"); n == -1 {
if n := bytes.Index(b, []byte("origin")); n == -1 {
t.Fatal(err)
}
}
@ -275,7 +276,7 @@ func TestToFileDir(t *testing.T) {
}
defer os.RemoveAll("./files")
b, err := ioutil.ReadFile(f)
if n := strings.Index(string(b), "origin"); n == -1 {
if n := bytes.Index(b, []byte("origin")); n == -1 {
t.Fatal(err)
}
}

View File

@ -195,7 +195,7 @@ func snakeStringWithAcronym(s string) string {
}
data = append(data, d)
}
return strings.ToLower(string(data[:]))
return strings.ToLower(string(data))
}
// snake string, XxYy to xx_yy , XxYY to xx_y_y
@ -213,7 +213,7 @@ func snakeString(s string) string {
}
data = append(data, d)
}
return strings.ToLower(string(data[:]))
return strings.ToLower(string(data))
}
// SetNameStrategy set different name strategy
@ -241,7 +241,7 @@ func camelString(s string) string {
}
data = append(data, d)
}
return string(data[:])
return string(data)
}
type argString []string

View File

@ -42,7 +42,7 @@ func (mi *MemoryItem) isExpire() bool {
if mi.lifespan == 0 {
return false
}
return time.Now().Sub(mi.createdTime) > mi.lifespan
return time.Since(mi.createdTime) > mi.lifespan
}
// MemoryCache is a memory cache adapter.
@ -66,7 +66,7 @@ func (bc *MemoryCache) Get(ctx context.Context, key string) (interface{}, error)
bc.RLock()
defer bc.RUnlock()
if itm, ok :=
bc.items[key]; ok {
bc.items[key]; ok {
if itm.isExpire() {
return nil, ErrKeyExpired
}

View File

@ -15,6 +15,7 @@
package httplib
import (
"bytes"
"context"
"errors"
"io/ioutil"
@ -259,7 +260,7 @@ func TestToFile(t *testing.T) {
}
defer os.Remove(f)
b, err := ioutil.ReadFile(f)
if n := strings.Index(string(b), "origin"); n == -1 {
if n := bytes.Index(b, []byte("origin")); n == -1 {
t.Fatal(err)
}
}
@ -273,7 +274,7 @@ func TestToFileDir(t *testing.T) {
}
defer os.RemoveAll("./files")
b, err := ioutil.ReadFile(f)
if n := strings.Index(string(b), "origin"); n == -1 {
if n := bytes.Index(b, []byte("origin")); n == -1 {
t.Fatal(err)
}
}

View File

@ -85,7 +85,7 @@ func (builder *FilterChainBuilder) report(ctx context.Context, inv *orm.Invocati
}
func (builder *FilterChainBuilder) reportTxn(ctx context.Context, inv *orm.Invocation) {
dur := time.Now().Sub(inv.TxStartTime) / time.Millisecond
dur := time.Since(inv.TxStartTime) / time.Millisecond
summaryVec.WithLabelValues(inv.Method, inv.TxName,
strconv.FormatBool(inv.InsideTx), inv.TxName).Observe(float64(dur))
}

View File

@ -41,7 +41,7 @@ func NewLog(out io.Writer) *Log {
func debugLogQueies(alias *alias, operaton, query string, t time.Time, err error, args ...interface{}) {
var logMap = make(map[string]interface{})
sub := time.Now().Sub(t) / 1e5
sub := time.Since(t) / 1e5
elsp := float64(int(sub)) / 10.0
logMap["cost_time"] = elsp
flag := " OK"

View File

@ -228,7 +228,7 @@ func snakeStringWithAcronym(s string) string {
}
data = append(data, d)
}
return strings.ToLower(string(data[:]))
return strings.ToLower(string(data))
}
// snake string, XxYy to xx_yy , XxYY to xx_y_y
@ -246,7 +246,7 @@ func snakeString(s string) string {
}
data = append(data, d)
}
return strings.ToLower(string(data[:]))
return strings.ToLower(string(data))
}
// SetNameStrategy set different name strategy
@ -274,7 +274,7 @@ func camelString(s string) string {
}
data = append(data, d)
}
return string(data[:])
return string(data)
}
type argString []string

View File

@ -108,7 +108,7 @@ func printGC(memStats *runtime.MemStats, gcstats *debug.GCStats, w io.Writer) {
if gcstats.NumGC > 0 {
lastPause := gcstats.Pause[0]
elapsed := time.Now().Sub(startTime)
elapsed := time.Since(startTime)
overhead := float64(gcstats.PauseTotal) / float64(elapsed) * 100
allocatedRate := float64(memStats.TotalAlloc) / elapsed.Seconds()
@ -125,7 +125,7 @@ func printGC(memStats *runtime.MemStats, gcstats *debug.GCStats, w io.Writer) {
utils.ToShortTimeFormat(gcstats.PauseQuantiles[99]))
} else {
// while GC has disabled
elapsed := time.Now().Sub(startTime)
elapsed := time.Since(startTime)
allocatedRate := float64(memStats.TotalAlloc) / elapsed.Seconds()
fmt.Fprintf(w, "Alloc:%s Sys:%s Alloc(Rate):%s/s\n",

View File

@ -143,7 +143,7 @@ func (o *Options) PreflightHeader(origin, rMethod, rHeaders string) (headers map
rHeader = strings.TrimSpace(rHeader)
lookupLoop:
for _, allowedHeader := range o.AllowHeaders {
if strings.ToLower(rHeader) == strings.ToLower(allowedHeader) {
if strings.EqualFold(rHeader, allowedHeader) {
allowed = append(allowed, rHeader)
break lookupLoop
}

View File

@ -211,9 +211,7 @@ func (fp *FileProvider) SessionGC(context.Context) {
// it walks save path to count files.
func (fp *FileProvider) SessionAll(context.Context) int {
a := &activeSession{}
err := filepath.Walk(fp.savePath, func(path string, f os.FileInfo, err error) error {
return a.visit(path, f, err)
})
err := filepath.Walk(fp.savePath, a.visit)
if err != nil {
SLogger.Printf("filepath.Walk() returned %v\n", err)
return 0

View File

@ -202,9 +202,7 @@ func BuildTemplate(dir string, files ...string) error {
root: dir,
files: make(map[string][]string),
}
err = Walk(fs, dir, func(path string, f os.FileInfo, err error) error {
return self.visit(path, f, err)
})
err = Walk(fs, dir, self.visit)
if err != nil {
fmt.Printf("Walk() returned %v\n", err)
return err

View File

@ -287,9 +287,7 @@ func _filePath(dir, name string) string {
}
func assetFS() *assetfs.AssetFS {
assetInfo := func(path string) (os.FileInfo, error) {
return os.Stat(path)
}
assetInfo := os.Stat
for k := range _bintree.Children {
return &assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: assetInfo, Prefix: k}
}