Skip to content

Commit

Permalink
update cache typing
Browse files Browse the repository at this point in the history
  • Loading branch information
ochom committed Jan 13, 2024
1 parent c2b033a commit 7f5b9ad
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 33 deletions.
23 changes: 19 additions & 4 deletions cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cache

import (
"fmt"
"runtime"
"time"

Expand Down Expand Up @@ -35,13 +36,27 @@ func initCache(host string, port int, username, password string, database int) *
}

// Set a key-value pair in the cache
func Set(key string, value []byte, exp time.Duration) error {
return store.Set(key, value, exp)
func Set[T any](key string, value *T, exp time.Duration) error {
return store.Set(key, helpers.ToBytes(value), exp)
}

// Get a key from the cache
func Get(key string) ([]byte, error) {
return store.Get(key)
func Get[T any](key string) (v *T, err error) {
val, err := store.Get(key)
if err != nil {
return v, err
}

if len(val) == 0 {
return v, fmt.Errorf("key %s not found", key)
}

v, err = helpers.FromBytes[*T](val)
if err != nil {
return v, err
}

return v, nil
}

// Has a key in the cache
Expand Down
6 changes: 4 additions & 2 deletions gttp/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io"
"net/http"
"time"

"github.com/ochom/gutils/helpers"
)

// Response is the response of the request.
Expand Down Expand Up @@ -52,7 +54,7 @@ func getTimeout(timeout ...time.Duration) time.Duration {

// Post sends a POST request to the specified URL.
func Post(url string, headers M, body any, timeout ...time.Duration) (res *Response, err error) {
req, err := http.NewRequest("POST", url, bytes.NewBuffer(toBytes(body)))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(helpers.ToBytes(body)))
if err != nil {
return
}
Expand Down Expand Up @@ -110,7 +112,7 @@ func Get(url string, headers M, timeout ...time.Duration) (res *Response, err er

// Custom sends a custom request to the specified URL.
func Custom(url, method string, headers M, body any, timeout ...time.Duration) (res *Response, err error) {
req, err := http.NewRequest(method, url, bytes.NewBuffer(toBytes(body)))
req, err := http.NewRequest(method, url, bytes.NewBuffer(helpers.ToBytes(body)))
if err != nil {
return
}
Expand Down
27 changes: 0 additions & 27 deletions gttp/types.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,7 @@
package gttp

import "encoding/json"

// M is a map[string]string
type M map[string]string

// Map is a map[string]interface{}
type Map map[string]interface{}

// toBytes ...
func toBytes(payload any) []byte {
if payload == nil {
return nil
}

// Check if the payload is already of type []byte.
if bytesPayload, ok := payload.([]byte); ok {
return bytesPayload
}

// Check if the payload is already of type string.
if stringPayload, ok := payload.(string); ok {
return []byte(stringPayload)
}

// Marshal the payload to JSON.
bytesPayload, err := json.Marshal(payload)
if err != nil {
return nil
}

return bytesPayload
}
43 changes: 43 additions & 0 deletions helpers/bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package helpers

import "encoding/json"

// toBytes ...
func ToBytes(payload any) []byte {
if payload == nil {
return nil
}

// Check if the payload is already of type []byte.
if bytesPayload, ok := payload.([]byte); ok {
return bytesPayload
}

// Check if the payload is already of type string.
if stringPayload, ok := payload.(*string); ok {
return []byte(*stringPayload)
}

// Marshal the payload to JSON.
bytesPayload, err := json.Marshal(payload)
if err != nil {
return nil
}

return bytesPayload
}

// FromBytes ...
func FromBytes[T any](payload []byte) (T, error) {
var data T
if payload == nil {
return data, nil
}

err := json.Unmarshal(payload, &data)
if err != nil {
return data, err
}

return data, nil
}

0 comments on commit 7f5b9ad

Please sign in to comment.