gofiber / fiber

⚡️ Express inspired web framework written in Go
https://gofiber.io
MIT License
33.96k stars 1.67k forks source link

🚀 [Feature]: Adding Default Value Support for QueryParser #2571

Open gopkg-dev opened 1 year ago

gopkg-dev commented 1 year ago

Feature Description

Currently, QueryParser in Fiber does not support setting default values for query parameters directly in the struct. This enhancement request suggests adding the ability to define default values for query parameters within the struct tags.

Additional Context (optional)

https://github.com/gofiber/fiber/issues/1306

Code Snippet (optional)

type Request struct {
    Limit int `query:"limit" default:"10"`
}
var request Request
ctx.QueryParser(&request);
limit := request.Limit // 10

Checklist:

manujgrover71 commented 1 year ago

@gopkg-dev I would like to take this up.

Could you please assign the same to me?

manujgrover71 commented 1 year ago

As pointed out by @hi019 in the issue #1306, a go-default package with similar functionality to our current need already exists.

One potential solution is to simply incorporate the aforementioned package into QueryParser (since this package has been tried and tested and is widely used, we are confident in its accuracy and can announce that the go-default default functionality is now supported).

// QueryParser binds the query string to a struct.
func (c *Ctx) QueryParser(out interface{}) error {
    data := make(map[string][]string)
    var err error

        // populating data map with query params...

    // set defaults in out struct before processing query parameters.
    defaults.SetDefaults(out)

    return c.parseToStruct(queryTag, out, data)
}

@ReneWerner87 @gopkg-dev Thoughts?

ReneWerner87 commented 1 year ago

But then we can use that outside and we just mention it in the documentation for a possible solution to the problem

Since this is independent and can be used separately I would not increase the fiber code and dependencies with it

Because this packet can then also be used for use with the body or header parser just before the structs are passed into the methods

manujgrover71 commented 1 year ago

Because this packet can then also be used for use with the body or header parser just before the structs are passed into the methods

Yes, that makes sense.

However, considering our aim is to incorporate this functionality within struct tags, we would need to decide between utilizing or building a tag parser mechanism and using reflection to populate the fields.

As you suggested, we could advise developers to leverage an external library for this purpose (could be go-default or maybe any other). Alternatively, I'm open to the idea of developing a custom implementation if that aligns better with our goals.

I'd be interested to hear your thoughts on this or any other potential implementations you might have in mind

gaby commented 1 year ago

The https://github.com/mcuadros/go-defaults package hasn't been updated in years, is there an alternative.

muratmirgun commented 1 year ago

Hey Guys, Can you assign me if this problem is not updating

efectn commented 1 year ago

Hey Guys, Can you assign me if this problem is not updating

Done