From a2cfaa87749b7ed9f84bb8d91fddd99f6ed5c989 Mon Sep 17 00:00:00 2001 From: Fajri Date: Wed, 10 May 2023 17:12:38 +0700 Subject: [PATCH] feature: add ParseCacheResultToPointerObject (#51) --- common.go | 17 +++++++++++++++ common_test.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 10 +++++++-- 4 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 common_test.go diff --git a/common.go b/common.go index 07a1bd0..c927fd8 100644 --- a/common.go +++ b/common.go @@ -1,6 +1,8 @@ package cacher import ( + "encoding/json" + "fmt" "strconv" redigo "github.com/gomodule/redigo/redis" @@ -16,6 +18,21 @@ func SafeUnlock(mutexes ...*redsync.Mutex) { } } +// ParseCacheResultToPointerObject parse cache result to any object you want +func ParseCacheResultToPointerObject[T any](in any) (*T, error) { + var obj *T + by, ok := in.([]byte) + if !ok { + return nil, fmt.Errorf("failed to cast %T to byte", in) + } + + err := json.Unmarshal(by, &obj) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal %s to %T", by, obj) + } + return obj, nil +} + // parse result return from scan // the index 0 is the cursor // and the rest is the elements diff --git a/common_test.go b/common_test.go new file mode 100644 index 0000000..2085515 --- /dev/null +++ b/common_test.go @@ -0,0 +1,59 @@ +package cacher + +import ( + "fmt" + "testing" + + "github.com/kumparan/go-utils" + + "github.com/stretchr/testify/assert" +) + +func Test_ParseCacheResultToPointerObject(t *testing.T) { + type TestObj struct { + ID int64 + } + + t.Run("ok, primitive types, string", func(t *testing.T) { + data := "123" + res, err := ParseCacheResultToPointerObject[string](utils.ToByte(data)) + assert.NoError(t, err) + assert.Equal(t, *res, data) + }) + + t.Run("ok, primitive types, integer", func(t *testing.T) { + data := int64(123) + res, err := ParseCacheResultToPointerObject[int64](utils.ToByte(data)) + assert.NoError(t, err) + assert.Equal(t, *res, data) + }) + + t.Run("ok, object", func(t *testing.T) { + data := TestObj{ID: 1234} + res, err := ParseCacheResultToPointerObject[TestObj](utils.ToByte(data)) + assert.NoError(t, err) + assert.Equal(t, *res, data) + }) + + t.Run("ok, null cache", func(t *testing.T) { + res, err := ParseCacheResultToPointerObject[TestObj]([]byte(`null`)) + assert.NoError(t, err) + assert.Nil(t, res) + // check null type are equal; + assert.Equal(t, fmt.Sprintf("%T", &TestObj{}), fmt.Sprintf("%T", res)) + }) + + t.Run("error, cache result are not byte ", func(t *testing.T) { + res, err := ParseCacheResultToPointerObject[TestObj](int64(123456)) + assert.Error(t, err) + assert.Nil(t, res) + assert.Equal(t, "failed to cast int64 to byte", err.Error()) + }) + + t.Run("error, failed on unmarshal ", func(t *testing.T) { + res, err := ParseCacheResultToPointerObject[TestObj](utils.ToByte([]int64{1, 2, 3, 4, 5, 6})) + assert.Error(t, err) + assert.Nil(t, res) + assert.Equal(t, "failed to unmarshal [1,2,3,4,5,6] to *cacher.TestObj", err.Error()) + }) +} diff --git a/go.mod b/go.mod index a1fc38a..a7c3d07 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/gomodule/redigo v1.8.9 github.com/hashicorp/go-multierror v1.1.1 github.com/jpillora/backoff v1.0.0 + github.com/kumparan/go-utils v1.32.0 github.com/kumparan/redsync/v4 v4.0.0-20220629071359-52de590a1465 github.com/sirupsen/logrus v1.9.0 github.com/stretchr/testify v1.7.5 @@ -24,7 +25,6 @@ require ( github.com/gorilla/css v1.0.0 // indirect github.com/graph-gophers/graphql-go v1.4.0 // indirect github.com/hashicorp/errwrap v1.0.0 // indirect - github.com/kumparan/go-utils v1.30.0 // indirect github.com/leekchan/accounting v0.3.1 // indirect github.com/microcosm-cc/bluemonday v1.0.19 // indirect github.com/oklog/ulid v1.3.1 // indirect diff --git a/go.sum b/go.sum index 0589273..e28cddf 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,7 @@ github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53/go.mod h1:+3 github.com/CloudyKit/jet/v3 v3.0.0/go.mod h1:HKQPgSJmdK8hdoAbKUUWajkHyHo4RaU5rMdUywE7VMo= github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY= github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0= +github.com/agiledragon/gomonkey v2.0.2+incompatible h1:eXKi9/piiC3cjJD1658mEE2o3NjkJ5vDLgYjCQu0Xlw= github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY= github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZpUGpz5+4FfNmIU+FmZg2P3Xaj2v2bfNWmk= github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc= @@ -50,6 +51,7 @@ github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/ github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8 h1:DujepqpGd1hyOd7aW59XpK7Qymp8iy83xq74fLr21is= github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q= github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98= +github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w= github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= @@ -92,6 +94,7 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= @@ -138,14 +141,15 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/kumparan/go-utils v1.30.0 h1:QZ/SuaHoNptCQCIkPWcSB8xRpYzQ7MXNjYSVkPntdkA= -github.com/kumparan/go-utils v1.30.0/go.mod h1:8oLNBRm8YJraRM+EfKUw8MtWX+6PbqRX6ZGLqbY0zO8= +github.com/kumparan/go-utils v1.32.0 h1:B9E5E48nxezncz5jBWz64WdH46hMYAV0WiVUZNpWNLI= +github.com/kumparan/go-utils v1.32.0/go.mod h1:8oLNBRm8YJraRM+EfKUw8MtWX+6PbqRX6ZGLqbY0zO8= github.com/kumparan/redsync/v4 v4.0.0-20220629071359-52de590a1465 h1:YnjMOul6wRIeJxh5SfTJgXu7oZynxCTZOieDM29o67A= github.com/kumparan/redsync/v4 v4.0.0-20220629071359-52de590a1465/go.mod h1:R0eZxHURyPY1UK2BgcvhFa0GSTSeZ6sSZp2hjqEwNVg= github.com/labstack/echo/v4 v4.1.11/go.mod h1:i541M3Fj6f76NZtHSj7TXnyM8n2gaodfvfxNnFqi74g= github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k= github.com/leekchan/accounting v0.3.1 h1:6cIBKG9QngR6tuVV+mWjzcxsJDnoegrc70Ntb3MFqYM= github.com/leekchan/accounting v0.3.1/go.mod h1:3timm6YPhY3YDaGxl0q3eaflX0eoSx3FXn7ckHe4tO0= +github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= @@ -187,6 +191,7 @@ github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAl github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs= github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -313,6 +318,7 @@ golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4f golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=