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:
Kevin Wan
2023-01-24 16:32:02 +08:00
committed by GitHub
parent 7e0ac77139
commit ae87114282
221 changed files with 1910 additions and 2207 deletions

View File

@@ -23,7 +23,7 @@ func tagMiddleware(tag string) Middleware {
// Not recommended (https://golang.org/pkg/reflect/#Value.Pointer),
// but the best we can do.
func funcsEqual(f1, f2 interface{}) bool {
func funcsEqual(f1, f2 any) bool {
val1 := reflect.ValueOf(f1)
val2 := reflect.ValueOf(f2)
return val1.Pointer() == val2.Pointer()

View File

@@ -34,7 +34,7 @@ func TestAuthHandlerFailed(t *testing.T) {
func TestAuthHandler(t *testing.T) {
const key = "B63F477D-BBA3-4E52-96D3-C0034C27694A"
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
token, err := buildToken(key, map[string]interface{}{
token, err := buildToken(key, map[string]any{
"key": "value",
}, 3600)
assert.Nil(t, err)
@@ -63,7 +63,7 @@ func TestAuthHandlerWithPrevSecret(t *testing.T) {
prevKey = "B63F477D-BBA3-4E52-96D3-C0034C27694A"
)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
token, err := buildToken(key, map[string]interface{}{
token, err := buildToken(key, map[string]any{
"key": "value",
}, 3600)
assert.Nil(t, err)
@@ -90,7 +90,7 @@ func TestAuthHandler_NilError(t *testing.T) {
})
}
func buildToken(secretKey string, payloads map[string]interface{}, seconds int64) (string, error) {
func buildToken(secretKey string, payloads map[string]any, seconds int64) (string, error) {
now := time.Now().Unix()
claims := make(jwt.MapClaims)
claims["exp"] = now + seconds

View File

@@ -69,7 +69,7 @@ func (h *timeoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h: make(http.Header),
req: r,
}
panicChan := make(chan interface{}, 1)
panicChan := make(chan any, 1)
go func() {
defer func() {
if p := recover(); p != nil {

View File

@@ -28,7 +28,7 @@ var interceptors = []internal.Interceptor{
// Do sends an HTTP request with the given arguments and returns an HTTP response.
// data is automatically marshal into a *httpRequest, typically it's defined in an API file.
func Do(ctx context.Context, method, url string, data interface{}) (*http.Response, error) {
func Do(ctx context.Context, method, url string, data any) (*http.Response, error) {
req, err := buildRequest(ctx, method, url, data)
if err != nil {
return nil, err
@@ -54,7 +54,7 @@ func (c defaultClient) do(r *http.Request) (*http.Response, error) {
return http.DefaultClient.Do(r)
}
func buildFormQuery(u *nurl.URL, val map[string]interface{}) string {
func buildFormQuery(u *nurl.URL, val map[string]any) string {
query := u.Query()
for k, v := range val {
query.Add(k, fmt.Sprint(v))
@@ -63,13 +63,13 @@ func buildFormQuery(u *nurl.URL, val map[string]interface{}) string {
return query.Encode()
}
func buildRequest(ctx context.Context, method, url string, data interface{}) (*http.Request, error) {
func buildRequest(ctx context.Context, method, url string, data any) (*http.Request, error) {
u, err := nurl.Parse(url)
if err != nil {
return nil, err
}
var val map[string]map[string]interface{}
var val map[string]map[string]any
if data != nil {
val, err = mapping.Marshal(data)
if err != nil {
@@ -111,13 +111,13 @@ func buildRequest(ctx context.Context, method, url string, data interface{}) (*h
return req, nil
}
func fillHeader(r *http.Request, val map[string]interface{}) {
func fillHeader(r *http.Request, val map[string]any) {
for k, v := range val {
r.Header.Add(k, fmt.Sprint(v))
}
}
func fillPath(u *nurl.URL, val map[string]interface{}) error {
func fillPath(u *nurl.URL, val map[string]any) error {
used := make(map[string]lang.PlaceholderType)
fields := strings.Split(u.Path, slash)

View File

@@ -12,7 +12,7 @@ import (
)
// Parse parses the response.
func Parse(resp *http.Response, val interface{}) error {
func Parse(resp *http.Response, val any) error {
if err := ParseHeaders(resp, val); err != nil {
return err
}
@@ -21,12 +21,12 @@ func Parse(resp *http.Response, val interface{}) error {
}
// ParseHeaders parses the response headers.
func ParseHeaders(resp *http.Response, val interface{}) error {
func ParseHeaders(resp *http.Response, val any) error {
return encoding.ParseHeaders(resp.Header, val)
}
// ParseJsonBody parses the response body, which should be in json content type.
func ParseJsonBody(resp *http.Response, val interface{}) error {
func ParseJsonBody(resp *http.Response, val any) error {
defer resp.Body.Close()
if isContentTypeJson(resp) {

View File

@@ -14,7 +14,7 @@ type (
// Service represents a remote HTTP service.
Service interface {
// Do sends an HTTP request with the given arguments and returns an HTTP response.
Do(ctx context.Context, method, url string, data interface{}) (*http.Response, error)
Do(ctx context.Context, method, url string, data any) (*http.Response, error)
// DoRequest sends a HTTP request to the service.
DoRequest(r *http.Request) (*http.Response, error)
}
@@ -43,7 +43,7 @@ func NewServiceWithClient(name string, cli *http.Client, opts ...Option) Service
}
// Do sends an HTTP request with the given arguments and returns an HTTP response.
func (s namedService) Do(ctx context.Context, method, url string, data interface{}) (*http.Response, error) {
func (s namedService) Do(ctx context.Context, method, url string, data any) (*http.Response, error) {
req, err := buildRequest(ctx, method, url, data)
if err != nil {
return nil, err

View File

@@ -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
}

View File

@@ -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)

View File

@@ -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)

View File

@@ -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 {

View File

@@ -13,8 +13,8 @@ var headerUnmarshaler = mapping.NewUnmarshaler(headerKey, mapping.WithStringValu
mapping.WithCanonicalKeyFunc(textproto.CanonicalMIMEHeaderKey))
// ParseHeaders parses the headers request.
func ParseHeaders(header http.Header, v interface{}) error {
m := map[string]interface{}{}
func ParseHeaders(header http.Header, v any) error {
m := map[string]any{}
for k, v := range header {
if len(v) == 1 {
m[k] = v[0]

View File

@@ -53,22 +53,22 @@ func (lc *LogCollector) takeAll() []string {
}
// Error logs the given v along with r in error log.
func Error(r *http.Request, v ...interface{}) {
func Error(r *http.Request, v ...any) {
logx.WithContext(r.Context()).Error(format(r, v...))
}
// Errorf logs the given v with format along with r in error log.
func Errorf(r *http.Request, format string, v ...interface{}) {
func Errorf(r *http.Request, format string, v ...any) {
logx.WithContext(r.Context()).Error(formatf(r, format, v...))
}
// Info logs the given v along with r in access log.
func Info(r *http.Request, v ...interface{}) {
func Info(r *http.Request, v ...any) {
appendLog(r, format(r, v...))
}
// Infof logs the given v with format along with r in access log.
func Infof(r *http.Request, format string, v ...interface{}) {
func Infof(r *http.Request, format string, v ...any) {
appendLog(r, formatf(r, format, v...))
}
@@ -79,11 +79,11 @@ func appendLog(r *http.Request, message string) {
}
}
func format(r *http.Request, v ...interface{}) string {
func format(r *http.Request, v ...any) string {
return formatWithReq(r, fmt.Sprint(v...))
}
func formatf(r *http.Request, format string, v ...interface{}) string {
func formatf(r *http.Request, format string, v ...any) string {
return formatWithReq(r, fmt.Sprintf(format, v...))
}

View File

@@ -80,7 +80,7 @@ func (tp *TokenParser) ParseToken(r *http.Request, secret, prevSecret string) (*
func (tp *TokenParser) doParseToken(r *http.Request, secret string) (*jwt.Token, error) {
return request.ParseFromRequest(r, request.AuthorizationHeaderExtractor,
func(token *jwt.Token) (interface{}, error) {
func(token *jwt.Token) (any, error) {
return []byte(secret), nil
}, request.WithParser(newParser()))
}
@@ -88,7 +88,7 @@ func (tp *TokenParser) doParseToken(r *http.Request, secret string) (*jwt.Token,
func (tp *TokenParser) incrementCount(secret string) {
now := timex.Now()
if tp.resetTime+tp.resetDuration < now {
tp.history.Range(func(key, value interface{}) bool {
tp.history.Range(func(key, value any) bool {
tp.history.Delete(key)
return true
})

View File

@@ -32,7 +32,7 @@ func TestTokenParser(t *testing.T) {
for _, pair := range keys {
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
token, err := buildToken(key, map[string]interface{}{
token, err := buildToken(key, map[string]any{
"key": "value",
}, 3600)
assert.Nil(t, err)
@@ -51,7 +51,7 @@ func TestTokenParser_Expired(t *testing.T) {
prevKey = "B63F477D-BBA3-4E52-96D3-C0034C27694A"
)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
token, err := buildToken(key, map[string]interface{}{
token, err := buildToken(key, map[string]any{
"key": "value",
}, 3600)
assert.Nil(t, err)
@@ -70,7 +70,7 @@ func TestTokenParser_Expired(t *testing.T) {
assert.Equal(t, "value", tok.Claims.(jwt.MapClaims)["key"])
}
func buildToken(secretKey string, payloads map[string]interface{}, seconds int64) (string, error) {
func buildToken(secretKey string, payloads map[string]any, seconds int64) (string, error) {
now := time.Now().Unix()
claims := make(jwt.MapClaims)
claims["exp"] = now + seconds