|
| 1 | +package kv |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + keyvalue "github.com/spinframework/spin-go-sdk/v2/internal/fermyon/spin/v2.0.0/key-value" |
| 7 | + "go.bytecodealliance.org/cm" |
| 8 | +) |
| 9 | + |
| 10 | +type Store struct { |
| 11 | + store *keyvalue.Store |
| 12 | +} |
| 13 | + |
| 14 | +// Open the store with the label. |
| 15 | +func Open(label string) (*Store, error) { |
| 16 | + result := keyvalue.StoreOpen(label) |
| 17 | + if result.IsErr() { |
| 18 | + return nil, errorVariantToError(*result.Err()) |
| 19 | + } |
| 20 | + |
| 21 | + return &Store{ |
| 22 | + store: result.OK(), |
| 23 | + }, nil |
| 24 | +} |
| 25 | + |
| 26 | +// Open the default store. |
| 27 | +// |
| 28 | +// This is equivalent to `kv.Open("default")`. |
| 29 | +func OpenDefault() (*Store, error) { |
| 30 | + return Open("default") |
| 31 | +} |
| 32 | + |
| 33 | +// Set the key/value pair in store |
| 34 | +func (s *Store) Set(key string, value []byte) error { |
| 35 | + result := s.store.Set(key, cm.ToList(value)) |
| 36 | + if result.IsErr() { |
| 37 | + return errorVariantToError(*result.Err()) |
| 38 | + } |
| 39 | + |
| 40 | + return nil |
| 41 | +} |
| 42 | + |
| 43 | +// Get the value of provided key from the store |
| 44 | +func (s *Store) Get(key string) ([]byte, error) { |
| 45 | + result := s.store.Get(key) |
| 46 | + if result.IsErr() { |
| 47 | + return nil, errorVariantToError(*result.Err()) |
| 48 | + } |
| 49 | + |
| 50 | + value := result.OK() |
| 51 | + if value.None() { |
| 52 | + return []byte(""), nil |
| 53 | + } |
| 54 | + |
| 55 | + return value.Some().Slice(), nil |
| 56 | +} |
| 57 | + |
| 58 | +// Delete the given key/value from the store |
| 59 | +func (s *Store) Delete(key string) error { |
| 60 | + result := s.store.Delete(key) |
| 61 | + if result.IsErr() { |
| 62 | + return errorVariantToError(*result.Err()) |
| 63 | + } |
| 64 | + |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +// Exists check if a given key exist in the store |
| 69 | +func (s *Store) Exists(key string) (bool, error) { |
| 70 | + result := s.store.Exists(key) |
| 71 | + if result.IsErr() { |
| 72 | + return false, errorVariantToError(*result.Err()) |
| 73 | + } |
| 74 | + |
| 75 | + return *result.OK(), nil |
| 76 | +} |
| 77 | + |
| 78 | +// GetKets returns all the keys from the store |
| 79 | +func (s *Store) GetKeys() ([]string, error) { |
| 80 | + result := s.store.GetKeys() |
| 81 | + if result.IsErr() { |
| 82 | + return nil, errorVariantToError(*result.Err()) |
| 83 | + } |
| 84 | + |
| 85 | + return result.OK().Slice(), nil |
| 86 | +} |
| 87 | + |
| 88 | +func errorVariantToError(code keyvalue.Error) error { |
| 89 | + switch code { |
| 90 | + case keyvalue.ErrorAccessDenied(): |
| 91 | + return fmt.Errorf("access denied") |
| 92 | + case keyvalue.ErrorNoSuchStore(): |
| 93 | + return fmt.Errorf("no such store") |
| 94 | + case keyvalue.ErrorStoreTableFull(): |
| 95 | + return fmt.Errorf("store table full") |
| 96 | + default: |
| 97 | + if code.Other() != nil { |
| 98 | + return fmt.Errorf(*code.Other()) |
| 99 | + } |
| 100 | + |
| 101 | + return fmt.Errorf("no error provided by host implementation") |
| 102 | + } |
| 103 | +} |
0 commit comments