feat: KeyStoreStringToBinary
This commit is contained in:
1 parent
1d08501b51
commit
72a6625c14
5 files changed
+94
-1
No files matched your search
Generated
+3
-1
@@ -1,4 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="VcsDirectoryMappings" defaultProject="true" />
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
module universe.mrlokop.dev/key_value
|
module universe.mrlokop.dev/key_value
|
||||||
|
|
||||||
go 1.24
|
go 1.24
|
||||||
|
|
||||||
|
require universe.mrlokop.dev/core v0.0.0-20250425091205-db1d0545d9c6
|
||||||
@@ -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=
|
||||||
@@ -3,6 +3,11 @@ package universe_key_value
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
"universe.mrlokop.dev/core/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrNoSuchKey = errors.Type("ErrNoSuchKey", "no such key")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Capability uint8
|
type Capability uint8
|
||||||
@@ -22,8 +27,16 @@ type KeyValueStore[T any] interface {
|
|||||||
HasCapability(capability Capability) bool
|
HasCapability(capability Capability) bool
|
||||||
|
|
||||||
Get(ctx context.Context, key string) (T, error)
|
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
|
Set(ctx context.Context, key string, value T) error
|
||||||
SetWithExpiration(ctx context.Context, key string, value T, expiresAt time.Time) 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
|
Remove(ctx context.Context, key string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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,
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user