Skip to content

Commit 539f177

Browse files
authored
Change to json.RawMessage for messages (#32)
1 parent fa8eec8 commit 539f177

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func main() {
4242
panic(err)
4343
}
4444

45-
id, err := q.Send(ctx, "my_queue", map[string]any{"foo": "bar"})
45+
id, err := q.Send(ctx, "my_queue", json.RawMessage(`{"foo": "bar"}`))
4646
if err != nil {
4747
panic(err)
4848
}

pgmq.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pgmq
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"time"
@@ -22,7 +23,7 @@ type Message struct {
2223
// VT is "visibility time". The UTC timestamp at which the message will
2324
// be available for reading again.
2425
VT time.Time
25-
Message map[string]any
26+
Message json.RawMessage
2627
}
2728

2829
type DB interface {
@@ -116,13 +117,13 @@ func (p *PGMQ) DropQueue(ctx context.Context, queue string) error {
116117

117118
// Send sends a single message to a queue. The message id, unique to the
118119
// queue, is returned.
119-
func (p *PGMQ) Send(ctx context.Context, queue string, msg map[string]any) (int64, error) {
120+
func (p *PGMQ) Send(ctx context.Context, queue string, msg json.RawMessage) (int64, error) {
120121
return p.SendWithDelay(ctx, queue, msg, 0)
121122
}
122123

123124
// SendWithDelay sends a single message to a queue with a delay. The delay
124125
// is specified in seconds. The message id, unique to the queue, is returned.
125-
func (p *PGMQ) SendWithDelay(ctx context.Context, queue string, msg map[string]any, delay int) (int64, error) {
126+
func (p *PGMQ) SendWithDelay(ctx context.Context, queue string, msg json.RawMessage, delay int) (int64, error) {
126127
var msgID int64
127128
err := p.db.
128129
QueryRow(ctx, "SELECT * FROM pgmq.send($1, $2, $3)", queue, msg, delay).
@@ -136,14 +137,14 @@ func (p *PGMQ) SendWithDelay(ctx context.Context, queue string, msg map[string]a
136137

137138
// SendBatch sends a batch of messages to a queue. The message ids, unique to
138139
// the queue, are returned.
139-
func (p *PGMQ) SendBatch(ctx context.Context, queue string, msgs []map[string]any) ([]int64, error) {
140+
func (p *PGMQ) SendBatch(ctx context.Context, queue string, msgs []json.RawMessage) ([]int64, error) {
140141
return p.SendBatchWithDelay(ctx, queue, msgs, 0)
141142
}
142143

143144
// SendBatchWithDelay sends a batch of messages to a queue with a delay. The
144145
// delay is specified in seconds. The message ids, unique to the queue, are
145146
// returned.
146-
func (p *PGMQ) SendBatchWithDelay(ctx context.Context, queue string, msgs []map[string]any, delay int) ([]int64, error) {
147+
func (p *PGMQ) SendBatchWithDelay(ctx context.Context, queue string, msgs []json.RawMessage, delay int) ([]int64, error) {
147148
rows, err := p.db.Query(ctx, "SELECT * FROM pgmq.send_batch($1, $2::jsonb[], $3)", queue, msgs, delay)
148149
if err != nil {
149150
return nil, wrapPostgresError(err)

pgmq_test.go

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pgmq
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"os"
@@ -20,8 +21,8 @@ import (
2021
var q *PGMQ
2122

2223
var (
23-
testMsg1 = map[string]any{"foo": "bar1"}
24-
testMsg2 = map[string]any{"foo": "bar2"}
24+
testMsg1 = json.RawMessage(`{"foo": "bar1"}`)
25+
testMsg2 = json.RawMessage(`{"foo": "bar2"}`)
2526
)
2627

2728
func TestMain(m *testing.M) {
@@ -124,14 +125,53 @@ func TestSend(t *testing.T) {
124125
require.EqualValues(t, 2, id)
125126
}
126127

128+
func TestSendAMarshalledStruct(t *testing.T) {
129+
type A struct {
130+
Val int `json:"val"`
131+
}
132+
133+
a := A{3}
134+
b, err := json.Marshal(a)
135+
require.NoError(t, err)
136+
137+
ctx := context.Background()
138+
queue := t.Name()
139+
140+
err = q.CreateQueue(ctx, queue)
141+
require.NoError(t, err)
142+
143+
_, err = q.Send(ctx, queue, b)
144+
require.NoError(t, err)
145+
146+
msg, err := q.Read(ctx, queue, 0)
147+
require.NoError(t, err)
148+
149+
var aa A
150+
err = json.Unmarshal(msg.Message, &aa)
151+
require.NoError(t, err)
152+
153+
require.EqualValues(t, a, aa)
154+
}
155+
156+
func TestSendInvalidJSONFails(t *testing.T) {
157+
ctx := context.Background()
158+
queue := t.Name()
159+
160+
err := q.CreateQueue(ctx, queue)
161+
require.NoError(t, err)
162+
163+
_, err = q.Send(ctx, queue, json.RawMessage(`{"foo":}`))
164+
require.Error(t, err)
165+
}
166+
127167
func TestSendBatch(t *testing.T) {
128168
ctx := context.Background()
129169
queue := t.Name()
130170

131171
err := q.CreateQueue(ctx, queue)
132172
require.NoError(t, err)
133173

134-
ids, err := q.SendBatch(ctx, queue, []map[string]any{testMsg1, testMsg2})
174+
ids, err := q.SendBatch(ctx, queue, []json.RawMessage{testMsg1, testMsg2})
135175
require.NoError(t, err)
136176
require.Equal(t, []int64{1, 2}, ids)
137177
}
@@ -174,7 +214,7 @@ func TestReadBatch(t *testing.T) {
174214
err := q.CreateQueue(ctx, queue)
175215
require.NoError(t, err)
176216

177-
_, err = q.SendBatch(ctx, queue, []map[string]any{testMsg1, testMsg2})
217+
_, err = q.SendBatch(ctx, queue, []json.RawMessage{testMsg1, testMsg2})
178218
require.NoError(t, err)
179219

180220
time.Sleep(time.Second)
@@ -264,7 +304,7 @@ func TestArchiveBatch(t *testing.T) {
264304
err := q.CreateQueue(ctx, queue)
265305
require.NoError(t, err)
266306

267-
ids, err := q.SendBatch(ctx, queue, []map[string]any{testMsg1, testMsg2})
307+
ids, err := q.SendBatch(ctx, queue, []json.RawMessage{testMsg1, testMsg2})
268308
require.NoError(t, err)
269309

270310
archived, err := q.ArchiveBatch(ctx, queue, ids)
@@ -318,7 +358,7 @@ func TestDeleteBatch(t *testing.T) {
318358
err := q.CreateQueue(ctx, queue)
319359
require.NoError(t, err)
320360

321-
ids, err := q.SendBatch(ctx, queue, []map[string]any{testMsg1, testMsg2})
361+
ids, err := q.SendBatch(ctx, queue, []json.RawMessage{testMsg1, testMsg2})
322362
require.NoError(t, err)
323363

324364
deleted, err := q.DeleteBatch(ctx, queue, ids)
@@ -372,7 +412,7 @@ func TestErrorCases(t *testing.T) {
372412

373413
t.Run("sendBatchError", func(t *testing.T) {
374414
mockDB.EXPECT().Query(ctx, "SELECT * FROM pgmq.send_batch($1, $2::jsonb[], $3)", queue, gomock.Any(), 0).Return(nil, testErr)
375-
ids, err := q.SendBatch(ctx, queue, []map[string]any{testMsg1})
415+
ids, err := q.SendBatch(ctx, queue, []json.RawMessage{testMsg1})
376416
require.Nil(t, ids)
377417
require.ErrorContains(t, err, "postgres error")
378418
})

0 commit comments

Comments
 (0)