add golangci feature to guarantee codes quality (#2229)

This commit is contained in:
houseme
2022-11-01 20:12:21 +08:00
committed by GitHub
parent 8e0e87877a
commit 1793bf0863
255 changed files with 1123 additions and 690 deletions

View File

@ -104,7 +104,7 @@ func BeDecodeToString(b []byte) string {
}
func BeEncodeBool(b bool) []byte {
if b == true {
if b {
return []byte{1}
} else {
return []byte{0}
@ -221,7 +221,7 @@ func BeDecodeToBool(b []byte) bool {
if len(b) == 0 {
return false
}
if bytes.Compare(b, make([]byte, len(b))) == 0 {
if bytes.Equal(b, make([]byte, len(b))) {
return false
}
return true

View File

@ -104,7 +104,7 @@ func LeDecodeToString(b []byte) string {
}
func LeEncodeBool(b bool) []byte {
if b == true {
if b {
return []byte{1}
} else {
return []byte{0}
@ -221,7 +221,7 @@ func LeDecodeToBool(b []byte) bool {
if len(b) == 0 {
return false
}
if bytes.Compare(b, make([]byte, len(b))) == 0 {
if bytes.Equal(b, make([]byte, len(b))) {
return false
}
return true

View File

@ -112,7 +112,7 @@ func doZipPathWriter(fileOrFolderPath string, exclude string, zipWriter *zip.Wri
headerPrefix = gfile.Basename(fileOrFolderPath)
}
}
headerPrefix = strings.Replace(headerPrefix, "//", "/", -1)
headerPrefix = strings.ReplaceAll(headerPrefix, "//", "/")
for _, file := range files {
if exclude == file {
intlog.Printf(context.TODO(), `exclude file path: %s`, file)
@ -229,7 +229,7 @@ func zipFile(filePath string, prefix string, zw *zip.Writer) error {
file, err := os.Open(filePath)
if err != nil {
err = gerror.Wrapf(err, `os.Open failed for name "%s"`, filePath)
return nil
return err
}
defer file.Close()
@ -272,7 +272,7 @@ func createFileHeader(info os.FileInfo, prefix string) (*zip.FileHeader, error)
}
if len(prefix) > 0 {
prefix = strings.Replace(prefix, `\`, `/`, -1)
prefix = strings.ReplaceAll(prefix, `\`, `/`)
prefix = strings.TrimRight(prefix, `/`)
header.Name = prefix + `/` + header.Name
}

View File

@ -8,7 +8,7 @@ package ghash
// AP implements the classic AP hash algorithm for 32 bits.
func AP(str []byte) uint32 {
var hash uint32 = 0
var hash uint32
for i := 0; i < len(str); i++ {
if (i & 1) == 0 {
hash ^= (hash << 7) ^ uint32(str[i]) ^ (hash >> 3)
@ -21,7 +21,7 @@ func AP(str []byte) uint32 {
// AP64 implements the classic AP hash algorithm for 64 bits.
func AP64(str []byte) uint64 {
var hash uint64 = 0
var hash uint64
for i := 0; i < len(str); i++ {
if (i & 1) == 0 {
hash ^= (hash << 7) ^ uint64(str[i]) ^ (hash >> 3)

View File

@ -8,8 +8,10 @@ package ghash
// ELF implements the classic ELF hash algorithm for 32 bits.
func ELF(str []byte) uint32 {
var hash uint32 = 0
var x uint32 = 0
var (
hash uint32
x uint32
)
for i := 0; i < len(str); i++ {
hash = (hash << 4) + uint32(str[i])
if x = hash & 0xF0000000; x != 0 {
@ -23,8 +25,8 @@ func ELF(str []byte) uint32 {
// ELF64 implements the classic ELF hash algorithm for 64 bits.
func ELF64(str []byte) uint64 {
var (
hash uint64 = 0
x uint64 = 0
hash uint64
x uint64
)
for i := 0; i < len(str); i++ {
hash = (hash << 4) + uint64(str[i])

View File

@ -9,12 +9,12 @@ package ghash
// PJW implements the classic PJW hash algorithm for 32 bits.
func PJW(str []byte) uint32 {
var (
BitsInUnsignedInt uint32 = 4 * 8
ThreeQuarters uint32 = (BitsInUnsignedInt * 3) / 4
OneEighth uint32 = BitsInUnsignedInt / 8
BitsInUnsignedInt uint32 = 32 // 4 * 8
ThreeQuarters = (BitsInUnsignedInt * 3) / 4
OneEighth = BitsInUnsignedInt / 8
HighBits uint32 = (0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth)
hash uint32 = 0
test uint32 = 0
hash uint32
test uint32
)
for i := 0; i < len(str); i++ {
hash = (hash << OneEighth) + uint32(str[i])
@ -28,12 +28,12 @@ func PJW(str []byte) uint32 {
// PJW64 implements the classic PJW hash algorithm for 64 bits.
func PJW64(str []byte) uint64 {
var (
BitsInUnsignedInt uint64 = 4 * 8
ThreeQuarters uint64 = (BitsInUnsignedInt * 3) / 4
OneEighth uint64 = BitsInUnsignedInt / 8
BitsInUnsignedInt uint64 = 32 // 4 * 8
ThreeQuarters = (BitsInUnsignedInt * 3) / 4
OneEighth = BitsInUnsignedInt / 8
HighBits uint64 = (0xFFFFFFFFFFFFFFFF) << (BitsInUnsignedInt - OneEighth)
hash uint64 = 0
test uint64 = 0
hash uint64
test uint64
)
for i := 0; i < len(str); i++ {
hash = (hash << OneEighth) + uint64(str[i])

View File

@ -8,7 +8,7 @@ package ghash
// SDBM implements the classic SDBM hash algorithm for 32 bits.
func SDBM(str []byte) uint32 {
var hash uint32 = 0
var hash uint32
for i := 0; i < len(str); i++ {
// equivalent to: hash = 65599*hash + uint32(str[i]);
hash = uint32(str[i]) + (hash << 6) + (hash << 16) - hash
@ -18,7 +18,7 @@ func SDBM(str []byte) uint32 {
// SDBM64 implements the classic SDBM hash algorithm for 64 bits.
func SDBM64(str []byte) uint64 {
var hash uint64 = 0
var hash uint64
for i := 0; i < len(str); i++ {
// equivalent to: hash = 65599*hash + uint32(str[i])
hash = uint64(str[i]) + (hash << 6) + (hash << 16) - hash

View File

@ -61,7 +61,7 @@ func Decode(data []byte) (res map[string]interface{}, err error) {
fieldMap = make(map[string]interface{})
}
haveSection = true
} else if haveSection == false {
} else if !haveSection {
continue
}
@ -72,7 +72,7 @@ func Decode(data []byte) (res map[string]interface{}, err error) {
}
}
if haveSection == false {
if !haveSection {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "failed to parse INI file, section not found")
}
return res, nil

View File

@ -8,6 +8,7 @@ package gjson_test
import (
"fmt"
"github.com/gogf/gf/v2/encoding/gjson"
)

View File

@ -2,6 +2,7 @@ package gjson_test
import (
"fmt"
"github.com/gogf/gf/v2/encoding/gjson"
)
@ -104,10 +105,10 @@ func ExampleJson_ToJsonIndent() {
fmt.Println(string(jsonBytes))
// Output:
//{
// {
// "Age": 18,
// "Name": "John"
//}
// }
}
func ExampleJson_ToJsonIndentString() {
@ -126,10 +127,10 @@ func ExampleJson_ToJsonIndentString() {
fmt.Println(jsonStr)
// Output:
//{
// {
// "Age": 18,
// "Name": "John"
//}
// }
}
func ExampleJson_MustToJson() {
@ -186,10 +187,10 @@ func ExampleJson_MustToJsonIndent() {
fmt.Println(string(jsonBytes))
// Output:
//{
// {
// "Age": 18,
// "Name": "John"
//}
// }
}
func ExampleJson_MustToJsonIndentString() {
@ -208,10 +209,10 @@ func ExampleJson_MustToJsonIndentString() {
fmt.Println(jsonStr)
// Output:
//{
// {
// "Age": 18,
// "Name": "John"
//}
// }
}
// ========================================================================
@ -271,10 +272,10 @@ func ExampleJson_ToXmlIndent() {
fmt.Println(string(xmlBytes))
// Output:
//<doc>
// <doc>
// <Age>18</Age>
// <Name>John</Name>
//</doc>
// </doc>
}
func ExampleJson_ToXmlIndentString() {
@ -293,10 +294,10 @@ func ExampleJson_ToXmlIndentString() {
fmt.Println(string(xmlStr))
// Output:
//<doc>
// <doc>
// <Age>18</Age>
// <Name>John</Name>
//</doc>
// </doc>
}
func ExampleJson_MustToXml() {
@ -353,10 +354,10 @@ func ExampleJson_MustToXmlIndent() {
fmt.Println(string(xmlBytes))
// Output:
//<doc>
// <doc>
// <Age>18</Age>
// <Name>John</Name>
//</doc>
// </doc>
}
func ExampleJson_MustToXmlIndentString() {
@ -375,10 +376,10 @@ func ExampleJson_MustToXmlIndentString() {
fmt.Println(string(xmlStr))
// Output:
//<doc>
// <doc>
// <Age>18</Age>
// <Name>John</Name>
//</doc>
// </doc>
}
// ========================================================================
@ -400,8 +401,8 @@ func ExampleJson_ToYaml() {
fmt.Println(string(YamlBytes))
// Output:
//Age: 18
//Name: John
// Age: 18
// Name: John
}
func ExampleJson_ToYamlString() {
@ -420,8 +421,8 @@ func ExampleJson_ToYamlString() {
fmt.Println(string(YamlStr))
// Output:
//Age: 18
//Name: John
// Age: 18
// Name: John
}
func ExampleJson_ToYamlIndent() {
@ -440,8 +441,8 @@ func ExampleJson_ToYamlIndent() {
fmt.Println(string(YamlBytes))
// Output:
//Age: 18
//Name: John
// Age: 18
// Name: John
}
func ExampleJson_MustToYaml() {
@ -460,8 +461,8 @@ func ExampleJson_MustToYaml() {
fmt.Println(string(YamlBytes))
// Output:
//Age: 18
//Name: John
// Age: 18
// Name: John
}
func ExampleJson_MustToYamlString() {
@ -480,8 +481,8 @@ func ExampleJson_MustToYamlString() {
fmt.Println(string(YamlStr))
// Output:
//Age: 18
//Name: John
// Age: 18
// Name: John
}
// ========================================================================
@ -503,8 +504,8 @@ func ExampleJson_ToToml() {
fmt.Println(string(TomlBytes))
// Output:
//Age = 18
//Name = "John"
// Age = 18
// Name = "John"
}
func ExampleJson_ToTomlString() {
@ -523,8 +524,8 @@ func ExampleJson_ToTomlString() {
fmt.Println(string(TomlStr))
// Output:
//Age = 18
//Name = "John"
// Age = 18
// Name = "John"
}
func ExampleJson_MustToToml() {
@ -543,8 +544,8 @@ func ExampleJson_MustToToml() {
fmt.Println(string(TomlBytes))
// Output:
//Age = 18
//Name = "John"
// Age = 18
// Name = "John"
}
func ExampleJson_MustToTomlString() {
@ -563,8 +564,8 @@ func ExampleJson_MustToTomlString() {
fmt.Println(string(TomlStr))
// Output:
//Age = 18
//Name = "John"
// Age = 18
// Name = "John"
}
// ========================================================================
@ -586,8 +587,8 @@ func ExampleJson_ToIni() {
fmt.Println(string(IniBytes))
// May Output:
//Name=John
//Age=18
// Name=John
// Age=18
}
func ExampleJson_ToIniString() {
@ -604,7 +605,7 @@ func ExampleJson_ToIniString() {
fmt.Println(string(IniStr))
// Output:
//Name=John
// Name=John
}
func ExampleJson_MustToIni() {
@ -621,7 +622,7 @@ func ExampleJson_MustToIni() {
fmt.Println(string(IniBytes))
// Output:
//Name=John
// Name=John
}
func ExampleJson_MustToIniString() {
@ -638,7 +639,7 @@ func ExampleJson_MustToIniString() {
fmt.Println(string(IniStr))
// Output:
//Name=John
// Name=John
}
// ========================================================================
@ -1180,8 +1181,8 @@ func ExampleJson_Dump() {
j.Dump()
// May Output:
//{
// {
// "name": "john",
// "age": "18",
//}
// }
}

View File

@ -12,10 +12,11 @@ import (
"sort"
"strings"
"github.com/magiconair/properties"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/util/gconv"
"github.com/magiconair/properties"
)
// Decode converts properties format to map.

View File

@ -28,6 +28,7 @@ sql.mysql.0.type = mysql
sql.mysql.0.ip = 127.0.0.1
sql.mysql.0.user = root
`
var errorTests = []struct {
input, msg string
}{
@ -75,6 +76,7 @@ func TestDecode(t *testing.T) {
}
})
}
func TestEncode(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
m := make(map[string]interface{})

View File

@ -11,8 +11,8 @@ import (
"bytes"
"github.com/BurntSushi/toml"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/json"
)

View File

@ -33,14 +33,14 @@ func Decode(str string) (string, error) {
// URL-encode according to RFC 3986.
// See http://php.net/manual/en/function.rawurlencode.php.
func RawEncode(str string) string {
return strings.Replace(url.QueryEscape(str), "+", "%20", -1)
return strings.ReplaceAll(url.QueryEscape(str), "+", "%20")
}
// RawDecode does decode the given string
// Decode URL-encoded strings.
// See http://php.net/manual/en/function.rawurldecode.php.
func RawDecode(str string) (string, error) {
return url.QueryUnescape(strings.Replace(str, "%20", "+", -1))
return url.QueryUnescape(strings.ReplaceAll(str, "%20", "+"))
}
// BuildQuery Generate URL-encoded query string.

View File

@ -11,9 +11,9 @@ import (
"strings"
"github.com/clbanning/mxj/v2"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/encoding/gcharset"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gregex"
)
@ -77,9 +77,9 @@ func ToJson(content []byte) ([]byte, error) {
}
mv, err := mxj.NewMapXml(res)
if err == nil {
err = gerror.Wrapf(err, `mxj.NewMapXml failed`)
return mv.Json()
}
err = gerror.Wrap(err, `mxj.NewMapXml failed`)
return nil, err
}