Skip to content

Commit

Permalink
test support go1.18 (#2990)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkerou committed Mar 21, 2022
1 parent 9701b65 commit 2bde107
Show file tree
Hide file tree
Showing 47 changed files with 214 additions and 174 deletions.
10 changes: 10 additions & 0 deletions any.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2022 Gin Core Team. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

//go:build !go1.18
// +build !go1.18

package gin

type any = interface{}
10 changes: 10 additions & 0 deletions binding/any.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2022 Gin Core Team. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

//go:build !go1.18
// +build !go1.18

package binding

type any = interface{}
12 changes: 6 additions & 6 deletions binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ const (
// the form POST.
type Binding interface {
Name() string
Bind(*http.Request, interface{}) error
Bind(*http.Request, any) error
}

// BindingBody adds BindBody method to Binding. BindBody is similar with Bind,
// but it reads the body from supplied bytes instead of req.Body.
type BindingBody interface {
Binding
BindBody([]byte, interface{}) error
BindBody([]byte, any) error
}

// BindingUri adds BindUri method to Binding. BindUri is similar with Bind,
// but it reads the Params.
type BindingUri interface {
Name() string
BindUri(map[string][]string, interface{}) error
BindUri(map[string][]string, any) error
}

// StructValidator is the minimal interface which needs to be implemented in
Expand All @@ -57,11 +57,11 @@ type StructValidator interface {
// If the received type is a struct or pointer to a struct, the validation should be performed.
// If the struct is not valid or the validation itself fails, a descriptive error should be returned.
// Otherwise nil must be returned.
ValidateStruct(interface{}) error
ValidateStruct(any) error

// Engine returns the underlying validator engine which powers the
// StructValidator implementation.
Engine() interface{}
Engine() any
}

// Validator is the default validator which implements the StructValidator
Expand Down Expand Up @@ -110,7 +110,7 @@ func Default(method, contentType string) Binding {
}
}

func validate(obj interface{}) error {
func validate(obj any) error {
if Validator == nil {
return nil
}
Expand Down
12 changes: 6 additions & 6 deletions binding/binding_nomsgpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ const (
// the form POST.
type Binding interface {
Name() string
Bind(*http.Request, interface{}) error
Bind(*http.Request, any) error
}

// BindingBody adds BindBody method to Binding. BindBody is similar with Bind,
// but it reads the body from supplied bytes instead of req.Body.
type BindingBody interface {
Binding
BindBody([]byte, interface{}) error
BindBody([]byte, any) error
}

// BindingUri adds BindUri method to Binding. BindUri is similar with Bind,
// but it reads the Params.
type BindingUri interface {
Name() string
BindUri(map[string][]string, interface{}) error
BindUri(map[string][]string, any) error
}

// StructValidator is the minimal interface which needs to be implemented in
Expand All @@ -54,11 +54,11 @@ type StructValidator interface {
// If the received type is a struct or pointer to a struct, the validation should be performed.
// If the struct is not valid or the validation itself fails, a descriptive error should be returned.
// Otherwise nil must be returned.
ValidateStruct(interface{}) error
ValidateStruct(any) error

// Engine returns the underlying validator engine which powers the
// StructValidator implementation.
Engine() interface{}
Engine() any
}

// Validator is the default validator which implements the StructValidator
Expand Down Expand Up @@ -104,7 +104,7 @@ func Default(method, contentType string) Binding {
}
}

func validate(obj interface{}) error {
func validate(obj any) error {
if Validator == nil {
return nil
}
Expand Down
18 changes: 9 additions & 9 deletions binding/binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ type FooDefaultBarStruct struct {
}

type FooStructUseNumber struct {
Foo interface{} `json:"foo" binding:"required"`
Foo any `json:"foo" binding:"required"`
}

type FooStructDisallowUnknownFields struct {
Foo interface{} `json:"foo" binding:"required"`
Foo any `json:"foo" binding:"required"`
}

type FooBarStructForTimeType struct {
Expand Down Expand Up @@ -93,7 +93,7 @@ type FooStructForTimeTypeFailLocation struct {
}

type FooStructForMapType struct {
MapFoo map[string]interface{} `form:"map_foo"`
MapFoo map[string]any `form:"map_foo"`
}

type FooStructForIgnoreFormTag struct {
Expand All @@ -106,7 +106,7 @@ type InvalidNameType struct {

type InvalidNameMapType struct {
TestName struct {
MapFoo map[string]interface{} `form:"map_foo"`
MapFoo map[string]any `form:"map_foo"`
}
}

Expand All @@ -128,7 +128,7 @@ type FooStructForStructPointerType struct {

type FooStructForSliceMapType struct {
// Unknown type: not support map
SliceMapFoo []map[string]interface{} `form:"slice_map_foo"`
SliceMapFoo []map[string]any `form:"slice_map_foo"`
}

type FooStructForBoolType struct {
Expand All @@ -141,7 +141,7 @@ type FooStructForStringPtrType struct {
}

type FooStructForMapPtrType struct {
PtrBar *map[string]interface{} `form:"ptr_bar"`
PtrBar *map[string]any `form:"ptr_bar"`
}

func TestBindingDefault(t *testing.T) {
Expand Down Expand Up @@ -768,7 +768,7 @@ func TestHeaderBinding(t *testing.T) {
req.Header.Add("fail", `{fail:fail}`)

type failStruct struct {
Fail map[string]interface{} `header:"fail"`
Fail map[string]any `header:"fail"`
}

err := h.Bind(req, &failStruct{})
Expand All @@ -789,11 +789,11 @@ func TestUriBinding(t *testing.T) {
assert.Equal(t, "thinkerou", tag.Name)

type NotSupportStruct struct {
Name map[string]interface{} `uri:"name"`
Name map[string]any `uri:"name"`
}
var not NotSupportStruct
assert.Error(t, b.BindUri(m, &not))
assert.Equal(t, map[string]interface{}(nil), not.Name)
assert.Equal(t, map[string]any(nil), not.Name)
}

func TestUriInnerBinding(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions binding/default_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (err SliceValidationError) Error() string {
var _ StructValidator = &defaultValidator{}

// ValidateStruct receives any kind of type, but only performed struct or pointer to struct type.
func (v *defaultValidator) ValidateStruct(obj interface{}) error {
func (v *defaultValidator) ValidateStruct(obj any) error {
if obj == nil {
return nil
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func (v *defaultValidator) ValidateStruct(obj interface{}) error {
}

// validateStruct receives struct type
func (v *defaultValidator) validateStruct(obj interface{}) error {
func (v *defaultValidator) validateStruct(obj any) error {
v.lazyinit()
return v.validate.Struct(obj)
}
Expand All @@ -84,7 +84,7 @@ func (v *defaultValidator) validateStruct(obj interface{}) error {
// Validator instance. This is useful if you want to register custom validations
// or struct level validations. See validator GoDoc for more info -
// https://pkg.go.dev/github.com/go-playground/validator/v10
func (v *defaultValidator) Engine() interface{} {
func (v *defaultValidator) Engine() any {
v.lazyinit()
return v.validate
}
Expand Down
2 changes: 1 addition & 1 deletion binding/default_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestDefaultValidator(t *testing.T) {
tests := []struct {
name string
v *defaultValidator
obj interface{}
obj any
wantErr bool
}{
{"validate nil obj", &defaultValidator{}, nil, false},
Expand Down
6 changes: 3 additions & 3 deletions binding/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (formBinding) Name() string {
return "form"
}

func (formBinding) Bind(req *http.Request, obj interface{}) error {
func (formBinding) Bind(req *http.Request, obj any) error {
if err := req.ParseForm(); err != nil {
return err
}
Expand All @@ -36,7 +36,7 @@ func (formPostBinding) Name() string {
return "form-urlencoded"
}

func (formPostBinding) Bind(req *http.Request, obj interface{}) error {
func (formPostBinding) Bind(req *http.Request, obj any) error {
if err := req.ParseForm(); err != nil {
return err
}
Expand All @@ -50,7 +50,7 @@ func (formMultipartBinding) Name() string {
return "multipart/form-data"
}

func (formMultipartBinding) Bind(req *http.Request, obj interface{}) error {
func (formMultipartBinding) Bind(req *http.Request, obj any) error {
if err := req.ParseMultipartForm(defaultMemory); err != nil {
return err
}
Expand Down
14 changes: 7 additions & 7 deletions binding/form_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ var (
ErrConvertToMapString = errors.New("can not convert to map of strings")
)

func mapURI(ptr interface{}, m map[string][]string) error {
func mapURI(ptr any, m map[string][]string) error {
return mapFormByTag(ptr, m, "uri")
}

func mapForm(ptr interface{}, form map[string][]string) error {
func mapForm(ptr any, form map[string][]string) error {
return mapFormByTag(ptr, form, "form")
}

func MapFormWithTag(ptr interface{}, form map[string][]string, tag string) error {
func MapFormWithTag(ptr any, form map[string][]string, tag string) error {
return mapFormByTag(ptr, form, tag)
}

var emptyField = reflect.StructField{}

func mapFormByTag(ptr interface{}, form map[string][]string, tag string) error {
func mapFormByTag(ptr any, form map[string][]string, tag string) error {
// Check if ptr is a map
ptrVal := reflect.ValueOf(ptr)
var pointed interface{}
var pointed any
if ptrVal.Kind() == reflect.Ptr {
ptrVal = ptrVal.Elem()
pointed = ptrVal.Interface()
Expand Down Expand Up @@ -73,7 +73,7 @@ func (form formSource) TrySet(value reflect.Value, field reflect.StructField, ta
return setByForm(value, field, form, tagValue, opt)
}

func mappingByPtr(ptr interface{}, setter setter, tag string) error {
func mappingByPtr(ptr any, setter setter, tag string) error {
_, err := mapping(reflect.ValueOf(ptr), emptyField, setter, tag)
return err
}
Expand Down Expand Up @@ -376,7 +376,7 @@ func head(str, sep string) (head string, tail string) {
return str[:idx], str[idx+len(sep):]
}

func setFormMap(ptr interface{}, form map[string][]string) error {
func setFormMap(ptr any, form map[string][]string) error {
el := reflect.TypeOf(ptr).Elem()

if el.Kind() == reflect.Slice {
Expand Down
4 changes: 2 additions & 2 deletions binding/form_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ func TestMappingBaseTypes(t *testing.T) {
}
for _, tt := range []struct {
name string
value interface{}
value any
form string
expect interface{}
expect any
}{
{"base type", struct{ F int }{}, "9", int(9)},
{"base type", struct{ F int8 }{}, "9", int8(9)},
Expand Down
4 changes: 2 additions & 2 deletions binding/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func (headerBinding) Name() string {
return "header"
}

func (headerBinding) Bind(req *http.Request, obj interface{}) error {
func (headerBinding) Bind(req *http.Request, obj any) error {

if err := mapHeader(obj, req.Header); err != nil {
return err
Expand All @@ -21,7 +21,7 @@ func (headerBinding) Bind(req *http.Request, obj interface{}) error {
return validate(obj)
}

func mapHeader(ptr interface{}, h map[string][]string) error {
func mapHeader(ptr any, h map[string][]string) error {
return mappingByPtr(ptr, headerSource(h), "header")
}

Expand Down
6 changes: 3 additions & 3 deletions binding/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ func (jsonBinding) Name() string {
return "json"
}

func (jsonBinding) Bind(req *http.Request, obj interface{}) error {
func (jsonBinding) Bind(req *http.Request, obj any) error {
if req == nil || req.Body == nil {
return errors.New("invalid request")
}
return decodeJSON(req.Body, obj)
}

func (jsonBinding) BindBody(body []byte, obj interface{}) error {
func (jsonBinding) BindBody(body []byte, obj any) error {
return decodeJSON(bytes.NewReader(body), obj)
}

func decodeJSON(r io.Reader, obj interface{}) error {
func decodeJSON(r io.Reader, obj any) error {
decoder := json.NewDecoder(r)
if EnableDecoderUseNumber {
decoder.UseNumber()
Expand Down
6 changes: 3 additions & 3 deletions binding/msgpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ func (msgpackBinding) Name() string {
return "msgpack"
}

func (msgpackBinding) Bind(req *http.Request, obj interface{}) error {
func (msgpackBinding) Bind(req *http.Request, obj any) error {
return decodeMsgPack(req.Body, obj)
}

func (msgpackBinding) BindBody(body []byte, obj interface{}) error {
func (msgpackBinding) BindBody(body []byte, obj any) error {
return decodeMsgPack(bytes.NewReader(body), obj)
}

func decodeMsgPack(r io.Reader, obj interface{}) error {
func decodeMsgPack(r io.Reader, obj any) error {
cdc := new(codec.MsgpackHandle)
if err := codec.NewDecoder(r, cdc).Decode(&obj); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion binding/msgpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestMsgpackBindingBindBody(t *testing.T) {
assert.Equal(t, "FOO", s.Foo)
}

func msgpackBody(t *testing.T, obj interface{}) []byte {
func msgpackBody(t *testing.T, obj any) []byte {
var bs bytes.Buffer
h := &codec.MsgpackHandle{}
err := codec.NewEncoder(&bs, h).Encode(obj)
Expand Down
2 changes: 1 addition & 1 deletion binding/multipart_form_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestFormMultipartBindingBindError(t *testing.T) {

for _, tt := range []struct {
name string
s interface{}
s any
}{
{"wrong type", &struct {
Files int `form:"file"`
Expand Down

0 comments on commit 2bde107

Please sign in to comment.