Skip to content

Commit f2e1e4f

Browse files
committed
refactor: refactor codebase to improve type safety and testing
- Replace type assertion with variable declaration for `job` type Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
1 parent 5f7fb02 commit f2e1e4f

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

README.md

+12-20
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,9 @@ func main() {
131131

132132
// initial queue pool
133133
q := queue.NewPool(5, queue.WithFn(func(ctx context.Context, m core.TaskMessage) error {
134-
v, ok := m.(*job)
135-
if !ok {
136-
if err := json.Unmarshal(m.Payload(), &v); err != nil {
137-
return err
138-
}
134+
var v job
135+
if err := json.Unmarshal(m.Payload(), &v); err != nil {
136+
return err
139137
}
140138

141139
rets <- "Hi, " + v.Name + ", " + v.Message
@@ -208,11 +206,9 @@ func main() {
208206
// concurrent job number
209207
nsq.WithMaxInFlight(10),
210208
nsq.WithRunFunc(func(ctx context.Context, m core.TaskMessage) error {
211-
v, ok := m.(*job)
212-
if !ok {
213-
if err := json.Unmarshal(m.Payload(), &v); err != nil {
214-
return err
215-
}
209+
var v job
210+
if err := json.Unmarshal(m.Payload(), &v); err != nil {
211+
return err
216212
}
217213

218214
rets <- v.Message
@@ -287,11 +283,9 @@ func main() {
287283
nats.WithSubj("example"),
288284
nats.WithQueue("foobar"),
289285
nats.WithRunFunc(func(ctx context.Context, m core.TaskMessage) error {
290-
v, ok := m.(*job)
291-
if !ok {
292-
if err := json.Unmarshal(m.Payload(), &v); err != nil {
293-
return err
294-
}
286+
var v job
287+
if err := json.Unmarshal(m.Payload(), &v); err != nil {
288+
return err
295289
}
296290

297291
rets <- v.Message
@@ -371,11 +365,9 @@ func main() {
371365
redisdb.WithAddr("127.0.0.1:6379"),
372366
redisdb.WithChannel("foobar"),
373367
redisdb.WithRunFunc(func(ctx context.Context, m core.TaskMessage) error {
374-
v, ok := m.(*job)
375-
if !ok {
376-
if err := json.Unmarshal(m.Payload(), &v); err != nil {
377-
return err
378-
}
368+
var v job
369+
if err := json.Unmarshal(m.Payload(), &v); err != nil {
370+
return err
379371
}
380372

381373
rets <- v.Message

0 commit comments

Comments
 (0)