chore: support min go version 1.18 (#3511)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* chore: min go version 1.18 * fix build tag error * remove build tag * fix word * remove any.go * replace interface{} instead of any
authored by thinkerou · committed by GitHub · parent 943e93c · Browse files · 31 files·+96 added−203 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
31 files+96 added−203 removed
- other:+96 added−203 removed
Provenance
- Commit
1e1f0b1- Tree
c2f233c- Author
thinkerou <thinkerou@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
943e93c
31 files changed+96 added−203 removed
modified:.github/workflows/gin.yml+3 added-3 removed
16 unmodified lines171819202121222324254 unmodified lines3031323334343536373833 unmodified lines7273747576767778798011 unmodified lines16 unmodified lines steps: - name: Setup go uses: actions/setup-go@v3 with: go-version: '^1.16' go-version: '^1.18' - name: Checkout repository uses: actions/checkout@v3 - name: Setup golangci-lint uses: golangci/golangci-lint-action@v3.4.04 unmodified lines needs: lint strategy: matrix: os: [ubuntu-latest, macos-latest] go: ['1.16', '1.17', '1.18', '1.19', '1.20'] go: ['1.18', '1.19', '1.20'] test-tags: ['', '-tags nomsgpack', '-tags "sonic avx"', '-tags go_json'] include: - os: ubuntu-latest go-build: ~/.cache/go-build33 unmodified lines with: flags: ${{ matrix.os }},go-${{ matrix.go }},${{ matrix.test-tags }}
- name: Format if: matrix.go-version == '1.19.x' if: matrix.go-version == '1.20.x' run: diff -u <(echo -n) <(gofmt -d .) notification-gitter: needs: test runs-on: ubuntu-latest11 unmodified linesmodified:.github/workflows/goreleaser.yml+1 added-1 removed
19 unmodified lines202122232424252627286 unmodified lines19 unmodified lines - name: Set up Go uses: actions/setup-go@v3 with: go-version: 1.17 go-version: 1.20 - name: Run GoReleaser uses: goreleaser/goreleaser-action@v4 with:6 unmodified linesdeleted:binding/any.go-10 removed
1-// Copyright 2022 Gin Core Team. All rights reserved.2-// Use of this source code is governed by a MIT style3-// license that can be found in the LICENSE file.4-5-//go:build !go1.186-// +build !go1.187-8-package binding9-10-type any = interface{}modified:binding/binding_msgpack_test.go-1 removed
1 unmodified line2 // Use of this source code is governed by a MIT style3 // license that can be found in the LICENSE file.4 5 //go:build !nomsgpack6-// +build !nomsgpack6 7 package binding8 9 import (48 unmodified linesmodified:binding/binding_nomsgpack.go-1 removed
1 unmodified line2 // Use of this source code is governed by a MIT style3 // license that can be found in the LICENSE file.4 5 //go:build nomsgpack6-// +build nomsgpack6 7 package binding8 9 import "net/http"106 unmodified linesmodified:binding/binding.go-1 removed
1 unmodified line2 // Use of this source code is governed by a MIT style3 // license that can be found in the LICENSE file.4 5 //go:build !nomsgpack6-// +build !nomsgpack6 7 package binding8 9 import "net/http"112 unmodified linesmodified:binding/json.go+1 added-1 removed
13 unmodified lines14 )15 16 // EnableDecoderUseNumber is used to call the UseNumber method on the JSON17 // Decoder instance. UseNumber causes the Decoder to unmarshal a number into an18-// interface{} as a Number instead of as a float64.18+// any as a Number instead of as a float64.19 var EnableDecoderUseNumber = false20 21 // EnableDecoderDisallowUnknownFields is used to call the DisallowUnknownFields method22 // on the JSON Decoder instance. DisallowUnknownFields causes the Decoder to34 unmodified linesmodified:binding/msgpack_test.go-1 removed
1 unmodified line2 // Use of this source code is governed by a MIT style3 // license that can be found in the LICENSE file.4 5 //go:build !nomsgpack6-// +build !nomsgpack6 7 package binding8 9 import (25 unmodified linesmodified:binding/msgpack.go-1 removed
1 unmodified line2 // Use of this source code is governed by a MIT style3 // license that can be found in the LICENSE file.4 5 //go:build !nomsgpack6-// +build !nomsgpack6 7 package binding8 9 import (28 unmodified linesmodified:docs/doc.md+4 added-4 removed
423 unmodified lines424 // By default gin.DefaultWriter = os.Stdout425 r.Use(gin.Logger())426 427 // Recovery middleware recovers from any panics and writes a 500 if there was one.428- r.Use(gin.CustomRecovery(func(c *gin.Context, recovered interface{}) {428+ r.Use(gin.CustomRecovery(func(c *gin.Context, recovered any) {429 if err, ok := recovered.(string); ok {430 c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))431 }432 c.AbortWithStatus(http.StatusInternalServerError)562 unmodified lines995 ```go996 func main() {997 r := gin.Default()998 999- // gin.H is a shortcut for map[string]interface{}999+ // gin.H is a shortcut for map[string]any1000 r.GET("/someJSON", func(c *gin.Context) {1001 c.JSON(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})1002 })1003 956 unmodified lines1960 func (customerBinding) Name() string {1961 return "form"1962 }1963 1964-func (customerBinding) Bind(req *http.Request, obj interface{}) error {1964+func (customerBinding) Bind(req *http.Request, obj any) error {1965 if err := req.ParseForm(); err != nil {1966 return err1967 }1968 if err := req.ParseMultipartForm(defaultMemory); err != nil {6 unmodified lines1975 }1976 return validate(obj)1977 }1978 1979-func validate(obj interface{}) error {1979+func validate(obj any) error {1980 if binding.Validator == nil {1981 return nil1982 }1983 return binding.Validator.ValidateStruct(obj)260 unmodified linesmodified:internal/json/go_json.go-1 removed
1 unmodified line2 // Use of this source code is governed by a MIT style3 // license that can be found in the LICENSE file.4 5 //go:build go_json6-// +build go_json6 7 package json8 9 import json "github.com/goccy/go-json"13 unmodified linesmodified:internal/json/json.go-3 removed
1 unmodified line2 // Use of this source code is governed by a MIT style3 // license that can be found in the LICENSE file.4 5 //go:build !jsoniter && !go_json && !(sonic && avx && (linux || windows || darwin) && amd64)6-// +build !jsoniter7-// +build !go_json8-// +build !sonic !avx !linux,!windows,!darwin !amd646 7 package json8 9 import "encoding/json"13 unmodified linesmodified:internal/json/jsoniter.go-1 removed
1 unmodified line2 // Use of this source code is governed by a MIT style3 // license that can be found in the LICENSE file.4 5 //go:build jsoniter6-// +build jsoniter6 7 package json8 9 import jsoniter "github.com/json-iterator/go"14 unmodified linesmodified:internal/json/sonic.go-4 removed
1 unmodified line2 // Use of this source code is governed by a MIT style3 // license that can be found in the LICENSE file.4 5 //go:build sonic && avx && (linux || windows || darwin) && amd646-// +build sonic7-// +build avx8-// +build linux windows darwin9-// +build amd646 7 package json8 9 import "github.com/bytedance/sonic"14 unmodified linesdeleted:render/any.go-10 removed
1-// Copyright 2021 Gin Core Team. All rights reserved.2-// Use of this source code is governed by a MIT style3-// license that can be found in the LICENSE file.4-5-//go:build !go1.186-// +build !go1.187-8-package render9-10-type any = interface{}modified:render/msgpack.go-1 removed
1 unmodified line2 // Use of this source code is governed by a MIT style3 // license that can be found in the LICENSE file.4 5 //go:build !nomsgpack6-// +build !nomsgpack6 7 package render8 9 import (34 unmodified linesmodified:render/render_msgpack_test.go-1 removed
1 unmodified line2 // Use of this source code is governed by a MIT style3 // license that can be found in the LICENSE file.4 5 //go:build !nomsgpack6-// +build !nomsgpack6 7 package render8 9 import (34 unmodified linesdeleted:testdata/protoexample/any.go-10 removed
1-// Copyright 2021 Gin Core Team. All rights reserved.2-// Use of this source code is governed by a MIT style3-// license that can be found in the LICENSE file.4-5-//go:build !go1.186-// +build !go1.187-8-package protoexample9-10-type any = interface{}deleted:any.go-10 removed
1-// Copyright 2022 Gin Core Team. All rights reserved.2-// Use of this source code is governed by a MIT style3-// license that can be found in the LICENSE file.4-5-//go:build !go1.186-// +build !go1.187-8-package gin9-10-type any = interface{}deleted:context_1.16_test.go-31 removed
1-// Copyright 2021 Gin Core Team. All rights reserved.2-// Use of this source code is governed by a MIT style3-// license that can be found in the LICENSE file.4-5-//go:build !go1.176-// +build !go1.177-8-package gin9-10-import (11- "bytes"12- "mime/multipart"13- "net/http"14- "net/http/httptest"15- "testing"16-17- "github.com/stretchr/testify/assert"18-)19-20-func TestContextFormFileFailed16(t *testing.T) {21- buf := new(bytes.Buffer)22- mw := multipart.NewWriter(buf)23- mw.Close()24- c, _ := CreateTestContext(httptest.NewRecorder())25- c.Request, _ = http.NewRequest("POST", "/", nil)26- c.Request.Header.Set("Content-Type", mw.FormDataContentType())27- c.engine.MaxMultipartMemory = 8 << 2028- f, err := c.FormFile("file")29- assert.Error(t, err)30- assert.Nil(t, f)31-}deleted:context_1.17_test.go-94 removed
1-// Copyright 2021 Gin Core Team. All rights reserved.2-// Use of this source code is governed by a MIT style3-// license that can be found in the LICENSE file.4-5-//go:build go1.176-// +build go1.177-8-package gin9-10-import (11- "bytes"12- "mime/multipart"13- "net/http"14- "net/http/httptest"15- "runtime"16- "strings"17- "testing"18-19- "github.com/stretchr/testify/assert"20-)21-22-type interceptedWriter struct {23- ResponseWriter24- b *bytes.Buffer25-}26-27-func (i interceptedWriter) WriteHeader(code int) {28- i.Header().Del("X-Test")29- i.ResponseWriter.WriteHeader(code)30-}31-32-func TestContextFormFileFailed17(t *testing.T) {33- if !isGo117OrGo118() {34- return35- }36- buf := new(bytes.Buffer)37- mw := multipart.NewWriter(buf)38- defer func(mw *multipart.Writer) {39- err := mw.Close()40- if err != nil {41- assert.Error(t, err)42- }43- }(mw)44- c, _ := CreateTestContext(httptest.NewRecorder())45- c.Request, _ = http.NewRequest("POST", "/", nil)46- c.Request.Header.Set("Content-Type", mw.FormDataContentType())47- c.engine.MaxMultipartMemory = 8 << 2048- assert.Panics(t, func() {49- f, err := c.FormFile("file")50- assert.Error(t, err)51- assert.Nil(t, f)52- })53-}54-55-func TestInterceptedHeader(t *testing.T) {56- w := httptest.NewRecorder()57- c, r := CreateTestContext(w)58-59- r.Use(func(c *Context) {60- i := interceptedWriter{61- ResponseWriter: c.Writer,62- b: bytes.NewBuffer(nil),63- }64- c.Writer = i65- c.Next()66- c.Header("X-Test", "overridden")67- c.Writer = i.ResponseWriter68- })69- r.GET("/", func(c *Context) {70- c.Header("X-Test", "original")71- c.Header("X-Test-2", "present")72- c.String(http.StatusOK, "hello world")73- })74- c.Request = httptest.NewRequest("GET", "/", nil)75- r.HandleContext(c)76- // Result() has headers frozen when WriteHeaderNow() has been called77- // Compared to this time, this is when the response headers will be flushed78- // As response is flushed on c.String, the Header cannot be set by the first79- // middleware. Assert this80- assert.Equal(t, "", w.Result().Header.Get("X-Test"))81- assert.Equal(t, "present", w.Result().Header.Get("X-Test-2"))82-}83-84-func isGo117OrGo118() bool {85- version := strings.Split(runtime.Version()[2:], ".")86- if len(version) >= 2 {87- x := version[0]88- y := version[1]89- if x == "1" && (y == "17" || y == "18") {90- return true91- }92- }93- return false94-}added:context_1.18_test.go+37 added
1+// Copyright 2021 Gin Core Team. All rights reserved.2+// Use of this source code is governed by a MIT style3+// license that can be found in the LICENSE file.4+5+//go:build !go1.196+7+package gin8+9+import (10+ "bytes"11+ "mime/multipart"12+ "net/http"13+ "net/http/httptest"14+ "testing"15+16+ "github.com/stretchr/testify/assert"17+)18+19+func TestContextFormFileFailed18(t *testing.T) {20+ buf := new(bytes.Buffer)21+ mw := multipart.NewWriter(buf)22+ defer func(mw *multipart.Writer) {23+ err := mw.Close()24+ if err != nil {25+ assert.Error(t, err)26+ }27+ }(mw)28+ c, _ := CreateTestContext(httptest.NewRecorder())29+ c.Request, _ = http.NewRequest("POST", "/", nil)30+ c.Request.Header.Set("Content-Type", mw.FormDataContentType())31+ c.engine.MaxMultipartMemory = 8 << 2032+ assert.Panics(t, func() {33+ f, err := c.FormFile("file")34+ assert.Error(t, err)35+ assert.Nil(t, f)36+ })37+}modified:context_1.19_test.go-1 removed
1 unmodified line2 // Use of this source code is governed by a MIT style3 // license that can be found in the LICENSE file.4 5 //go:build go1.196-// +build go1.196 7 package gin8 9 import (21 unmodified linesmodified:context_appengine.go-1 removed
1 unmodified line2 // Use of this source code is governed by a MIT style3 // license that can be found in the LICENSE file.4 5 //go:build appengine6-// +build appengine6 7 package gin8 9 func init() {2 unmodified linesmodified:context_test.go+40 added-1 removed
35 unmodified lines36 37 // Unit tests TODO38 // func (c *Context) File(filepath string) {39 // func (c *Context) Negotiate(code int, config Negotiate) {40-// BAD case: func (c *Context) Render(code int, render render.Render, obj ...interface{}) {40+// BAD case: func (c *Context) Render(code int, render render.Render, obj ...any) {41 // test that information is not leaked when reusing Contexts (using the Pool)42 43 func createMultipartRequest() *http.Request {44 boundary := "--testboundary"2328 unmodified lines2373 2374 assert.Equal(t, http.StatusOK, w.Code)2375 assert.Equal(t, "hello gin", w.Body.String())2376 }2377+2378+type interceptedWriter struct {2379+ ResponseWriter2380+ b *bytes.Buffer2381+}2382+2383+func (i interceptedWriter) WriteHeader(code int) {2384+ i.Header().Del("X-Test")2385+ i.ResponseWriter.WriteHeader(code)2386+}2387+2388+func TestInterceptedHeader(t *testing.T) {2389+ w := httptest.NewRecorder()2390+ c, r := CreateTestContext(w)2391+2392+ r.Use(func(c *Context) {2393+ i := interceptedWriter{2394+ ResponseWriter: c.Writer,2395+ b: bytes.NewBuffer(nil),2396+ }2397+ c.Writer = i2398+ c.Next()2399+ c.Header("X-Test", "overridden")2400+ c.Writer = i.ResponseWriter2401+ })2402+ r.GET("/", func(c *Context) {2403+ c.Header("X-Test", "original")2404+ c.Header("X-Test-2", "present")2405+ c.String(http.StatusOK, "hello world")2406+ })2407+ c.Request = httptest.NewRequest("GET", "/", nil)2408+ r.HandleContext(c)2409+ // Result() has headers frozen when WriteHeaderNow() has been called2410+ // Compared to this time, this is when the response headers will be flushed2411+ // As response is flushed on c.String, the Header cannot be set by the first2412+ // middleware. Assert this2413+ assert.Equal(t, "", w.Result().Header.Get("X-Test"))2414+ assert.Equal(t, "present", w.Result().Header.Get("X-Test-2"))2415+}modified:context.go+3 added-3 removed
650 unmodified lines651 return c.MustBindWith(obj, binding.YAML)652 }653 654 // BindTOML is a shortcut for c.MustBindWith(obj, binding.TOML).655-func (c *Context) BindTOML(obj interface{}) error {655+func (c *Context) BindTOML(obj any) error {656 return c.MustBindWith(obj, binding.TOML)657 }658 659 // BindHeader is a shortcut for c.MustBindWith(obj, binding.Header).56 unmodified lines716 return c.ShouldBindWith(obj, binding.YAML)717 }718 719 // ShouldBindTOML is a shortcut for c.ShouldBindWith(obj, binding.TOML).720-func (c *Context) ShouldBindTOML(obj interface{}) error {720+func (c *Context) ShouldBindTOML(obj any) error {721 return c.ShouldBindWith(obj, binding.TOML)722 }723 724 // ShouldBindHeader is a shortcut for c.ShouldBindWith(obj, binding.Header).269 unmodified lines994 c.Render(code, render.YAML{Data: obj})995 }996 997 // TOML serializes the given struct as TOML into the response body.998-func (c *Context) TOML(code int, obj interface{}) {998+func (c *Context) TOML(code int, obj any) {999 c.Render(code, render.TOML{Data: obj})1000 }1001 1002 // ProtoBuf serializes the given struct as ProtoBuf into the response body.218 unmodified linesmodified:debug_test.go+2 added-2 removed
19 unmodified lines20 )21 22 // TODO23 // func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {24-// func debugPrint(format string, values ...interface{}) {24+// func debugPrint(format string, values ...any) {25 26 func TestIsDebugging(t *testing.T) {27 SetMode(DebugMode)28 assert.True(t, IsDebugging())74 unmodified lines103 SetMode(TestMode)104 })105 m, e := getMinVer(runtime.Version())106 if e == nil && m < ginSupportMinGoVer {107- assert.Equal(t, "[GIN-debug] [WARNING] Now Gin requires Go 1.16+.\n\n[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)107+ assert.Equal(t, "[GIN-debug] [WARNING] Now Gin requires Go 1.18+.\n\n[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)108 } else {109 assert.Equal(t, "[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)110 }111 }55 unmodified linesmodified:debug.go+2 added-2 removed
10 unmodified lines11 "strconv"12 "strings"13 )14 15-const ginSupportMinGoVer = 1615+const ginSupportMinGoVer = 1816 17 // IsDebugging returns true if the framework is running in debug mode.18 // Use SetMode(gin.ReleaseMode) to disable debug mode.19 func IsDebugging() bool {46 unmodified lines66 }67 68 func debugPrintWARNINGDefault() {69 if v, e := getMinVer(runtime.Version()); e == nil && v < ginSupportMinGoVer {70- debugPrint(`[WARNING] Now Gin requires Go 1.16+.70+ debugPrint(`[WARNING] Now Gin requires Go 1.18+.71 72 `)73 }74 debugPrint(`[WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.27 unmodified linesmodified:deprecated.go+1 added-1 removed
11 unmodified lines12 13 // BindWith binds the passed struct pointer using the specified binding engine.14 // See the binding package.15 func (c *Context) BindWith(obj any, b binding.Binding) error {16- log.Println(`BindWith(\"interface{}, binding.Binding\") error is going to16+ log.Println(`BindWith(\"any, binding.Binding\") error is going to17 be deprecated, please check issue #662 and either use MustBindWith() if you18 want HTTP 400 to be automatically returned if any error occur, or use19 ShouldBindWith() if you need to manage the error.`)20 return c.MustBindWith(obj, b)1 unmodified linemodified:README.md+1 added-1 removed
29 unmodified lines30 ## Getting started31 32 ### Prerequisites33 34-- **[Go](https://go.dev/)**: ~~any one of the **three latest major** [releases](https://go.dev/doc/devel/release)~~ (now version **1.16+** is required).34+- **[Go](https://go.dev/)**: any one of the **three latest major** [releases](https://go.dev/doc/devel/release) (we test it with these).35 36 ### Getting Gin37 38 With [Go module](https://github.com/golang/go/wiki/Modules) support, simply add the following import141 unmodified linesmodified:utils.go+1 added-1 removed
48 unmodified lines49 h.ServeHTTP(c.Writer, c.Request)50 }51 }52 53-// H is a shortcut for map[string]interface{}53+// H is a shortcut for map[string]any54 type H map[string]any55 56 // MarshalXML allows type H to be used with xml.Marshal.57 func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error {107 unmodified lines