initial commit
This commit is contained in:
48
pkg/store/utils.go
Normal file
48
pkg/store/utils.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// serializeValue converts a value to a string format suitable for Redis hash storage
|
||||
func serializeValue[T any](value any) (T, error) {
|
||||
var t T
|
||||
|
||||
if value == nil {
|
||||
return t, nil
|
||||
}
|
||||
|
||||
if val, ok := value.(T); ok {
|
||||
return val, nil
|
||||
}
|
||||
|
||||
val, ok := value.(string)
|
||||
if !ok {
|
||||
return t, fmt.Errorf("invalid type %T", value)
|
||||
}
|
||||
|
||||
unmarshalErr := json.Unmarshal([]byte(val), &t)
|
||||
if unmarshalErr != nil {
|
||||
return t, unmarshalErr
|
||||
}
|
||||
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// extractKeyPattern extracts the appropriate key pattern for metrics
|
||||
// Handles both 2-part (prefix:hash) and 3-part (prefix:service:hash) keys
|
||||
func extractKeyPattern(key string) (string, error) {
|
||||
keyPattern := strings.Split(key, ":")
|
||||
if len(keyPattern) < 2 {
|
||||
return "", fmt.Errorf("invalid key: %s", key)
|
||||
}
|
||||
|
||||
// For 2-part keys (prefix:hash), use the prefix
|
||||
// For 3-part keys (prefix:service:hash), use the service name
|
||||
if len(keyPattern) == 2 {
|
||||
return keyPattern[0], nil // prefix
|
||||
}
|
||||
return keyPattern[1], nil // service
|
||||
}
|
||||
Reference in New Issue
Block a user