Skip to content

Commit 4ab19e2

Browse files
authored
Add LPOS command (#1556)
* Add LPos
1 parent 16981c0 commit 4ab19e2

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

.github/workflows/go.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
11+
build:
12+
name: Build
13+
runs-on: ubuntu-latest
14+
15+
services:
16+
# Label used to access the service container
17+
redis:
18+
# Docker Hub image
19+
image: redis
20+
# Set health checks to wait until redis has started
21+
options: >-
22+
--health-cmd "redis-cli ping"
23+
--health-interval 10s
24+
--health-timeout 5s
25+
--health-retries 5
26+
ports:
27+
# Maps port 6379 on service container to the host
28+
- 6379:6379
29+
steps:
30+
31+
- name: Set up Go 1.x
32+
uses: actions/setup-go@v2
33+
with:
34+
go-version: ^1.14
35+
36+
- name: Check out code into the Go module directory
37+
uses: actions/checkout@v2
38+
39+
- name: Get dependencies
40+
run: |
41+
go get -v -t -d ./...
42+
if [ -f Gopkg.toml ]; then
43+
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
44+
dep ensure
45+
fi
46+
- name: Install golangci
47+
run: |
48+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.31.0
49+
- name: Test
50+
run: make

commands.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ type Cmdable interface {
168168
LInsertAfter(ctx context.Context, key string, pivot, value interface{}) *IntCmd
169169
LLen(ctx context.Context, key string) *IntCmd
170170
LPop(ctx context.Context, key string) *StringCmd
171+
LPos(ctx context.Context, key string, value string, args LPosArgs) *IntCmd
172+
LPosCount(ctx context.Context, key string, value string, count int64, args LPosArgs) *IntSliceCmd
171173
LPush(ctx context.Context, key string, values ...interface{}) *IntCmd
172174
LPushX(ctx context.Context, key string, values ...interface{}) *IntCmd
173175
LRange(ctx context.Context, key string, start, stop int64) *StringSliceCmd
@@ -1188,6 +1190,37 @@ func (c cmdable) LPop(ctx context.Context, key string) *StringCmd {
11881190
return cmd
11891191
}
11901192

1193+
type LPosArgs struct {
1194+
Rank, MaxLen int64
1195+
}
1196+
1197+
func (c cmdable) LPos(ctx context.Context, key string, value string, a LPosArgs) *IntCmd {
1198+
args := []interface{}{"lpos", key, value}
1199+
if a.Rank != 0 {
1200+
args = append(args, "rank", a.Rank)
1201+
}
1202+
if a.MaxLen != 0 {
1203+
args = append(args, "maxlen", a.MaxLen)
1204+
}
1205+
1206+
cmd := NewIntCmd(ctx, args...)
1207+
_ = c(ctx, cmd)
1208+
return cmd
1209+
}
1210+
1211+
func (c cmdable) LPosCount(ctx context.Context, key string, value string, count int64, a LPosArgs) *IntSliceCmd {
1212+
args := []interface{}{"lpos", key, value, "count", count}
1213+
if a.Rank != 0 {
1214+
args = append(args, "rank", a.Rank)
1215+
}
1216+
if a.MaxLen != 0 {
1217+
args = append(args, "maxlen", a.MaxLen)
1218+
}
1219+
cmd := NewIntSliceCmd(ctx, args...)
1220+
_ = c(ctx, cmd)
1221+
return cmd
1222+
}
1223+
11911224
func (c cmdable) LPush(ctx context.Context, key string, values ...interface{}) *IntCmd {
11921225
args := make([]interface{}, 2, 2+len(values))
11931226
args[0] = "lpush"

commands_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,62 @@ var _ = Describe("Commands", func() {
16911691
Expect(lRange.Val()).To(Equal([]string{"two", "three"}))
16921692
})
16931693

1694+
It("should LPos", func() {
1695+
rPush := client.RPush(ctx, "list", "a")
1696+
Expect(rPush.Err()).NotTo(HaveOccurred())
1697+
rPush = client.RPush(ctx, "list", "b")
1698+
Expect(rPush.Err()).NotTo(HaveOccurred())
1699+
rPush = client.RPush(ctx, "list", "c")
1700+
Expect(rPush.Err()).NotTo(HaveOccurred())
1701+
rPush = client.RPush(ctx, "list", "b")
1702+
Expect(rPush.Err()).NotTo(HaveOccurred())
1703+
1704+
lPos := client.LPos(ctx, "list", "b", redis.LPosArgs{})
1705+
Expect(lPos.Err()).NotTo(HaveOccurred())
1706+
Expect(lPos.Val()).To(Equal(int64(1)))
1707+
1708+
lPos = client.LPos(ctx, "list", "b", redis.LPosArgs{Rank: 2})
1709+
Expect(lPos.Err()).NotTo(HaveOccurred())
1710+
Expect(lPos.Val()).To(Equal(int64(3)))
1711+
1712+
lPos = client.LPos(ctx, "list", "b", redis.LPosArgs{Rank: -2})
1713+
Expect(lPos.Err()).NotTo(HaveOccurred())
1714+
Expect(lPos.Val()).To(Equal(int64(1)))
1715+
1716+
lPos = client.LPos(ctx, "list", "b", redis.LPosArgs{Rank: 2, MaxLen: 1})
1717+
Expect(lPos.Err()).To(Equal(redis.Nil))
1718+
1719+
lPos = client.LPos(ctx, "list", "z", redis.LPosArgs{})
1720+
Expect(lPos.Err()).To(Equal(redis.Nil))
1721+
})
1722+
1723+
It("should LPosCount", func() {
1724+
rPush := client.RPush(ctx, "list", "a")
1725+
Expect(rPush.Err()).NotTo(HaveOccurred())
1726+
rPush = client.RPush(ctx, "list", "b")
1727+
Expect(rPush.Err()).NotTo(HaveOccurred())
1728+
rPush = client.RPush(ctx, "list", "c")
1729+
Expect(rPush.Err()).NotTo(HaveOccurred())
1730+
rPush = client.RPush(ctx, "list", "b")
1731+
Expect(rPush.Err()).NotTo(HaveOccurred())
1732+
1733+
lPos := client.LPosCount(ctx, "list", "b", 2, redis.LPosArgs{})
1734+
Expect(lPos.Err()).NotTo(HaveOccurred())
1735+
Expect(lPos.Val()).To(Equal([]int64{1, 3}))
1736+
1737+
lPos = client.LPosCount(ctx, "list", "b", 2, redis.LPosArgs{Rank: 2})
1738+
Expect(lPos.Err()).NotTo(HaveOccurred())
1739+
Expect(lPos.Val()).To(Equal([]int64{3}))
1740+
1741+
lPos = client.LPosCount(ctx, "list", "b", 1, redis.LPosArgs{Rank: 1, MaxLen: 1})
1742+
Expect(lPos.Err()).NotTo(HaveOccurred())
1743+
Expect(lPos.Val()).To(Equal([]int64{}))
1744+
1745+
lPos = client.LPosCount(ctx, "list", "b", 1, redis.LPosArgs{Rank: 1, MaxLen: 0})
1746+
Expect(lPos.Err()).NotTo(HaveOccurred())
1747+
Expect(lPos.Val()).To(Equal([]int64{1}))
1748+
})
1749+
16941750
It("should LPush", func() {
16951751
lPush := client.LPush(ctx, "list", "World")
16961752
Expect(lPush.Err()).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)