Skip to content
Snippets Groups Projects
Commit d2125545 authored by Yordan Kinkov's avatar Yordan Kinkov
Browse files

#3 change SET endpoint payload type to json

parent c379431a
No related branches found
No related tags found
1 merge request!3Create SET cache endpoint
Pipeline #49801 passed with stage
in 43 seconds
......@@ -11,7 +11,7 @@ var CacheGetRequest = Type("CacheGetRequest", func() {
})
var CacheSetRequest = Type("CacheSetRequest", func() {
Field(1, "data", Bytes)
Field(1, "data", Any)
Field(2, "key", String)
Field(3, "namespace", String)
Field(4, "scope", String) // Initial implementation with a single scope
......
......@@ -2,6 +2,7 @@ package cache
import (
"context"
"encoding/json"
"fmt"
"time"
......@@ -51,7 +52,8 @@ func (s *Service) Get(ctx context.Context, req *cache.CacheGetRequest) ([]byte,
func (s *Service) Set(ctx context.Context, req *cache.CacheSetRequest) error {
var operation = zap.String("operation", "set")
if req.Key == "" || req.Namespace == "" || req.Scope == "" || len(req.Data) == 0 {
if req.Key == "" || req.Namespace == "" || req.Scope == "" || len(req.Data.(map[string]interface{})) == 0 {
s.logger.Error("bad request: missing key or namespace or scope or data", operation)
return errors.New(errors.BadRequest, "bad request")
}
......@@ -60,7 +62,13 @@ func (s *Service) Set(ctx context.Context, req *cache.CacheSetRequest) error {
// create key from the input fields
key := makeCacheKey(req.Namespace, req.Scope, req.Key)
if err := s.cache.Set(ctx, key, req.Data, 0); err != nil {
// encode payload to json bytes for storing in cache
value, err := json.Marshal(req.Data)
if err != nil {
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)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment