From 72a6625c143105b480a420236abddd44e4fbe4d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Art=D1=91m=20Mishin?= Date: Fri, 25 Apr 2025 12:29:46 +0300 Subject: [PATCH] feat: KeyStoreStringToBinary --- .idea/vcs.xml | 4 ++- go.mod | 2 ++ go.sum | 2 ++ main.go | 13 +++++++++ string2bin.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 go.sum create mode 100644 string2bin.go diff --git a/.idea/vcs.xml b/.idea/vcs.xml index d843f34..94a25f7 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,4 +1,6 @@ - + + + \ No newline at end of file diff --git a/go.mod b/go.mod index 326fc5f..c0fa9e7 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module universe.mrlokop.dev/key_value go 1.24 + +require universe.mrlokop.dev/core v0.0.0-20250425091205-db1d0545d9c6 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2d15feb --- /dev/null +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index 0d34244..bab7413 100644 --- a/main.go +++ b/main.go @@ -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 } diff --git a/string2bin.go b/string2bin.go new file mode 100644 index 0000000..46bb8cf --- /dev/null +++ b/string2bin.go @@ -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, + } +}