v3 (chore): cleanup (#2255)

* middleware: switch to new Ctx interface in documentation

* all: use any instead of interface{}
pull/2269/head
leonklingele 2022-12-03 13:39:57 +01:00 committed by GitHub
parent 77d14848b8
commit 802defa5ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 31 additions and 35 deletions

View File

@ -35,13 +35,13 @@ var (
// SetParserDecoder allow globally change the option of form decoder, update decoderPool
func SetParserDecoder(parserConfig ParserConfig) {
for _, tag := range tags {
decoderPoolMap[tag] = &sync.Pool{New: func() interface{} {
decoderPoolMap[tag] = &sync.Pool{New: func() any {
return decoderBuilder(parserConfig)
}}
}
}
func decoderBuilder(parserConfig ParserConfig) interface{} {
func decoderBuilder(parserConfig ParserConfig) any {
decoder := schema.NewDecoder()
decoder.IgnoreUnknownKeys(parserConfig.IgnoreUnknownKeys)
if parserConfig.SetAliasTag != "" {
@ -56,7 +56,7 @@ func decoderBuilder(parserConfig ParserConfig) interface{} {
func init() {
for _, tag := range tags {
decoderPoolMap[tag] = &sync.Pool{New: func() interface{} {
decoderPoolMap[tag] = &sync.Pool{New: func() any {
return decoderBuilder(ParserConfig{
IgnoreUnknownKeys: true,
ZeroEmpty: true,
@ -84,7 +84,7 @@ func parse(aliasTag string, out any, data map[string][]string) error {
}
// Parse data into the struct with gorilla/schema
func parseToStruct(aliasTag string, out interface{}, data map[string][]string) error {
func parseToStruct(aliasTag string, out any, data map[string][]string) error {
// Get decoder from pool
schemaDecoder := decoderPoolMap[aliasTag].Get().(*schema.Decoder)
defer decoderPoolMap[aliasTag].Put(schemaDecoder)

View File

@ -872,7 +872,7 @@ func (a *Agent) reset() {
var (
clientPool sync.Pool
agentPool = sync.Pool{
New: func() interface{} {
New: func() any {
return &Agent{req: &Request{}}
},
}

View File

@ -17,8 +17,8 @@ type Storage struct {
type item struct {
// max value is 4294967295 -> Sun Feb 07 2106 06:28:15 GMT+0000
e uint32 // exp
v interface{} // val
e uint32 // exp
v any // val
}
func New() *Storage {
@ -31,7 +31,7 @@ func New() *Storage {
}
// Get value by key
func (s *Storage) Get(key string) interface{} {
func (s *Storage) Get(key string) any {
s.RLock()
v, ok := s.data[key]
s.RUnlock()
@ -42,7 +42,7 @@ func (s *Storage) Get(key string) interface{} {
}
// Set key with value
func (s *Storage) Set(key string, val interface{}, ttl time.Duration) {
func (s *Storage) Set(key string, val any, ttl time.Duration) {
var exp uint32
if ttl > 0 {
exp = uint32(ttl.Seconds()) + atomic.LoadUint32(&utils.Timestamp)

View File

@ -13,9 +13,9 @@ import (
func Test_Memory(t *testing.T) {
var store = New()
var (
key = "john"
val interface{} = []byte("doe")
exp = 1 * time.Second
key = "john"
val any = []byte("doe")
exp = 1 * time.Second
)
store.Set(key, val, 0)

View File

@ -33,7 +33,7 @@ type cache struct {
}
// registerConverter registers a converter function for a custom type.
func (c *cache) registerConverter(value interface{}, converterFunc Converter) {
func (c *cache) registerConverter(value any, converterFunc Converter) {
c.regconv[reflect.TypeOf(value)] = converterFunc
}

View File

@ -55,7 +55,7 @@ func (d *Decoder) IgnoreUnknownKeys(i bool) {
}
// RegisterConverter registers a converter function for a custom type.
func (d *Decoder) RegisterConverter(value interface{}, converterFunc Converter) {
func (d *Decoder) RegisterConverter(value any, converterFunc Converter) {
d.cache.registerConverter(value, converterFunc)
}
@ -67,7 +67,7 @@ func (d *Decoder) RegisterConverter(value interface{}, converterFunc Converter)
// Keys are "paths" in dotted notation to the struct fields and nested structs.
//
// See the package documentation for a full explanation of the mechanics.
func (d *Decoder) Decode(dst interface{}, src map[string][]string) error {
func (d *Decoder) Decode(dst any, src map[string][]string) error {
v := reflect.ValueOf(dst)
if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
return errors.New("schema: interface must be a pointer to struct")

View File

@ -23,14 +23,14 @@ func NewEncoder() *Encoder {
// Encode encodes a struct into map[string][]string.
//
// Intended for use with url.Values.
func (e *Encoder) Encode(src interface{}, dst map[string][]string) error {
func (e *Encoder) Encode(src any, dst map[string][]string) error {
v := reflect.ValueOf(src)
return e.encode(v, dst)
}
// RegisterEncoder registers a converter for encoding a custom type.
func (e *Encoder) RegisterEncoder(value interface{}, encoder func(reflect.Value) string) {
func (e *Encoder) RegisterEncoder(value any, encoder func(reflect.Value) string) {
e.regenc[reflect.TypeOf(value)] = encoder
}

View File

@ -34,7 +34,7 @@ const (
noStore = "no-store"
)
var ignoreHeaders = map[string]interface{}{
var ignoreHeaders = map[string]any{
"Connection": nil,
"Keep-Alive": nil,
"Proxy-Authenticate": nil,

View File

@ -32,7 +32,7 @@ func newManager(storage fiber.Storage) *manager {
// Create new storage handler
manager := &manager{
pool: sync.Pool{
New: func() interface{} {
New: func() any {
return new(item)
},
},

View File

@ -49,7 +49,7 @@ app.Use(csrf.New(csrf.Config{
CookieSameSite: "Lax",
Expiration: 1 * time.Hour,
KeyGenerator: utils.UUID,
Extractor: func(c *fiber.Ctx) (string, error) { ... },
Extractor: func(c fiber.Ctx) (string, error) { ... },
}))
```
@ -144,7 +144,7 @@ type Config struct {
// If set this will be used in place of an Extractor based on KeyLookup.
//
// Optional. Default will create an Extractor based on KeyLookup.
Extractor func(c *fiber.Ctx) (string, error)
Extractor func(c fiber.Ctx) (string, error)
}
```

View File

@ -24,7 +24,7 @@ func newManager(storage fiber.Storage) *manager {
// Create new storage handler
manager := &manager{
pool: sync.Pool{
New: func() interface{} {
New: func() any {
return new(item)
},
},

View File

@ -29,7 +29,7 @@ func newManager(storage fiber.Storage) *manager {
// Create new storage handler
manager := &manager{
pool: sync.Pool{
New: func() interface{} {
New: func() any {
return new(item)
},
},

View File

@ -81,7 +81,7 @@ app.Use(logger.New(logger.Config{
```go
app.Use(logger.New(logger.Config{
CustomTags: map[string]logger.LogFunc{
"custom_tag": func(output logger.Buffer, c *fiber.Ctx, data *logger.Data, extraParam string) (int, error) {
"custom_tag": func(output logger.Buffer, c fiber.Ctx, data *logger.Data, extraParam string) (int, error) {
return output.WriteString("it is a custom tag")
},
},
@ -94,7 +94,7 @@ app.Use(logger.New(logger.Config{
app.Use(logger.New(logger.Config{
TimeFormat: time.RFC3339Nano,
TimeZone: "Asia/Shanghai",
Done: func(c *fiber.Ctx, logString []byte) {
Done: func(c fiber.Ctx, logString []byte) {
if c.Response().StatusCode() != fiber.StatusOK {
reporter.SendToSlack(logString)
}
@ -152,7 +152,7 @@ type Config struct {
// and pass the log string as parameter.
//
// Optional. Default: nil
Done func(c *fiber.Ctx, logString []byte)
Done func(c fiber.Ctx, logString []byte)
// tagFunctions defines the custom tag action
//
@ -197,7 +197,7 @@ type Config struct {
LoggerFunc func(c fiber.Ctx, data *LoggerData, cfg Config) error
}
type LogFunc func(buf logger.Buffer, c *fiber.Ctx, data *logger.Data, extraParam string) (int, error)
type LogFunc func(buf logger.Buffer, c fiber.Ctx, data *logger.Data, extraParam string) (int, error)
```
## Default Config

View File

@ -6,7 +6,7 @@ import (
"time"
)
var DataPool = sync.Pool{New: func() interface{} { return new(Data) }}
var DataPool = sync.Pool{New: func() any { return new(Data) }}
// Data is a struct to define some variables to use in custom logger function.
type Data struct {

View File

@ -42,10 +42,7 @@ type Config struct {
// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
<<<<<<< HEAD
Next func(c fiber.Ctx) bool
=======
Next func(c *fiber.Ctx) bool
// Prefix defines a URL prefix added before "/debug/pprof".
// Note that it should start with (but not end with) a slash.
@ -53,7 +50,6 @@ type Config struct {
//
// Optional. Default: ""
Prefix string
>>>>>>> origin/master
}
```

View File

@ -8,7 +8,7 @@ import (
"github.com/gofiber/fiber/v3"
)
func defaultStackTraceHandler(_ fiber.Ctx, e interface{}) {
func defaultStackTraceHandler(_ fiber.Ctx, e any) {
_, _ = os.Stderr.WriteString(fmt.Sprintf("panic: %v\n%s\n", e, debug.Stack()))
}

View File

@ -27,7 +27,7 @@ func acquireData() *data {
func (d *data) Reset() {
d.Lock()
d.Data = make(map[string]interface{})
d.Data = make(map[string]any)
d.Unlock()
}

View File

@ -71,7 +71,7 @@ var ErrFooTimeOut = errors.New("foo context canceled")
func main() {
app := fiber.New()
h := func(c *fiber.Ctx) error {
h := func(c fiber.Ctx) error {
sleepTime, _ := time.ParseDuration(c.Params("sleepTime") + "ms")
if err := sleepWithContextWithCustomError(c.UserContext(), sleepTime); err != nil {
return fmt.Errorf("%w: execution error", err)