From 69e41d1cae5c1d3e6754951f18445d1664f6db63 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Sun, 7 Aug 2022 14:31:07 +0200 Subject: [PATCH] feat: marshal raw data to JSON Signed-off-by: Valery Piashchynski --- beanstalkjobs/item.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/beanstalkjobs/item.go b/beanstalkjobs/item.go index e84f274..43c9232 100644 --- a/beanstalkjobs/item.go +++ b/beanstalkjobs/item.go @@ -152,6 +152,13 @@ func (c *Consumer) unpack(id uint64, data []byte, out *Item) error { uid := uuid.NewString() c.log.Debug("get raw payload", zap.String("assigned ID", uid)) + if isJSONEncoded(data) != nil { + data, err = json.Marshal(data) + if err != nil { + return err + } + } + *out = Item{ Job: auto, Ident: uid, @@ -182,3 +189,8 @@ func (c *Consumer) unpack(id uint64, data []byte, out *Item) error { return nil } + +func isJSONEncoded(data []byte) error { + var a any + return json.Unmarshal(data, &a) +}