fiber icon indicating copy to clipboard operation
fiber copied to clipboard

🚀 [Feature]: Adding Default Value Support for QueryParser

Open gopkg-dev opened this issue 2 years ago • 7 comments

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:

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

gopkg-dev avatar Aug 11 '23 04:08 gopkg-dev

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

Could you please assign the same to me?

manujgrover71 avatar Aug 19 '23 10:08 manujgrover71

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?

manujgrover71 avatar Aug 19 '23 11:08 manujgrover71

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

ReneWerner87 avatar Aug 19 '23 11:08 ReneWerner87

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

manujgrover71 avatar Aug 19 '23 12:08 manujgrover71

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

gaby avatar Aug 19 '23 15:08 gaby

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

muratmirgun avatar Oct 29 '23 10:10 muratmirgun

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

Done

efectn avatar Oct 29 '23 12:10 efectn