Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
3 tasks done
gopkg-dev opened this issue Aug 11, 2023 · 8 comments
Closed
3 tasks done

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

gopkg-dev opened this issue Aug 11, 2023 · 8 comments
Assignees
Milestone

Comments

@gopkg-dev
Copy link
Contributor

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)

#1306

Code Snippet (optional)

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

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.
@manujgrover71
Copy link

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

Could you please assign the same to me?

@manujgrover71
Copy link

manujgrover71 commented Aug 19, 2023

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
Copy link
Member

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
Copy link

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
Copy link
Member

gaby commented Aug 19, 2023

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

@muratmirgun
Copy link

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

@efectn
Copy link
Member

efectn commented Oct 29, 2023

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

Done

@efectn
Copy link
Member

efectn commented Dec 4, 2024

Closing since v3 has got this feature.

@efectn efectn closed this as completed Dec 4, 2024
@efectn efectn added the v3 label Dec 4, 2024
@efectn efectn added this to the v3 milestone Dec 4, 2024
@efectn efectn added this to v3 Dec 4, 2024
@efectn efectn moved this to Done in v3 Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

No branches or pull requests

6 participants