Skip to content

Commit

Permalink
feat: support peek multi value for query args (#684)
Browse files Browse the repository at this point in the history
Co-authored-by: kinggo <[email protected]>
  • Loading branch information
a631807682 and li-jin-gou authored Oct 19, 2023
1 parent f7a873f commit e02c162
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/protocol/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,17 @@ func (a *Args) PeekExists(key string) (string, bool) {
return peekArgStrExists(a.args, key)
}

// PeekAll returns all the arg values for the given key.
func (a *Args) PeekAll(key string) [][]byte {
var values [][]byte
a.VisitAll(func(k, v []byte) {
if bytesconv.B2s(k) == key {
values = append(values, v)
}
})
return values
}

func visitArgs(args []argsKV, f func(k, v []byte)) {
for i, n := 0, len(args); i < n; i++ {
kv := &args[i]
Expand Down
23 changes: 23 additions & 0 deletions pkg/protocol/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,26 @@ func TestArgsVisitAll(t *testing.T) {
})
assert.DeepEqual(t, []string{"cloudwego", "hertz", "hello", "world"}, s)
}

func TestArgsPeekMulti(t *testing.T) {
var a Args
a.Add("cloudwego", "hertz")
a.Add("cloudwego", "kitex")
a.Add("cloudwego", "")
a.Add("hello", "world")

vv := a.PeekAll("cloudwego")
expectedVV := [][]byte{
[]byte("hertz"),
[]byte("kitex"),
[]byte(nil),
}
assert.DeepEqual(t, expectedVV, vv)

vv = a.PeekAll("aaaa")
assert.DeepEqual(t, 0, len(vv))

vv = a.PeekAll("hello")
expectedVV = [][]byte{[]byte("world")}
assert.DeepEqual(t, expectedVV, vv)
}

0 comments on commit e02c162

Please sign in to comment.