Skip to content
Myr

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

31 files+96 added−203 removed

modified:.github/workflows/gin.yml+3 added-3 removed
modified:.github/workflows/goreleaser.yml+1 added-1 removed
deleted: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.45//go:build !go1.186// +build !go1.1878package binding910type any = interface{}
modified:binding/binding_msgpack_test.go-1 removed
1 unmodified line
2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45//go:build !nomsgpack6// +build !nomsgpack67package binding89import (
48 unmodified lines
modified:binding/binding_nomsgpack.go-1 removed
1 unmodified line
2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45//go:build nomsgpack6// +build nomsgpack67package binding89import "net/http"
106 unmodified lines
modified:binding/binding.go-1 removed
1 unmodified line
2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45//go:build !nomsgpack6// +build !nomsgpack67package binding89import "net/http"
112 unmodified lines
modified:binding/json.go+1 added-1 removed
13 unmodified lines
14)1516// 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.19var EnableDecoderUseNumber = false2021// EnableDecoderDisallowUnknownFields is used to call the DisallowUnknownFields method22// on the JSON Decoder instance. DisallowUnknownFields causes the Decoder to
34 unmodified lines
modified:binding/msgpack_test.go-1 removed
1 unmodified line
2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45//go:build !nomsgpack6// +build !nomsgpack67package binding89import (
25 unmodified lines
modified:binding/msgpack.go-1 removed
1 unmodified line
2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45//go:build !nomsgpack6// +build !nomsgpack67package binding89import (
28 unmodified lines
modified:docs/doc.md+4 added-4 removed
423 unmodified lines
424 // By default gin.DefaultWriter = os.Stdout425 r.Use(gin.Logger())426427 // 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 lines
995```go996func main() {997 r := gin.Default()998999 // 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 lines
1960func (customerBinding) Name() string {1961 return "form"1962}19631964func (customerBinding) Bind(req *http.Request, obj interface{}) error {1964func (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 lines
1975 }1976 return validate(obj)1977}19781979func validate(obj interface{}) error {1979func validate(obj any) error {1980 if binding.Validator == nil {1981 return nil1982 }1983 return binding.Validator.ValidateStruct(obj)
260 unmodified lines
modified:internal/json/go_json.go-1 removed
1 unmodified line
2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45//go:build go_json6// +build go_json67package json89import json "github.com/goccy/go-json"
13 unmodified lines
modified:internal/json/json.go-3 removed
1 unmodified line
2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45//go:build !jsoniter && !go_json && !(sonic && avx && (linux || windows || darwin) && amd64)6// +build !jsoniter7// +build !go_json8// +build !sonic !avx !linux,!windows,!darwin !amd6467package json89import "encoding/json"
13 unmodified lines
modified:internal/json/jsoniter.go-1 removed
1 unmodified line
2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45//go:build jsoniter6// +build jsoniter67package json89import jsoniter "github.com/json-iterator/go"
14 unmodified lines
modified:internal/json/sonic.go-4 removed
1 unmodified line
2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45//go:build sonic && avx && (linux || windows || darwin) && amd646// +build sonic7// +build avx8// +build linux windows darwin9// +build amd6467package json89import "github.com/bytedance/sonic"
14 unmodified lines
deleted: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.45//go:build !go1.186// +build !go1.1878package render910type any = interface{}
modified:render/msgpack.go-1 removed
1 unmodified line
2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45//go:build !nomsgpack6// +build !nomsgpack67package render89import (
34 unmodified lines
modified:render/render_msgpack_test.go-1 removed
1 unmodified line
2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45//go:build !nomsgpack6// +build !nomsgpack67package render89import (
34 unmodified lines
deleted: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.45//go:build !go1.186// +build !go1.1878package protoexample910type 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.45//go:build !go1.186// +build !go1.1878package gin910type 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.45//go:build !go1.176// +build !go1.1778package gin910import (11 "bytes"12 "mime/multipart"13 "net/http"14 "net/http/httptest"15 "testing"1617 "github.com/stretchr/testify/assert"18)1920func 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.45//go:build go1.176// +build go1.1778package gin910import (11 "bytes"12 "mime/multipart"13 "net/http"14 "net/http/httptest"15 "runtime"16 "strings"17 "testing"1819 "github.com/stretchr/testify/assert"20)2122type interceptedWriter struct {23 ResponseWriter24 b *bytes.Buffer25}2627func (i interceptedWriter) WriteHeader(code int) {28 i.Header().Del("X-Test")29 i.ResponseWriter.WriteHeader(code)30}3132func 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}5455func TestInterceptedHeader(t *testing.T) {56 w := httptest.NewRecorder()57 c, r := CreateTestContext(w)5859 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}8384func 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.45//go:build !go1.1967package gin89import (10 "bytes"11 "mime/multipart"12 "net/http"13 "net/http/httptest"14 "testing"1516 "github.com/stretchr/testify/assert"17)1819func 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 line
2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45//go:build go1.196// +build go1.1967package gin89import (
21 unmodified lines
modified:context_appengine.go-1 removed
1 unmodified line
2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45//go:build appengine6// +build appengine67package gin89func init() {
2 unmodified lines
modified:context_test.go+40 added-1 removed
35 unmodified lines
3637// 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)4243func createMultipartRequest() *http.Request {44 boundary := "--testboundary"
2328 unmodified lines
23732374 assert.Equal(t, http.StatusOK, w.Code)2375 assert.Equal(t, "hello gin", w.Body.String())2376}23772378type interceptedWriter struct {2379 ResponseWriter2380 b *bytes.Buffer2381}23822383func (i interceptedWriter) WriteHeader(code int) {2384 i.Header().Del("X-Test")2385 i.ResponseWriter.WriteHeader(code)2386}23872388func TestInterceptedHeader(t *testing.T) {2389 w := httptest.NewRecorder()2390 c, r := CreateTestContext(w)23912392 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 lines
651 return c.MustBindWith(obj, binding.YAML)652}653654// BindTOML is a shortcut for c.MustBindWith(obj, binding.TOML).655func (c *Context) BindTOML(obj interface{}) error {655func (c *Context) BindTOML(obj any) error {656 return c.MustBindWith(obj, binding.TOML)657}658659// BindHeader is a shortcut for c.MustBindWith(obj, binding.Header).
56 unmodified lines
716 return c.ShouldBindWith(obj, binding.YAML)717}718719// ShouldBindTOML is a shortcut for c.ShouldBindWith(obj, binding.TOML).720func (c *Context) ShouldBindTOML(obj interface{}) error {720func (c *Context) ShouldBindTOML(obj any) error {721 return c.ShouldBindWith(obj, binding.TOML)722}723724// ShouldBindHeader is a shortcut for c.ShouldBindWith(obj, binding.Header).
269 unmodified lines
994 c.Render(code, render.YAML{Data: obj})995}996997// TOML serializes the given struct as TOML into the response body.998func (c *Context) TOML(code int, obj interface{}) {998func (c *Context) TOML(code int, obj any) {999 c.Render(code, render.TOML{Data: obj})1000}10011002// ProtoBuf serializes the given struct as ProtoBuf into the response body.
218 unmodified lines
modified:debug_test.go+2 added-2 removed
19 unmodified lines
20)2122// TODO23// func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {24// func debugPrint(format string, values ...interface{}) {24// func debugPrint(format string, values ...any) {2526func TestIsDebugging(t *testing.T) {27 SetMode(DebugMode)28 assert.True(t, IsDebugging())
74 unmodified lines
103 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 lines
modified:debug.go+2 added-2 removed
10 unmodified lines
11 "strconv"12 "strings"13)1415const ginSupportMinGoVer = 1615const ginSupportMinGoVer = 181617// IsDebugging returns true if the framework is running in debug mode.18// Use SetMode(gin.ReleaseMode) to disable debug mode.19func IsDebugging() bool {
46 unmodified lines
66}6768func 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+.7172`)73 }74 debugPrint(`[WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
27 unmodified lines
modified:deprecated.go+1 added-1 removed
11 unmodified lines
1213// BindWith binds the passed struct pointer using the specified binding engine.14// See the binding package.15func (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 line
modified:README.md+1 added-1 removed
29 unmodified lines
30## Getting started3132### Prerequisites3334- **[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).3536### Getting Gin3738With [Go module](https://github.com/golang/go/wiki/Modules) support, simply add the following import
141 unmodified lines
modified:utils.go+1 added-1 removed
48 unmodified lines
49 h.ServeHTTP(c.Writer, c.Request)50 }51}5253// H is a shortcut for map[string]interface{}53// H is a shortcut for map[string]any54type H map[string]any5556// MarshalXML allows type H to be used with xml.Marshal.57func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
107 unmodified lines