Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: support min go version 1.18 #3511

Merged
merged 6 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/gin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- 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
Expand All @@ -31,7 +31,7 @@ jobs:
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
Expand Down Expand Up @@ -73,7 +73,7 @@ jobs:
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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Gin is a web framework written in [Go](https://go.dev/). It features a martini-l

### Prerequisites

- **[Go](https://go.dev/)**: ~~any one of the **three latest major** [releases](https://go.dev/doc/devel/release)~~ (now version **1.16+** is required).
- **[Go](https://go.dev/)**: any one of the **three latest major** [releases](https://go.dev/doc/devel/release) (we test it with these).

### Getting Gin

Expand Down
10 changes: 0 additions & 10 deletions any.go

This file was deleted.

10 changes: 0 additions & 10 deletions binding/any.go

This file was deleted.

1 change: 0 additions & 1 deletion binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// license that can be found in the LICENSE file.

//go:build !nomsgpack
// +build !nomsgpack

package binding

Expand Down
1 change: 0 additions & 1 deletion binding/binding_msgpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// license that can be found in the LICENSE file.

//go:build !nomsgpack
// +build !nomsgpack

package binding

Expand Down
1 change: 0 additions & 1 deletion binding/binding_nomsgpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// license that can be found in the LICENSE file.

//go:build nomsgpack
// +build nomsgpack

package binding

Expand Down
2 changes: 1 addition & 1 deletion binding/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// EnableDecoderUseNumber is used to call the UseNumber method on the JSON
// Decoder instance. UseNumber causes the Decoder to unmarshal a number into an
// interface{} as a Number instead of as a float64.
// any as a Number instead of as a float64.
var EnableDecoderUseNumber = false

// EnableDecoderDisallowUnknownFields is used to call the DisallowUnknownFields method
Expand Down
1 change: 0 additions & 1 deletion binding/msgpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// license that can be found in the LICENSE file.

//go:build !nomsgpack
// +build !nomsgpack

package binding

Expand Down
1 change: 0 additions & 1 deletion binding/msgpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// license that can be found in the LICENSE file.

//go:build !nomsgpack
// +build !nomsgpack

package binding

Expand Down
6 changes: 3 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ func (c *Context) BindYAML(obj any) error {
}

// BindTOML is a shortcut for c.MustBindWith(obj, binding.TOML).
func (c *Context) BindTOML(obj interface{}) error {
func (c *Context) BindTOML(obj any) error {
return c.MustBindWith(obj, binding.TOML)
}

Expand Down Expand Up @@ -717,7 +717,7 @@ func (c *Context) ShouldBindYAML(obj any) error {
}

// ShouldBindTOML is a shortcut for c.ShouldBindWith(obj, binding.TOML).
func (c *Context) ShouldBindTOML(obj interface{}) error {
func (c *Context) ShouldBindTOML(obj any) error {
return c.ShouldBindWith(obj, binding.TOML)
}

Expand Down Expand Up @@ -995,7 +995,7 @@ func (c *Context) YAML(code int, obj any) {
}

// TOML serializes the given struct as TOML into the response body.
func (c *Context) TOML(code int, obj interface{}) {
func (c *Context) TOML(code int, obj any) {
c.Render(code, render.TOML{Data: obj})
}

Expand Down
94 changes: 0 additions & 94 deletions context_1.17_test.go

This file was deleted.

20 changes: 13 additions & 7 deletions context_1.16_test.go → context_1.18_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

//go:build !go1.17
// +build !go1.17
//go:build !go1.19

package gin

Expand All @@ -17,15 +16,22 @@ import (
"github.com/stretchr/testify/assert"
)

func TestContextFormFileFailed16(t *testing.T) {
func TestContextFormFileFailed18(t *testing.T) {
buf := new(bytes.Buffer)
mw := multipart.NewWriter(buf)
mw.Close()
defer func(mw *multipart.Writer) {
err := mw.Close()
if err != nil {
assert.Error(t, err)
}
}(mw)
c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("POST", "/", nil)
c.Request.Header.Set("Content-Type", mw.FormDataContentType())
c.engine.MaxMultipartMemory = 8 << 20
f, err := c.FormFile("file")
assert.Error(t, err)
assert.Nil(t, f)
assert.Panics(t, func() {
f, err := c.FormFile("file")
assert.Error(t, err)
assert.Nil(t, f)
})
}
1 change: 0 additions & 1 deletion context_1.19_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// license that can be found in the LICENSE file.

//go:build go1.19
// +build go1.19

package gin

Expand Down
1 change: 0 additions & 1 deletion context_appengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// license that can be found in the LICENSE file.

//go:build appengine
// +build appengine

package gin

Expand Down
41 changes: 40 additions & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var errTestRender = errors.New("TestRender")
// Unit tests TODO
// func (c *Context) File(filepath string) {
// func (c *Context) Negotiate(code int, config Negotiate) {
// BAD case: func (c *Context) Render(code int, render render.Render, obj ...interface{}) {
// BAD case: func (c *Context) Render(code int, render render.Render, obj ...any) {
// test that information is not leaked when reusing Contexts (using the Pool)

func createMultipartRequest() *http.Request {
Expand Down Expand Up @@ -2374,3 +2374,42 @@ func TestCreateTestContextWithRouteParams(t *testing.T) {
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "hello gin", w.Body.String())
}

type interceptedWriter struct {
ResponseWriter
b *bytes.Buffer
}

func (i interceptedWriter) WriteHeader(code int) {
i.Header().Del("X-Test")
i.ResponseWriter.WriteHeader(code)
}

func TestInterceptedHeader(t *testing.T) {
w := httptest.NewRecorder()
c, r := CreateTestContext(w)

r.Use(func(c *Context) {
i := interceptedWriter{
ResponseWriter: c.Writer,
b: bytes.NewBuffer(nil),
}
c.Writer = i
c.Next()
c.Header("X-Test", "overridden")
c.Writer = i.ResponseWriter
})
r.GET("/", func(c *Context) {
c.Header("X-Test", "original")
c.Header("X-Test-2", "present")
c.String(http.StatusOK, "hello world")
})
c.Request = httptest.NewRequest("GET", "/", nil)
r.HandleContext(c)
// Result() has headers frozen when WriteHeaderNow() has been called
// Compared to this time, this is when the response headers will be flushed
// As response is flushed on c.String, the Header cannot be set by the first
// middleware. Assert this
assert.Equal(t, "", w.Result().Header.Get("X-Test"))
assert.Equal(t, "present", w.Result().Header.Get("X-Test-2"))
}
4 changes: 2 additions & 2 deletions debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strings"
)

const ginSupportMinGoVer = 16
const ginSupportMinGoVer = 18

// IsDebugging returns true if the framework is running in debug mode.
// Use SetMode(gin.ReleaseMode) to disable debug mode.
Expand Down Expand Up @@ -67,7 +67,7 @@ func getMinVer(v string) (uint64, error) {

func debugPrintWARNINGDefault() {
if v, e := getMinVer(runtime.Version()); e == nil && v < ginSupportMinGoVer {
debugPrint(`[WARNING] Now Gin requires Go 1.16+.
debugPrint(`[WARNING] Now Gin requires Go 1.18+.

`)
}
Expand Down
4 changes: 2 additions & 2 deletions debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

// TODO
// func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {
// func debugPrint(format string, values ...interface{}) {
// func debugPrint(format string, values ...any) {

func TestIsDebugging(t *testing.T) {
SetMode(DebugMode)
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestDebugPrintWARNINGDefault(t *testing.T) {
})
m, e := getMinVer(runtime.Version())
if e == nil && m < ginSupportMinGoVer {
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)
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)
} else {
assert.Equal(t, "[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)
}
Expand Down
2 changes: 1 addition & 1 deletion deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// BindWith binds the passed struct pointer using the specified binding engine.
// See the binding package.
func (c *Context) BindWith(obj any, b binding.Binding) error {
log.Println(`BindWith(\"interface{}, binding.Binding\") error is going to
log.Println(`BindWith(\"any, binding.Binding\") error is going to
be deprecated, please check issue #662 and either use MustBindWith() if you
want HTTP 400 to be automatically returned if any error occur, or use
ShouldBindWith() if you need to manage the error.`)
Expand Down