-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
🐛 [Bug]: client.SetValWithStruct set zero value if the field is slice #3167
Comments
Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
@ksw2000 Hi, are you sure |
@JIeJaitt I'm not entirely sure, since there are two different logics for processing int slice and bool slice in the If the
Lines 948 to 951 in 7b3a36f
|
@ksw2000 I think what you said makes sense. There is indeed a problem here, which creates ambiguity for users. I think if possible, two functions are provided here. One is the normal |
@JIeJaitt |
Hi folks, I'd like to take a stab at fixing this issue. What's the suggested fix? |
@gaby I think the zero values in the slice might need to be removed, since |
@efectn Thoughts? |
@gaby @ksw2000 @neowulf I think the meaning of skipping the 0 of int type and false of boolean type here is unclear. If we want to set a parameter through this method, don't we need 0 and false? If I have a free product, I need to set its price to 0.0 and it is temporarily not sellable func SubmitProductForm(client *Client, product ProductForm) (*Response, error) {
req := client.AcquireRequest()
defer client.ReleaseRequest(req)
req.SetMethod(fiber.MethodPost)
req.SetURL("https://api.example.com/products")
req.SetFormDataWithStruct(product)
return req.Send()
}
// SetFormDataWithStruct sets form data from a struct using Fiber's SetValWithStruct
func (r *Request) SetFormDataWithStruct(s interface{}) *Request {
if r.formData == nil {
r.formData = make(fiber.Map)
}
fiber.SetValWithStruct(r.formData, "form", s)
return r
}
// use
product := ProductForm{
Name: "Free Sample",
Price: 0.0, // price == 0, free product
Quantity: 100,
Sale: false
}
response, err := SubmitProductForm(client, product) In summary, I believe there is no need to control whether to pass 0 and false or provide two functions through a single tag, as this is completely redundant and users will choose the parameter values they need to pass themselves. |
@gaby , what do you think? I believe we should fix the bug first, and in order to maintain backward compatibility for |
Hi @ksw2000 you can create a PR to fix the issue. You don't need to mind backward-compatibility since v3 hasn't been released yet. I also agree with you about the fact that both 0, false should be covered by SetValWithStruct |
…3167 (#3227) * 🩹 fix: make SetValWithStruct set zero values and support more types * 🚨 test: check zero in int_slice * fix: SetValWithStruct does not exist in fiber v2 * 🩹fix: restrict supported types in SetValWithStruct * 🩹fix: golangci-lint --------- Co-authored-by: Juan Calderon-Perez <[email protected]>
Bug Description
The function
SetValWithStruct()
sets values using structs. It skips zero values, such as0
andfalse
. However, if the fields are slices (excluding bool slices), all the values in the slices will be set.How to Reproduce
To reproduce the bugs, we can simply add a test in
client/request_test.go
:result:
Expected Behavior
The
SetValWithStruct()
should skip0
in the int slice too.Fiber Version
latest
Code Snippet (optional)
No response
Checklist:
The text was updated successfully, but these errors were encountered: