From 324a785233aa56d6920209bdd411a01cede67089 Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Fri, 20 Dec 2019 20:14:40 -0800 Subject: [PATCH] Add Hash method to Payload --- asynq.go | 1 - payload.go | 6 ++++++ payload_test.go | 17 +++++++++++++++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/asynq.go b/asynq.go index 06f6cc2..647d8d4 100644 --- a/asynq.go +++ b/asynq.go @@ -6,7 +6,6 @@ import "github.com/go-redis/redis/v7" TODOs: - [P0] asynqmon kill , asynqmon killall - [P0] Pagination for `asynqmon ls` command -- [P0] Better Payload API - Assigning int or any number type to Payload will be converted to float64 in handler - [P0] Show elapsed time for InProgress tasks (asynqmon ls inprogress) - [P0] Redis Memory Usage, Connection info in stats - [P0] Processed, Failed count for today diff --git a/payload.go b/payload.go index 0ca0393..8430abc 100644 --- a/payload.go +++ b/payload.go @@ -19,6 +19,12 @@ func (e *errKeyNotFound) Error() string { return fmt.Sprintf("key %q does not exist", e.key) } +// Has reports whether key exists. +func (p Payload) Has(key string) bool { + _, ok := p[key] + return ok +} + // GetString returns a string value if a string type is associated with // the key, otherwise reports an error. func (p Payload) GetString(key string) (string, error) { diff --git a/payload_test.go b/payload_test.go index e943bb8..3c37481 100644 --- a/payload_test.go +++ b/payload_test.go @@ -8,7 +8,7 @@ import ( "github.com/google/go-cmp/cmp" ) -func TestPayload(t *testing.T) { +func TestPayloadGet(t *testing.T) { names := []string{"luke", "anakin", "ray"} primes := []int{2, 3, 5, 7, 11, 13, 17} user := map[string]interface{}{"name": "Ken", "score": 3.14} @@ -125,7 +125,7 @@ func TestPayload(t *testing.T) { } } -func TestPayloadWithMarshaling(t *testing.T) { +func TestPayloadGetWithMarshaling(t *testing.T) { names := []string{"luke", "anakin", "ray"} primes := []int{2, 3, 5, 7, 11, 13, 17} user := map[string]interface{}{"name": "Ken", "score": 3.14} @@ -252,3 +252,16 @@ func TestPayloadWithMarshaling(t *testing.T) { "duration", gotDuration, err, duration) } } + +func TestPayloadHas(t *testing.T) { + payload := Payload{ + "user_id": 123, + } + + if !payload.Has("user_id") { + t.Errorf("Payload.Has(%q) = false, want true", "user_id") + } + if payload.Has("name") { + t.Errorf("Payload.Has(%q) = true, want false", "name") + } +}