From f4eac4c12a124fcd04ea712f06213296f6ffd1d2 Mon Sep 17 00:00:00 2001
From: Lyuben Penkovski <lyuben.penkovski@vereign.com>
Date: Tue, 19 Apr 2022 12:54:02 +0300
Subject: [PATCH] Return JSON when getting value from cache

---
 design/design.go                       |  4 +++-
 gen/http/cache/server/encode_decode.go |  1 +
 gen/http/openapi.json                  |  2 +-
 gen/http/openapi.yaml                  |  2 ++
 internal/clients/redis/client.go       |  4 ++--
 internal/service/cache/service.go      | 11 +++++++++--
 6 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/design/design.go b/design/design.go
index 1b8d82d..df4c8c8 100644
--- a/design/design.go
+++ b/design/design.go
@@ -59,7 +59,9 @@ var _ = Service("cache", func() {
 				Example("administration")
 			})
 
-			Response(StatusOK)
+			Response(StatusOK, func() {
+				ContentType("application/json")
+			})
 		})
 	})
 
diff --git a/gen/http/cache/server/encode_decode.go b/gen/http/cache/server/encode_decode.go
index 1f443c6..b6587e8 100644
--- a/gen/http/cache/server/encode_decode.go
+++ b/gen/http/cache/server/encode_decode.go
@@ -21,6 +21,7 @@ import (
 func EncodeGetResponse(encoder func(context.Context, http.ResponseWriter) goahttp.Encoder) func(context.Context, http.ResponseWriter, interface{}) error {
 	return func(ctx context.Context, w http.ResponseWriter, v interface{}) error {
 		res, _ := v.(interface{})
+		ctx = context.WithValue(ctx, goahttp.ContentTypeKey, "application/json")
 		enc := encoder(ctx, w)
 		body := res
 		w.WriteHeader(http.StatusOK)
diff --git a/gen/http/openapi.json b/gen/http/openapi.json
index 86a6fd8..b9d4303 100644
--- a/gen/http/openapi.json
+++ b/gen/http/openapi.json
@@ -1 +1 @@
-{"swagger":"2.0","info":{"title":"Cache Service","description":"The cache service exposes interface for working with Redis.","version":""},"host":"localhost:8083","consumes":["application/json","application/xml","application/gob"],"produces":["application/json","application/xml","application/gob"],"paths":{"/liveness":{"get":{"tags":["health"],"summary":"Liveness health","operationId":"health#Liveness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/readiness":{"get":{"tags":["health"],"summary":"Readiness health","operationId":"health#Readiness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/v1/cache":{"get":{"tags":["cache"],"summary":"Get cache","description":"Get JSON value from the cache.","operationId":"cache#Get","parameters":[{"name":"x-cache-key","in":"header","description":"Cache entry key","required":true,"type":"string"},{"name":"x-cache-namespace","in":"header","description":"Cache entry namespace","required":true,"type":"string"},{"name":"x-cache-scope","in":"header","description":"Cache entry scope","required":true,"type":"string"}],"responses":{"200":{"description":"OK response.","schema":{"type":"string","format":"binary"}}},"schemes":["http"]},"post":{"tags":["cache"],"summary":"Set cache","description":"Set a JSON value in the cache.","operationId":"cache#Set","parameters":[{"name":"x-cache-key","in":"header","description":"Cache entry key","required":true,"type":"string"},{"name":"x-cache-namespace","in":"header","description":"Cache entry namespace","required":true,"type":"string"},{"name":"x-cache-scope","in":"header","description":"Cache entry scope","required":true,"type":"string"},{"name":"any","in":"body","required":true,"schema":{"type":"string","format":"binary"}}],"responses":{"201":{"description":"Created response."}},"schemes":["http"]}}}}
\ No newline at end of file
+{"swagger":"2.0","info":{"title":"Cache Service","description":"The cache service exposes interface for working with Redis.","version":""},"host":"localhost:8083","consumes":["application/json","application/xml","application/gob"],"produces":["application/json","application/xml","application/gob"],"paths":{"/liveness":{"get":{"tags":["health"],"summary":"Liveness health","operationId":"health#Liveness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/readiness":{"get":{"tags":["health"],"summary":"Readiness health","operationId":"health#Readiness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/v1/cache":{"get":{"tags":["cache"],"summary":"Get cache","description":"Get JSON value from the cache.","operationId":"cache#Get","produces":["application/json"],"parameters":[{"name":"x-cache-key","in":"header","description":"Cache entry key","required":true,"type":"string"},{"name":"x-cache-namespace","in":"header","description":"Cache entry namespace","required":true,"type":"string"},{"name":"x-cache-scope","in":"header","description":"Cache entry scope","required":true,"type":"string"}],"responses":{"200":{"description":"OK response.","schema":{"type":"string","format":"binary"}}},"schemes":["http"]},"post":{"tags":["cache"],"summary":"Set cache","description":"Set a JSON value in the cache.","operationId":"cache#Set","parameters":[{"name":"x-cache-key","in":"header","description":"Cache entry key","required":true,"type":"string"},{"name":"x-cache-namespace","in":"header","description":"Cache entry namespace","required":true,"type":"string"},{"name":"x-cache-scope","in":"header","description":"Cache entry scope","required":true,"type":"string"},{"name":"any","in":"body","required":true,"schema":{"type":"string","format":"binary"}}],"responses":{"201":{"description":"Created response."}},"schemes":["http"]}}}}
\ No newline at end of file
diff --git a/gen/http/openapi.yaml b/gen/http/openapi.yaml
index ea46a9b..b1bac2c 100644
--- a/gen/http/openapi.yaml
+++ b/gen/http/openapi.yaml
@@ -42,6 +42,8 @@ paths:
       summary: Get cache
       description: Get JSON value from the cache.
       operationId: cache#Get
+      produces:
+      - application/json
       parameters:
       - name: x-cache-key
         in: header
diff --git a/internal/clients/redis/client.go b/internal/clients/redis/client.go
index fbe9f52..0b1f1c9 100644
--- a/internal/clients/redis/client.go
+++ b/internal/clients/redis/client.go
@@ -31,7 +31,7 @@ func New(addr, user, pass string, db int, defaultTTL time.Duration) *Client {
 	}
 }
 
-func (c *Client) Get(ctx context.Context, key string) (interface{}, error) {
+func (c *Client) Get(ctx context.Context, key string) ([]byte, error) {
 	result := c.rdb.Get(ctx, key)
 	if result.Err() != nil {
 		if result.Err() == redis.Nil {
@@ -39,7 +39,7 @@ func (c *Client) Get(ctx context.Context, key string) (interface{}, error) {
 		}
 		return nil, result.Err()
 	}
-	return result.Val(), nil
+	return []byte(result.Val()), nil
 }
 
 func (c *Client) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error {
diff --git a/internal/service/cache/service.go b/internal/service/cache/service.go
index 7c4357f..4644cc0 100644
--- a/internal/service/cache/service.go
+++ b/internal/service/cache/service.go
@@ -13,7 +13,7 @@ import (
 )
 
 type Cache interface {
-	Get(ctx context.Context, key string) (interface{}, error)
+	Get(ctx context.Context, key string) ([]byte, error)
 	Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
 }
 
@@ -47,7 +47,13 @@ func (s *Service) Get(ctx context.Context, req *cache.CacheGetRequest) (interfac
 		return nil, errors.New("error getting value from cache", err)
 	}
 
-	return data, nil
+	var decodedValue interface{}
+	if err := json.Unmarshal(data, &decodedValue); err != nil {
+		s.logger.Error("cannot decode json value from cache", zap.Error(err))
+		return nil, errors.New("cannot decode json value from cache", err)
+	}
+
+	return decodedValue, nil
 }
 
 func (s *Service) Set(ctx context.Context, req *cache.CacheSetRequest) error {
@@ -68,6 +74,7 @@ func (s *Service) Set(ctx context.Context, req *cache.CacheSetRequest) error {
 		s.logger.Error("error encode payload to json", zap.Error(err), operation)
 		return errors.New("error encode payload to json", err)
 	}
+
 	if err := s.cache.Set(ctx, key, value, 0); err != nil {
 		s.logger.Error("error setting value in cache", zap.Error(err), operation)
 		return errors.New("error setting value in cache", err)
-- 
GitLab