Skip to content

🐛 [Bug]: client.SetValWithStruct set zero value if the field is slice #3167

@ksw2000

Description

@ksw2000

Bug Description

The function SetValWithStruct() sets values using structs. It skips zero values, such as 0 and false. 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:

func TestSetValWithStructForSlices(t *testing.T) {
	p := &QueryParam{
		Args: fasthttp.AcquireArgs(),
	}
	SetValWithStruct(p, "param", struct {
		TZeroInt   int
		TInt       int
		TIntSlice  []int
		TBoolSlice []bool
	}{
		TZeroInt:   0,
		TInt:       1,
		TIntSlice:  []int{0, 1, 2},
		TBoolSlice: []bool{true, false},
	})

	fmt.Println(string(p.Peek("TZeroInt")))
	fmt.Println(string(p.Peek("TInt")))
	for _, v := range p.PeekMulti("TIntSlice") {
		fmt.Print(string(v), ", ")
	}
	fmt.Println()
	for _, v := range p.PeekMulti("TBoolSlice") {
		fmt.Print(string(v), ", ")
	}
	fmt.Println()
}
cd client
go test --run TestSetValWithStructForSlices -v

result:


1
0, 1, 2, 
true,

Expected Behavior


1
1, 2, 
true,

The SetValWithStruct() should skip 0 in the int slice too.

Fiber Version

latest

Code Snippet (optional)

No response

Checklist:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions