Skip to content

Commit ca86502

Browse files
committed
simplifying conversions and workflow
1 parent 8403d11 commit ca86502

File tree

8 files changed

+49
-90
lines changed

8 files changed

+49
-90
lines changed

.github/dependabot.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ updates:
88
directory: "/"
99
schedule:
1010
interval: "weekly"
11-
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ on:
44
push:
55
branches:
66
- '**'
7+
tags:
8+
- 'v*'
9+
10+
permissions:
11+
contents: read
712

813
jobs:
914
test:
@@ -20,9 +25,11 @@ jobs:
2025
go-version: 1.18
2126
- name: Build
2227
run: go build -v ./...
28+
- name: Fmt
29+
run: test -z $(gofmt -l .)
30+
- name: Vet
31+
run: go vet ./...
2332
- name: Test
2433
run: go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
2534
- name: Codecov
2635
uses: codecov/codecov-action@v3
27-
- name: Vet
28-
run: go vet ./...

.github/workflows/lint.yaml

Lines changed: 0 additions & 23 deletions
This file was deleted.

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
[![Go Reference](https://pkg.go.dev/badge/github.com/semihbkgr/go-rsmq.svg)](https://pkg.go.dev/github.com/semihbkgr/go-rsmq)
2-
![ci workflow](https://github.com/semihbkgr/go-rsmq/actions/workflows/ci.yml/badge.svg)
3-
[![codecov](https://codecov.io/gh/SemihBKGR/go-rsmq/branch/master/graph/badge.svg?token=IVOQ6PLNHM)](https://codecov.io/gh/SemihBKGR/go-rsmq)
4-
51
# go-rsmq
62

3+
[![CI workflow](https://github.com/semihbkgr/go-rsmq/actions/workflows/ci.yaml/badge.svg)](https://github.com/semihbkgr/go-rsmq/actions/workflows/ci.yaml)
4+
[![Codecov](https://codecov.io/gh/SemihBKGR/go-rsmq/branch/master/graph/badge.svg?token=IVOQ6PLNHM)](https://codecov.io/gh/SemihBKGR/go-rsmq)
5+
[![Go Reference](https://pkg.go.dev/badge/github.com/semihbkgr/go-rsmq.svg)](https://pkg.go.dev/github.com/semihbkgr/go-rsmq)
6+
77
A lightweight message queue for Go that requires no dedicated queue server. Just a Redis server.
88

99
Go implementation of https://github.com/smrchy/rsmq.
1010

1111
```shell
12-
go get github.com/semihbkgr/go-rsmq
12+
$ go get github.com/semihbkgr/go-rsmq
1313
```
1414

1515
## Redis Simple Message Queue
@@ -50,10 +50,10 @@ if msg == nil {
5050

5151
```
5252

53-
[Producer/Consumer example](./example_test.go)
53+
[Producer/Consumer example](./example/example.go)
5454

5555
## Implementation Notes
5656

57-
All details about the queue are [here](https://github.com/smrchy/rsmq/blob/master/README.md).
57+
All details about the queue implementation are in [here](https://github.com/smrchy/rsmq/blob/master/README.md).
5858

59-
It follows all naming conventions of javascript implementation.
59+
go-rsmq follows all the naming conventions of javascript implementation.

convert.go

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,32 @@
11
package rsmq
22

33
import (
4-
"fmt"
54
"math/rand"
65
"strconv"
76
"strings"
87
)
98

10-
func convertToSigned[n int | int64](a any) n {
11-
switch v := a.(type) {
12-
case int:
13-
return n(v)
14-
case int64:
15-
return n(v)
16-
case string:
17-
r, err := strconv.ParseInt(v, 10, 0)
18-
if err != nil {
19-
panic(err)
20-
}
21-
return n(r)
22-
default:
23-
panic(fmt.Errorf("type: %T, incompatible type to convert to signed", a))
9+
func convertStringToInt[n int | int64](s any) n {
10+
r, err := strconv.ParseInt(s.(string), 10, 0)
11+
if err != nil {
12+
panic(err)
2413
}
14+
return n(r)
2515
}
2616

27-
func convertToUnsigned[n uint | uint64](a any) n {
28-
switch v := a.(type) {
29-
case uint:
30-
return n(v)
31-
case uint64:
32-
return n(v)
33-
case int:
34-
return n(v)
35-
case int64:
36-
return n(v)
37-
case string:
38-
r, err := strconv.ParseUint(v, 10, 0)
39-
if err != nil {
40-
panic(err)
41-
}
42-
return n(r)
43-
default:
44-
panic(fmt.Errorf("type: %T, incompatible type to convert to unsigned", a))
17+
func convertStringToUint[n uint | uint64](s any) n {
18+
r, err := strconv.ParseUint(s.(string), 10, 0)
19+
if err != nil {
20+
panic(err)
4521
}
22+
return n(r)
4623
}
4724

48-
func convertStringToUnsigned[n uint | uint64](s any, def n) n {
25+
func convertIntToUint(s any) uint64 {
26+
return uint64(s.(int64))
27+
}
28+
29+
func convertStringToUnsignedOrDefault[n uint | uint64](s any, def n) n {
4930
if s == nil {
5031
return def
5132
}
@@ -59,9 +40,6 @@ func convertStringToUnsigned[n uint | uint64](s any, def n) n {
5940
const idLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
6041

6142
func makeID(n int) string {
62-
if n <= 0 {
63-
return ""
64-
}
6543
sb := strings.Builder{}
6644
ln := len(idLetters)
6745
for i := 0; i < n; i++ {

example_test.go renamed to example/example.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
package rsmq_test
1+
package example
22

33
import (
44
"fmt"
5-
"github.com/go-redis/redis"
6-
"github.com/semihbkgr/go-rsmq"
75
"strconv"
86
"time"
9-
)
107

11-
func Example_producer_consumer() {
8+
"github.com/go-redis/redis"
9+
"github.com/semihbkgr/go-rsmq"
10+
)
1211

12+
func exampleProducerConsumer() {
1313
ns := "example"
1414
qname := "queue"
1515

@@ -46,5 +46,4 @@ func Example_producer_consumer() {
4646
}
4747
}
4848
}
49-
5049
}

rsmq.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ func (rsmq *RedisSMQ) getQueue(qname string, uid bool) (*queueDef, error) {
172172
return nil, ErrQueueNotFound
173173
}
174174

175-
vt := convertToUnsigned[uint](hmGetValues[0])
176-
delay := convertToUnsigned[uint](hmGetValues[1])
177-
maxsize := convertToSigned[int](hmGetValues[2])
175+
vt := convertStringToUint[uint](hmGetValues[0])
176+
delay := convertStringToUint[uint](hmGetValues[1])
177+
maxsize := convertStringToInt[int](hmGetValues[2])
178178

179179
t := timeCmd.Val()
180180

@@ -221,13 +221,13 @@ func (rsmq *RedisSMQ) GetQueueAttributes(qname string) (*QueueAttributes, error)
221221

222222
hmGetValues := hmGetSliceCmd.Val()
223223

224-
vt := convertToUnsigned[uint](hmGetValues[0])
225-
delay := convertToUnsigned[uint](hmGetValues[1])
226-
maxsize := convertToSigned[int](hmGetValues[2])
227-
totalRecv := convertStringToUnsigned[uint64](hmGetValues[3], 0)
228-
totalSent := convertStringToUnsigned[uint64](hmGetValues[4], 0)
229-
created := convertToUnsigned[uint64](hmGetValues[5])
230-
modified := convertToUnsigned[uint64](hmGetValues[6])
224+
vt := convertStringToUint[uint](hmGetValues[0])
225+
delay := convertStringToUint[uint](hmGetValues[1])
226+
maxsize := convertStringToInt[int](hmGetValues[2])
227+
totalRecv := convertStringToUnsignedOrDefault[uint64](hmGetValues[3], 0)
228+
totalSent := convertStringToUnsignedOrDefault[uint64](hmGetValues[4], 0)
229+
created := convertStringToUint[uint64](hmGetValues[5])
230+
modified := convertStringToUint[uint64](hmGetValues[6])
231231

232232
msgs := uint64(zCardIntCmd.Val())
233233
hiddenMsgs := uint64(zCountIntCmd.Val())
@@ -420,8 +420,8 @@ func (rsmq *RedisSMQ) createQueueMessage(cmd *redis.Cmd) (*QueueMessage, error)
420420
}
421421
id := vals[0].(string)
422422
message := vals[1].(string)
423-
rc := convertToUnsigned[uint64](vals[2])
424-
fr := convertToSigned[int64](vals[3])
423+
rc := convertIntToUint(vals[2])
424+
fr := convertStringToInt[int64](vals[3])
425425
sent, err := strconv.ParseInt(id[0:10], 36, 64)
426426
if err != nil {
427427
panic(err)

scripts.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ return o`
7676
// KEYS[1]: the zset key
7777
// KEYS[2]: the message id
7878
//
79-
//
8079
// * Find the message id
8180
// * Set the new timer
8281
//

0 commit comments

Comments
 (0)