Skip to content

🚀 [Feature]: Implement Generic APIs for Enhanced Variable Type #2758

@dozheiny

Description

@dozheiny

Feature Description

Some APIs like ctx.QueryBool or ctx.QueryFloat convert given variables. Why don't add APIs that let users convert the needed variable types?

For example, Query convertor API can be implemented like this (Since we can't use generics in methods):

func QueryFunc[T any](c *Ctx, key string, convertor func(string)(T, error), defaultValue ...T) (T, error)

Usage:

package main

import (
    "github.com/gofiber/fiber/v2"
    "github.com/google/uuid"
    "log"
)

func main() {
	app := fiber.New()

	// [GET] /?id=01827964-1320-47b4-b2fa-c67fa9c39bed
	app.Get("/", func(c *fiber.Ctx) error {

		id, err := fiber.QueryFunc(c, "id", uuid.Parse)
       		if err != nil {
			log.Fatal(err)
			}

       		// some functionality
    })

    app.Listen(":3000")
}

We can implement other APIs like ctx.Params and ctx.Get functions.

Additional Context (optional)

No response

Code Snippet (optional)

No response

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my suggestion prior to opening this one.
  • I understand that improperly formatted feature requests may be closed without explanation.

Metadata

Metadata

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions