Skip to content

Commit beaed0b

Browse files
committed
Fixed with tags behavior
- Made sure there are at least two arguments (because 0 % 2 == 0 so that check did not suffice) - Set tagset capacity to correct size because that is exactly how long it must be when everything goes right - Completely restructured for-loop to do what it was intended to do
1 parent c596cc5 commit beaed0b

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

core/readpref/options.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ func WithMaxStaleness(ms time.Duration) Option {
3434
// overrides all previous calls to either method.
3535
func WithTags(tags ...string) Option {
3636
return func(rp *ReadPref) error {
37-
if len(tags)%2 != 0 {
37+
l := len(tags)
38+
if l < 2 || l % 2 != 0 {
3839
return ErrInvalidTagSet
3940
}
4041

41-
tagset := make(tag.Set, 0)
42+
tagset := make(tag.Set, 0, l/2)
4243

43-
for i := 0; i < len(tags)/2; i++ {
44-
tagset = append(tagset, tag.Tag{Name: tags[i], Value: tags[i+1]})
44+
for i := 1; i < l; i+=2 {
45+
tagset = append(tagset, tag.Tag{Name: tags[i-1], Value: tags[i]})
4546
}
4647

4748
return WithTagSets(tagset)(rp)

0 commit comments

Comments
 (0)