chore: change interface{} to any (#2818)
* chore: change interface{} to any
* chore: update goctl version to 1.5.0
* chore: update goctl deps
This commit is contained in:
@@ -26,7 +26,7 @@ var (
|
||||
)
|
||||
|
||||
// Parse parses the request.
|
||||
func Parse(r *http.Request, v interface{}) error {
|
||||
func Parse(r *http.Request, v any) error {
|
||||
if err := ParsePath(r, v); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -43,12 +43,12 @@ func Parse(r *http.Request, v interface{}) error {
|
||||
}
|
||||
|
||||
// ParseHeaders parses the headers request.
|
||||
func ParseHeaders(r *http.Request, v interface{}) error {
|
||||
func ParseHeaders(r *http.Request, v any) error {
|
||||
return encoding.ParseHeaders(r.Header, v)
|
||||
}
|
||||
|
||||
// ParseForm parses the form request.
|
||||
func ParseForm(r *http.Request, v interface{}) error {
|
||||
func ParseForm(r *http.Request, v any) error {
|
||||
params, err := GetFormValues(r)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -80,7 +80,7 @@ func ParseHeader(headerValue string) map[string]string {
|
||||
}
|
||||
|
||||
// ParseJsonBody parses the post request which contains json in body.
|
||||
func ParseJsonBody(r *http.Request, v interface{}) error {
|
||||
func ParseJsonBody(r *http.Request, v any) error {
|
||||
if withJsonBody(r) {
|
||||
reader := io.LimitReader(r.Body, maxBodyLen)
|
||||
return mapping.UnmarshalJsonReader(reader, v)
|
||||
@@ -91,9 +91,9 @@ func ParseJsonBody(r *http.Request, v interface{}) error {
|
||||
|
||||
// ParsePath parses the symbols reside in url path.
|
||||
// Like http://localhost/bag/:name
|
||||
func ParsePath(r *http.Request, v interface{}) error {
|
||||
func ParsePath(r *http.Request, v any) error {
|
||||
vars := pathvar.Vars(r)
|
||||
m := make(map[string]interface{}, len(vars))
|
||||
m := make(map[string]any, len(vars))
|
||||
for k, v := range vars {
|
||||
m[k] = v
|
||||
}
|
||||
|
||||
@@ -13,8 +13,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
errorHandler func(error) (int, interface{})
|
||||
errorHandlerCtx func(context.Context, error) (int, interface{})
|
||||
errorHandler func(error) (int, any)
|
||||
errorHandlerCtx func(context.Context, error) (int, any)
|
||||
lock sync.RWMutex
|
||||
)
|
||||
|
||||
@@ -34,13 +34,13 @@ func ErrorCtx(ctx context.Context, w http.ResponseWriter, err error,
|
||||
handlerCtx := errorHandlerCtx
|
||||
lock.RUnlock()
|
||||
|
||||
var handler func(error) (int, interface{})
|
||||
var handler func(error) (int, any)
|
||||
if handlerCtx != nil {
|
||||
handler = func(err error) (int, interface{}) {
|
||||
handler = func(err error) (int, any) {
|
||||
return handlerCtx(ctx, err)
|
||||
}
|
||||
}
|
||||
writeJson := func(w http.ResponseWriter, code int, v interface{}) {
|
||||
writeJson := func(w http.ResponseWriter, code int, v any) {
|
||||
WriteJsonCtx(ctx, w, code, v)
|
||||
}
|
||||
doHandleError(w, err, handler, writeJson, fns...)
|
||||
@@ -52,45 +52,45 @@ func Ok(w http.ResponseWriter) {
|
||||
}
|
||||
|
||||
// OkJson writes v into w with 200 OK.
|
||||
func OkJson(w http.ResponseWriter, v interface{}) {
|
||||
func OkJson(w http.ResponseWriter, v any) {
|
||||
WriteJson(w, http.StatusOK, v)
|
||||
}
|
||||
|
||||
// OkJsonCtx writes v into w with 200 OK.
|
||||
func OkJsonCtx(ctx context.Context, w http.ResponseWriter, v interface{}) {
|
||||
func OkJsonCtx(ctx context.Context, w http.ResponseWriter, v any) {
|
||||
WriteJsonCtx(ctx, w, http.StatusOK, v)
|
||||
}
|
||||
|
||||
// SetErrorHandler sets the error handler, which is called on calling Error.
|
||||
func SetErrorHandler(handler func(error) (int, interface{})) {
|
||||
func SetErrorHandler(handler func(error) (int, any)) {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
errorHandler = handler
|
||||
}
|
||||
|
||||
// SetErrorHandlerCtx sets the error handler, which is called on calling Error.
|
||||
func SetErrorHandlerCtx(handlerCtx func(context.Context, error) (int, interface{})) {
|
||||
func SetErrorHandlerCtx(handlerCtx func(context.Context, error) (int, any)) {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
errorHandlerCtx = handlerCtx
|
||||
}
|
||||
|
||||
// WriteJson writes v as json string into w with code.
|
||||
func WriteJson(w http.ResponseWriter, code int, v interface{}) {
|
||||
func WriteJson(w http.ResponseWriter, code int, v any) {
|
||||
if err := doWriteJson(w, code, v); err != nil {
|
||||
logx.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// WriteJsonCtx writes v as json string into w with code.
|
||||
func WriteJsonCtx(ctx context.Context, w http.ResponseWriter, code int, v interface{}) {
|
||||
func WriteJsonCtx(ctx context.Context, w http.ResponseWriter, code int, v any) {
|
||||
if err := doWriteJson(w, code, v); err != nil {
|
||||
logx.WithContext(ctx).Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func doHandleError(w http.ResponseWriter, err error, handler func(error) (int, interface{}),
|
||||
writeJson func(w http.ResponseWriter, code int, v interface{}),
|
||||
func doHandleError(w http.ResponseWriter, err error, handler func(error) (int, any),
|
||||
writeJson func(w http.ResponseWriter, code int, v any),
|
||||
fns ...func(w http.ResponseWriter, err error)) {
|
||||
if handler == nil {
|
||||
if len(fns) > 0 {
|
||||
@@ -122,7 +122,7 @@ func doHandleError(w http.ResponseWriter, err error, handler func(error) (int, i
|
||||
}
|
||||
}
|
||||
|
||||
func doWriteJson(w http.ResponseWriter, code int, v interface{}) error {
|
||||
func doWriteJson(w http.ResponseWriter, code int, v any) error {
|
||||
bs, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
||||
@@ -30,7 +30,7 @@ func TestError(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
errorHandler func(error) (int, interface{})
|
||||
errorHandler func(error) (int, any)
|
||||
expectHasBody bool
|
||||
expectBody string
|
||||
expectCode int
|
||||
@@ -45,7 +45,7 @@ func TestError(t *testing.T) {
|
||||
{
|
||||
name: "customized error handler return string",
|
||||
input: body,
|
||||
errorHandler: func(err error) (int, interface{}) {
|
||||
errorHandler: func(err error) (int, any) {
|
||||
return http.StatusForbidden, err.Error()
|
||||
},
|
||||
expectHasBody: true,
|
||||
@@ -55,7 +55,7 @@ func TestError(t *testing.T) {
|
||||
{
|
||||
name: "customized error handler return error",
|
||||
input: body,
|
||||
errorHandler: func(err error) (int, interface{}) {
|
||||
errorHandler: func(err error) (int, any) {
|
||||
return http.StatusForbidden, err
|
||||
},
|
||||
expectHasBody: true,
|
||||
@@ -65,7 +65,7 @@ func TestError(t *testing.T) {
|
||||
{
|
||||
name: "customized error handler return nil",
|
||||
input: body,
|
||||
errorHandler: func(err error) (int, interface{}) {
|
||||
errorHandler: func(err error) (int, any) {
|
||||
return http.StatusForbidden, nil
|
||||
},
|
||||
expectHasBody: false,
|
||||
@@ -174,7 +174,7 @@ func TestWriteJsonMarshalFailed(t *testing.T) {
|
||||
w := tracedResponseWriter{
|
||||
headers: make(map[string][]string),
|
||||
}
|
||||
WriteJson(&w, http.StatusOK, map[string]interface{}{
|
||||
WriteJson(&w, http.StatusOK, map[string]any{
|
||||
"Data": complex(0, 0),
|
||||
})
|
||||
assert.Equal(t, http.StatusInternalServerError, w.code)
|
||||
@@ -225,7 +225,7 @@ func TestErrorCtx(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
errorHandlerCtx func(context.Context, error) (int, interface{})
|
||||
errorHandlerCtx func(context.Context, error) (int, any)
|
||||
expectHasBody bool
|
||||
expectBody string
|
||||
expectCode int
|
||||
@@ -240,7 +240,7 @@ func TestErrorCtx(t *testing.T) {
|
||||
{
|
||||
name: "customized error handler return string",
|
||||
input: body,
|
||||
errorHandlerCtx: func(ctx context.Context, err error) (int, interface{}) {
|
||||
errorHandlerCtx: func(ctx context.Context, err error) (int, any) {
|
||||
return http.StatusForbidden, err.Error()
|
||||
},
|
||||
expectHasBody: true,
|
||||
@@ -250,7 +250,7 @@ func TestErrorCtx(t *testing.T) {
|
||||
{
|
||||
name: "customized error handler return error",
|
||||
input: body,
|
||||
errorHandlerCtx: func(ctx context.Context, err error) (int, interface{}) {
|
||||
errorHandlerCtx: func(ctx context.Context, err error) (int, any) {
|
||||
return http.StatusForbidden, err
|
||||
},
|
||||
expectHasBody: true,
|
||||
@@ -260,7 +260,7 @@ func TestErrorCtx(t *testing.T) {
|
||||
{
|
||||
name: "customized error handler return nil",
|
||||
input: body,
|
||||
errorHandlerCtx: func(context.Context, error) (int, interface{}) {
|
||||
errorHandlerCtx: func(context.Context, error) (int, any) {
|
||||
return http.StatusForbidden, nil
|
||||
},
|
||||
expectHasBody: false,
|
||||
@@ -292,7 +292,7 @@ func TestErrorCtx(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
//The current handler is a global event,Set default values to avoid impacting subsequent unit tests
|
||||
// The current handler is a global event,Set default values to avoid impacting subsequent unit tests
|
||||
SetErrorHandlerCtx(nil)
|
||||
}
|
||||
|
||||
@@ -322,7 +322,7 @@ func TestWriteJsonCtxMarshalFailed(t *testing.T) {
|
||||
w := tracedResponseWriter{
|
||||
headers: make(map[string][]string),
|
||||
}
|
||||
WriteJsonCtx(context.Background(), &w, http.StatusOK, map[string]interface{}{
|
||||
WriteJsonCtx(context.Background(), &w, http.StatusOK, map[string]any{
|
||||
"Data": complex(0, 0),
|
||||
})
|
||||
assert.Equal(t, http.StatusInternalServerError, w.code)
|
||||
|
||||
@@ -5,7 +5,7 @@ import "net/http"
|
||||
const xForwardedFor = "X-Forwarded-For"
|
||||
|
||||
// GetFormValues returns the form values.
|
||||
func GetFormValues(r *http.Request) (map[string]interface{}, error) {
|
||||
func GetFormValues(r *http.Request) (map[string]any, error) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -16,7 +16,7 @@ func GetFormValues(r *http.Request) (map[string]interface{}, error) {
|
||||
}
|
||||
}
|
||||
|
||||
params := make(map[string]interface{}, len(r.Form))
|
||||
params := make(map[string]any, len(r.Form))
|
||||
for name := range r.Form {
|
||||
formValue := r.Form.Get(name)
|
||||
if len(formValue) > 0 {
|
||||
|
||||
Reference in New Issue
Block a user