-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk/go): Refactor the key/value Go SDK to be more idiomatic
- Introduces a client struct responsible for handling the connection - Remove exposed C types Signed-off-by: Adam Reese <[email protected]>
- Loading branch information
Showing
6 changed files
with
149 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Package kv provides access to key value stores within Spin | ||
// components. | ||
package kv | ||
|
||
// #include "key-value.h" | ||
import "C" | ||
import ( | ||
"errors" | ||
"fmt" | ||
"unsafe" | ||
) | ||
|
||
// Store is the Key/Value backend storage. | ||
type Store struct { | ||
name string | ||
active bool | ||
ptr C.key_value_store_t | ||
} | ||
|
||
// NewStore creates a new instance of Store. | ||
func NewStore(name string) *Store { | ||
return &Store{name: name} | ||
} | ||
|
||
// Open establishes a connection to the key/value storage. | ||
func (s *Store) Open() error { | ||
return s.open() | ||
} | ||
|
||
// Close terminates the connection to Store. | ||
func (s *Store) Close() { | ||
if s.active { | ||
C.key_value_close(C.uint32_t(s.ptr)) | ||
} | ||
s.active = false | ||
} | ||
|
||
// Get retrieves a value from Store. | ||
func (s *Store) Get(key string) ([]byte, error) { | ||
ckey := toCStr(key) | ||
var ret C.key_value_expected_list_u8_error_t | ||
C.key_value_get(C.uint32_t(s.ptr), &ckey, &ret) | ||
if ret.is_err { | ||
return nil, toErr((*C.key_value_error_t)(unsafe.Pointer(&ret.val))) | ||
} | ||
list := (*C.key_value_list_u8_t)(unsafe.Pointer(&ret.val)) | ||
return C.GoBytes(unsafe.Pointer(list.ptr), C.int(list.len)), nil | ||
} | ||
|
||
// Delete removes a value from Store. | ||
func (s *Store) Delete(key string) error { | ||
ckey := toCStr(key) | ||
var ret C.key_value_expected_unit_error_t | ||
C.key_value_delete(C.uint32_t(s.ptr), &ckey, &ret) | ||
if ret.is_err { | ||
return toErr((*C.key_value_error_t)(unsafe.Pointer(&ret.val))) | ||
} | ||
return nil | ||
} | ||
|
||
// Set creates a new key/value in Store. | ||
func (s *Store) Set(key string, value []byte) error { | ||
ckey := toCStr(key) | ||
cbytes := toCBytes(value) | ||
var ret C.key_value_expected_unit_error_t | ||
C.key_value_set(C.uint32_t(s.ptr), &ckey, &cbytes, &ret) | ||
if ret.is_err { | ||
return toErr((*C.key_value_error_t)(unsafe.Pointer(&ret.val))) | ||
} | ||
return nil | ||
} | ||
|
||
// Exists checks if a key exists within Store. | ||
func (s *Store) Exists(key string) (bool, error) { | ||
ckey := toCStr(key) | ||
var ret C.key_value_expected_bool_error_t | ||
C.key_value_exists(C.uint32_t(s.ptr), &ckey, &ret) | ||
if ret.is_err { | ||
return false, toErr((*C.key_value_error_t)(unsafe.Pointer(&ret.val))) | ||
} | ||
return *(*bool)(unsafe.Pointer(&ret.val)), nil | ||
} | ||
|
||
func (s *Store) open() error { | ||
if s.active { | ||
return nil | ||
} | ||
cname := toCStr(s.name) | ||
var ret C.key_value_expected_store_error_t | ||
C.key_value_open(&cname, &ret) | ||
if ret.is_err { | ||
return toErr((*C.key_value_error_t)(unsafe.Pointer(&ret.val))) | ||
} | ||
s.ptr = *(*C.key_value_store_t)(unsafe.Pointer(&ret.val)) | ||
s.active = true | ||
return nil | ||
} | ||
|
||
func toCBytes(x []byte) C.key_value_list_u8_t { | ||
return C.key_value_list_u8_t{ptr: (*C.uint8_t)(unsafe.Pointer(&x[0])), len: C.size_t(len(x))} | ||
} | ||
|
||
func toCStr(x string) C.key_value_string_t { | ||
return C.key_value_string_t{ptr: C.CString(x), len: C.size_t(len(x))} | ||
} | ||
|
||
func fromCStrList(list *C.key_value_list_string_t) []string { | ||
var result []string | ||
|
||
listLen := int(list.len) | ||
slice := unsafe.Slice(list.ptr, listLen) | ||
for i := 0; i < listLen; i++ { | ||
str := slice[i] | ||
result = append(result, C.GoStringN(str.ptr, C.int(str.len))) | ||
} | ||
|
||
return result | ||
} | ||
|
||
func toErr(err *C.key_value_error_t) error { | ||
switch err.tag { | ||
case 0: | ||
return errors.New("store table full") | ||
case 1: | ||
return errors.New("no such store") | ||
case 2: | ||
return errors.New("access denied") | ||
case 3: | ||
return errors.New("invalid store") | ||
case 4: | ||
return errors.New("no such key") | ||
case 5: | ||
str := (*C.key_value_string_t)(unsafe.Pointer(&err.val)) | ||
return fmt.Errorf("io error: %s", C.GoStringN(str.ptr, C.int(str.len))) | ||
default: | ||
return fmt.Errorf("unrecognized error: %v", err.tag) | ||
} | ||
} |