feat: KeyStoreStringToBinary

This commit is contained in:
Artёm Mishin committed 2025-04-25 12:29:46 +03:00
1 parent 1d08501b51
commit 72a6625c14
5 files changed
+94 -1

No files matched your search

Generated
+3 -1
View File
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings" defaultProject="true" />
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
+2
View File
@@ -1,3 +1,5 @@
module universe.mrlokop.dev/key_value
go 1.24
require universe.mrlokop.dev/core v0.0.0-20250425091205-db1d0545d9c6
+2
View File
@@ -0,0 +1,2 @@
universe.mrlokop.dev/core v0.0.0-20250425091205-db1d0545d9c6 h1:aM0o2QFSizLY9KOXuoOmsp/o345yJEptgq8egA85VnM=
universe.mrlokop.dev/core v0.0.0-20250425091205-db1d0545d9c6/go.mod h1:1xEErU5FKve38at9zd2RZDDbojuebd9rGpWzeUD4zu4=
+13
View File
@@ -3,6 +3,11 @@ package universe_key_value
import (
"context"
"time"
"universe.mrlokop.dev/core/errors"
)
var (
ErrNoSuchKey = errors.Type("ErrNoSuchKey", "no such key")
)
type Capability uint8
@@ -22,8 +27,16 @@ type KeyValueStore[T any] interface {
HasCapability(capability Capability) bool
Get(ctx context.Context, key string) (T, error)
Has(ctx context.Context, key string) (bool, error)
Set(ctx context.Context, key string, value T) error
SetWithExpiration(ctx context.Context, key string, value T, expiresAt time.Time) error
SetIfNotExist(ctx context.Context, key string, value T) (bool, error)
SetIfNotExistWithExpiration(ctx context.Context, key string, value T, expiration time.Time) (bool, error)
ExpireAt(ctx context.Context, key string, expiration time.Time) error
Remove(ctx context.Context, key string) error
}
+74
View File
@@ -0,0 +1,74 @@
package universe_key_value
import (
"context"
"time"
)
type KeyStoreStringToBinary struct {
binaryStore KeyValueStoreBinary
}
func (k *KeyStoreStringToBinary) Has(ctx context.Context, key string) (bool, error) {
value, err := k.binaryStore.Has(ctx, key)
if err != nil {
return false, err
}
return value, nil
}
func (k *KeyStoreStringToBinary) SetIfNotExist(ctx context.Context, key string, value string) (bool, error) {
valueBytes := []byte(value)
set, err := k.binaryStore.SetIfNotExist(ctx, key, valueBytes)
if err != nil {
return false, err
}
return set, nil
}
func (k *KeyStoreStringToBinary) SetIfNotExistWithExpiration(ctx context.Context, key string, value string, expiration time.Time) (bool, error) {
valueBytes := []byte(value)
set, err := k.binaryStore.SetIfNotExistWithExpiration(ctx, key, valueBytes, expiration)
if err != nil {
return false, err
}
return set, nil
}
func (k *KeyStoreStringToBinary) ExpireAt(ctx context.Context, key string, expiration time.Time) error {
return k.binaryStore.ExpireAt(ctx, key, expiration)
}
func (k *KeyStoreStringToBinary) HasCapability(capability Capability) bool {
return k.binaryStore.HasCapability(capability)
}
func (k *KeyStoreStringToBinary) Get(ctx context.Context, key string) (string, error) {
value, err := k.binaryStore.Get(ctx, key)
if err != nil {
return "", err
}
return string(value), nil
}
func (k *KeyStoreStringToBinary) Set(ctx context.Context, key string, value string) error {
return k.binaryStore.Set(ctx, key, []byte(value))
}
func (k *KeyStoreStringToBinary) SetWithExpiration(ctx context.Context, key string, value string, expiresAt time.Time) error {
return k.binaryStore.SetWithExpiration(ctx, key, []byte(value), expiresAt)
}
func (k *KeyStoreStringToBinary) Remove(ctx context.Context, key string) error {
return k.binaryStore.Remove(ctx, key)
}
func WrapStringToBinary(binaryStore KeyValueStoreBinary) KeyValueStoreString {
if binaryStore == nil {
return nil
}
return &KeyStoreStringToBinary{
binaryStore: binaryStore,
}
}