mirror of
https://github.com/gin-gonic/gin.git
synced 2025-05-28 16:19:16 +08:00
ci(golangci): add gofumpt linter and fix related issues- Added gofumpt linter to .golangci.yml
Signed-off-by: Flc <four_leaf_clover@foxmail.com>
This commit is contained in:
parent
82549da4b9
commit
4092436c56
@ -70,6 +70,7 @@ formatters:
|
|||||||
enable:
|
enable:
|
||||||
- gci
|
- gci
|
||||||
- gofmt
|
- gofmt
|
||||||
|
- gofumpt
|
||||||
- goimports
|
- goimports
|
||||||
exclusions:
|
exclusions:
|
||||||
generated: lax
|
generated: lax
|
||||||
|
@ -18,14 +18,16 @@ func TestSliceValidationError(t *testing.T) {
|
|||||||
{"has nil elements", SliceValidationError{errors.New("test error"), nil}, "[0]: test error"},
|
{"has nil elements", SliceValidationError{errors.New("test error"), nil}, "[0]: test error"},
|
||||||
{"has zero elements", SliceValidationError{}, ""},
|
{"has zero elements", SliceValidationError{}, ""},
|
||||||
{"has one element", SliceValidationError{errors.New("test one error")}, "[0]: test one error"},
|
{"has one element", SliceValidationError{errors.New("test one error")}, "[0]: test one error"},
|
||||||
{"has two elements",
|
{
|
||||||
|
"has two elements",
|
||||||
SliceValidationError{
|
SliceValidationError{
|
||||||
errors.New("first error"),
|
errors.New("first error"),
|
||||||
errors.New("second error"),
|
errors.New("second error"),
|
||||||
},
|
},
|
||||||
"[0]: first error\n[1]: second error",
|
"[0]: first error\n[1]: second error",
|
||||||
},
|
},
|
||||||
{"has many elements",
|
{
|
||||||
|
"has many elements",
|
||||||
SliceValidationError{
|
SliceValidationError{
|
||||||
errors.New("first error"),
|
errors.New("first error"),
|
||||||
errors.New("second error"),
|
errors.New("second error"),
|
||||||
|
@ -11,9 +11,11 @@ import (
|
|||||||
|
|
||||||
const defaultMemory = 32 << 20
|
const defaultMemory = 32 << 20
|
||||||
|
|
||||||
type formBinding struct{}
|
type (
|
||||||
type formPostBinding struct{}
|
formBinding struct{}
|
||||||
type formMultipartBinding struct{}
|
formPostBinding struct{}
|
||||||
|
formMultipartBinding struct{}
|
||||||
|
)
|
||||||
|
|
||||||
func (formBinding) Name() string {
|
func (formBinding) Name() string {
|
||||||
return "form"
|
return "form"
|
||||||
|
@ -17,7 +17,6 @@ func (headerBinding) Name() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (headerBinding) Bind(req *http.Request, obj any) error {
|
func (headerBinding) Bind(req *http.Request, obj any) error {
|
||||||
|
|
||||||
if err := mapHeader(obj, req.Header); err != nil {
|
if err := mapHeader(obj, req.Header); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -158,16 +158,16 @@ type structNoValidationPointer struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestValidateNoValidationPointers(t *testing.T) {
|
func TestValidateNoValidationPointers(t *testing.T) {
|
||||||
//origin := createNoValidation_values()
|
// origin := createNoValidation_values()
|
||||||
//test := createNoValidation_values()
|
// test := createNoValidation_values()
|
||||||
empty := structNoValidationPointer{}
|
empty := structNoValidationPointer{}
|
||||||
|
|
||||||
//assert.Nil(t, validate(test))
|
// assert.Nil(t, validate(test))
|
||||||
//assert.Nil(t, validate(&test))
|
// assert.Nil(t, validate(&test))
|
||||||
require.NoError(t, validate(empty))
|
require.NoError(t, validate(empty))
|
||||||
require.NoError(t, validate(&empty))
|
require.NoError(t, validate(&empty))
|
||||||
|
|
||||||
//assert.Equal(t, origin, test)
|
// assert.Equal(t, origin, test)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Object map[string]any
|
type Object map[string]any
|
||||||
@ -198,7 +198,7 @@ type structModifyValidation struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func toZero(sl validator.StructLevel) {
|
func toZero(sl validator.StructLevel) {
|
||||||
var s = sl.Top().Interface().(*structModifyValidation)
|
s := sl.Top().Interface().(*structModifyValidation)
|
||||||
s.Integer = 0
|
s.Integer = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,6 +24,7 @@ func (xmlBinding) Bind(req *http.Request, obj any) error {
|
|||||||
func (xmlBinding) BindBody(body []byte, obj any) error {
|
func (xmlBinding) BindBody(body []byte, obj any) error {
|
||||||
return decodeXML(bytes.NewReader(body), obj)
|
return decodeXML(bytes.NewReader(body), obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
func decodeXML(r io.Reader, obj any) error {
|
func decodeXML(r io.Reader, obj any) error {
|
||||||
decoder := xml.NewDecoder(r)
|
decoder := xml.NewDecoder(r)
|
||||||
if err := decoder.Decode(obj); err != nil {
|
if err := decoder.Decode(obj); err != nil {
|
||||||
|
@ -1518,7 +1518,7 @@ func TestContextNegotiationFormat(t *testing.T) {
|
|||||||
c.Request, _ = http.NewRequest(http.MethodPost, "", nil)
|
c.Request, _ = http.NewRequest(http.MethodPost, "", nil)
|
||||||
|
|
||||||
assert.Panics(t, func() { c.NegotiateFormat() })
|
assert.Panics(t, func() { c.NegotiateFormat() })
|
||||||
assert.JSONEq(t, MIMEJSON, c.NegotiateFormat(MIMEJSON, MIMEXML))
|
assert.Equal(t, MIMEJSON, c.NegotiateFormat(MIMEJSON, MIMEXML))
|
||||||
assert.Equal(t, MIMEHTML, c.NegotiateFormat(MIMEHTML, MIMEJSON))
|
assert.Equal(t, MIMEHTML, c.NegotiateFormat(MIMEHTML, MIMEJSON))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1540,7 +1540,7 @@ func TestContextNegotiationFormatWithWildcardAccept(t *testing.T) {
|
|||||||
assert.Equal(t, "*/*", c.NegotiateFormat("*/*"))
|
assert.Equal(t, "*/*", c.NegotiateFormat("*/*"))
|
||||||
assert.Equal(t, "text/*", c.NegotiateFormat("text/*"))
|
assert.Equal(t, "text/*", c.NegotiateFormat("text/*"))
|
||||||
assert.Equal(t, "application/*", c.NegotiateFormat("application/*"))
|
assert.Equal(t, "application/*", c.NegotiateFormat("application/*"))
|
||||||
assert.JSONEq(t, MIMEJSON, c.NegotiateFormat(MIMEJSON))
|
assert.Equal(t, MIMEJSON, c.NegotiateFormat(MIMEJSON))
|
||||||
assert.Equal(t, MIMEXML, c.NegotiateFormat(MIMEXML))
|
assert.Equal(t, MIMEXML, c.NegotiateFormat(MIMEXML))
|
||||||
assert.Equal(t, MIMEHTML, c.NegotiateFormat(MIMEHTML))
|
assert.Equal(t, MIMEHTML, c.NegotiateFormat(MIMEHTML))
|
||||||
|
|
||||||
@ -1564,9 +1564,9 @@ func TestContextNegotiationFormatCustom(t *testing.T) {
|
|||||||
c.Accepted = nil
|
c.Accepted = nil
|
||||||
c.SetAccepted(MIMEJSON, MIMEXML)
|
c.SetAccepted(MIMEJSON, MIMEXML)
|
||||||
|
|
||||||
assert.JSONEq(t, MIMEJSON, c.NegotiateFormat(MIMEJSON, MIMEXML))
|
assert.Equal(t, MIMEJSON, c.NegotiateFormat(MIMEJSON, MIMEXML))
|
||||||
assert.Equal(t, MIMEXML, c.NegotiateFormat(MIMEXML, MIMEHTML))
|
assert.Equal(t, MIMEXML, c.NegotiateFormat(MIMEXML, MIMEHTML))
|
||||||
assert.JSONEq(t, MIMEJSON, c.NegotiateFormat(MIMEJSON))
|
assert.Equal(t, MIMEJSON, c.NegotiateFormat(MIMEJSON))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContextNegotiationFormat2(t *testing.T) {
|
func TestContextNegotiationFormat2(t *testing.T) {
|
||||||
|
1
fs.go
1
fs.go
@ -17,7 +17,6 @@ type OnlyFilesFS struct {
|
|||||||
// Open passes `Open` to the upstream implementation without `Readdir` functionality.
|
// Open passes `Open` to the upstream implementation without `Readdir` functionality.
|
||||||
func (o OnlyFilesFS) Open(name string) (http.File, error) {
|
func (o OnlyFilesFS) Open(name string) (http.File, error) {
|
||||||
f, err := o.FileSystem.Open(name)
|
f, err := o.FileSystem.Open(name)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -12,8 +12,10 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
var once sync.Once
|
var (
|
||||||
var internalEngine *gin.Engine
|
once sync.Once
|
||||||
|
internalEngine *gin.Engine
|
||||||
|
)
|
||||||
|
|
||||||
func engine() *gin.Engine {
|
func engine() *gin.Engine {
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
|
@ -28,7 +28,6 @@ import (
|
|||||||
// params[1]=response status (custom compare status) default:"200 OK"
|
// params[1]=response status (custom compare status) default:"200 OK"
|
||||||
// params[2]=response body (custom compare content) default:"it worked"
|
// params[2]=response body (custom compare content) default:"it worked"
|
||||||
func testRequest(t *testing.T, params ...string) {
|
func testRequest(t *testing.T, params ...string) {
|
||||||
|
|
||||||
if len(params) == 0 {
|
if len(params) == 0 {
|
||||||
t.Fatal("url cannot be empty")
|
t.Fatal("url cannot be empty")
|
||||||
}
|
}
|
||||||
@ -47,12 +46,12 @@ func testRequest(t *testing.T, params ...string) {
|
|||||||
body, ioerr := io.ReadAll(resp.Body)
|
body, ioerr := io.ReadAll(resp.Body)
|
||||||
require.NoError(t, ioerr)
|
require.NoError(t, ioerr)
|
||||||
|
|
||||||
var responseStatus = "200 OK"
|
responseStatus := "200 OK"
|
||||||
if len(params) > 1 && params[1] != "" {
|
if len(params) > 1 && params[1] != "" {
|
||||||
responseStatus = params[1]
|
responseStatus = params[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
var responseBody = "it worked"
|
responseBody := "it worked"
|
||||||
if len(params) > 2 && params[2] != "" {
|
if len(params) > 2 && params[2] != "" {
|
||||||
responseBody = params[2]
|
responseBody = params[2]
|
||||||
}
|
}
|
||||||
@ -170,7 +169,7 @@ func TestRunTLS(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPusher(t *testing.T) {
|
func TestPusher(t *testing.T) {
|
||||||
var html = template.Must(template.New("https").Parse(`
|
html := template.Must(template.New("https").Parse(`
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Https Test</title>
|
<title>Https Test</title>
|
||||||
|
@ -12,8 +12,10 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var testString = "Albert Einstein: Logic will get you from A to B. Imagination will take you everywhere."
|
var (
|
||||||
var testBytes = []byte(testString)
|
testString = "Albert Einstein: Logic will get you from A to B. Imagination will take you everywhere."
|
||||||
|
testBytes = []byte(testString)
|
||||||
|
)
|
||||||
|
|
||||||
func rawBytesToStr(b []byte) string {
|
func rawBytesToStr(b []byte) string {
|
||||||
return string(b)
|
return string(b)
|
||||||
|
@ -13,7 +13,6 @@ type FileSystem struct {
|
|||||||
// Open passes `Open` to the upstream implementation and return an [fs.File].
|
// Open passes `Open` to the upstream implementation and return an [fs.File].
|
||||||
func (o FileSystem) Open(name string) (fs.File, error) {
|
func (o FileSystem) Open(name string) (fs.File, error) {
|
||||||
f, err := o.FileSystem.Open(name)
|
f, err := o.FileSystem.Open(name)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
6
mode.go
6
mode.go
@ -44,8 +44,10 @@ var DefaultWriter io.Writer = os.Stdout
|
|||||||
// DefaultErrorWriter is the default io.Writer used by Gin to debug errors
|
// DefaultErrorWriter is the default io.Writer used by Gin to debug errors
|
||||||
var DefaultErrorWriter io.Writer = os.Stderr
|
var DefaultErrorWriter io.Writer = os.Stderr
|
||||||
|
|
||||||
var ginMode int32 = debugCode
|
var (
|
||||||
var modeName atomic.Value
|
ginMode int32 = debugCode
|
||||||
|
modeName atomic.Value
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
mode := os.Getenv(EnvGinMode)
|
mode := os.Getenv(EnvGinMode)
|
||||||
|
@ -67,6 +67,7 @@ func (r HTMLDebug) Instance(name string, data any) Render {
|
|||||||
Data: data,
|
Data: data,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r HTMLDebug) loadTemplate() *template.Template {
|
func (r HTMLDebug) loadTemplate() *template.Template {
|
||||||
if r.FuncMap == nil {
|
if r.FuncMap == nil {
|
||||||
r.FuncMap = template.FuncMap{}
|
r.FuncMap = template.FuncMap{}
|
||||||
|
@ -481,7 +481,7 @@ func TestTreeDuplicatePath(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//printChildren(tree, "")
|
// printChildren(tree, "")
|
||||||
|
|
||||||
checkRequests(t, tree, testRequests{
|
checkRequests(t, tree, testRequests{
|
||||||
{"/", false, "/", nil},
|
{"/", false, "/", nil},
|
||||||
@ -532,7 +532,7 @@ func TestTreeCatchAllConflictRoot(t *testing.T) {
|
|||||||
|
|
||||||
func TestTreeCatchMaxParams(t *testing.T) {
|
func TestTreeCatchMaxParams(t *testing.T) {
|
||||||
tree := &node{}
|
tree := &node{}
|
||||||
var route = "/cmd/*filepath"
|
route := "/cmd/*filepath"
|
||||||
tree.addRoute(route, fakeHandler(route))
|
tree.addRoute(route, fakeHandler(route))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -692,7 +692,7 @@ func TestTreeRootTrailingSlashRedirect(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRedirectTrailingSlash(t *testing.T) {
|
func TestRedirectTrailingSlash(t *testing.T) {
|
||||||
var data = []struct {
|
data := []struct {
|
||||||
path string
|
path string
|
||||||
}{
|
}{
|
||||||
{"/hello/:name"},
|
{"/hello/:name"},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user