add func get value by key with binding

This commit is contained in:
Nam Huynh 2022-12-05 15:16:42 +07:00
parent 2e747ff26b
commit 5d14b794dd
1 changed files with 5 additions and 7 deletions

View File

@ -3,6 +3,7 @@ package redisdb
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
) )
// GetValueByKey ... // GetValueByKey ...
@ -15,14 +16,11 @@ func (s Server) GetValueByKey(ctx context.Context, key string) string {
return value return value
} }
// GetJSON ... // GetValueByKeyWithBinding ...
func (s Server) GetJSON(ctx context.Context, key string, result interface{}) (ok bool) { func (s Server) GetValueByKeyWithBinding(ctx context.Context, key string, result interface{}) error {
v := s.GetValueByKey(ctx, key) v := s.GetValueByKey(ctx, key)
if v == "" { if v == "" {
return false return errors.New("key not found")
} }
if err := json.Unmarshal([]byte(v), result); err != nil { return json.Unmarshal([]byte(v), result)
return false
}
return true
} }