Skip to content

Commit

Permalink
Merge pull request #34 from fastly/dm/rename_object_store_to_kv_store
Browse files Browse the repository at this point in the history
  • Loading branch information
joeshaw authored May 15, 2023
2 parents e600cde + 28fe9ed commit 9d3171e
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 94 deletions.
6 changes: 3 additions & 3 deletions _examples/objectstore/main.go → _examples/kvstore/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"strings"

"github.com/fastly/compute-sdk-go/fsthttp"
"github.com/fastly/compute-sdk-go/objectstore"
"github.com/fastly/compute-sdk-go/kvstore"
)

func main() {
fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
o, err := objectstore.Open("example_objectstore")
o, err := kvstore.Open("example_kvstore")
if err != nil {
fsthttp.Error(w, err.Error(), fsthttp.StatusBadGateway)
return
Expand All @@ -31,7 +31,7 @@ func main() {
// We can detect when a key does not exist and supply a default value instead.
var reader io.Reader
v, err = o.Lookup("might-not-exist")
if err != nil && err == objectstore.ErrKeyNotFound {
if err != nil && err == kvstore.ErrKeyNotFound {
reader = strings.NewReader("default value")
} else if err != nil {
fsthttp.Error(w, err.Error(), fsthttp.StatusBadGateway)
Expand Down
50 changes: 25 additions & 25 deletions internal/abi/fastly/hostcalls_guest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2051,31 +2051,31 @@ func GeoLookup(ip net.IP) ([]byte, error) {

// witx:
//
// (module $fastly_object_store
// (module $fastly_kv_store
// (@interface func (export "open")
// (param $name string)
// (result $err (expected $object_store_handle (error $fastly_status)))
// (result $err (expected $kv_store_handle (error $fastly_status)))
// )

//go:wasm-module fastly_object_store
//go:wasm-module fastly_kv_store
//export open
//go:noescape
func fastlyObjectStoreOpen(
func fastlyKVStoreOpen(
name prim.Wstring,
h *objectStoreHandle,
h *kvStoreHandle,
) FastlyStatus

// ObjectStore represents a Fastly object store, a collection of key/value pairs.
// KVStore represents a Fastly kv store, a collection of key/value pairs.
// For convenience, keys and values are both modelled as Go strings.
type ObjectStore struct {
h objectStoreHandle
type KVStore struct {
h kvStoreHandle
}

// ObjectStoreOpen returns a reference to the named object store, if it exists.
func OpenObjectStore(name string) (*ObjectStore, error) {
var o ObjectStore
// KVStoreOpen returns a reference to the named kv store, if it exists.
func OpenKVStore(name string) (*KVStore, error) {
var o KVStore

if err := fastlyObjectStoreOpen(
if err := fastlyKVStoreOpen(
prim.NewReadBufferFromString(name).Wstring(),
&o.h,
).toError(); err != nil {
Expand All @@ -2088,26 +2088,26 @@ func OpenObjectStore(name string) (*ObjectStore, error) {
// witx:
//
// (@interface func (export "lookup")
// (param $store $object_store_handle)
// (param $store $kv_store_handle)
// (param $key string)
// (param $body_handle_out (@witx pointer $body_handle))
// (result $err (expected (error $fastly_status)))
// )

//go:wasm-module fastly_object_store
//go:wasm-module fastly_kv_store
//export lookup
//go:noescape
func fastlyObjectStoreLookup(
h objectStoreHandle,
func fastlyKVStoreLookup(
h kvStoreHandle,
key prim.Wstring,
b *bodyHandle,
) FastlyStatus

// Lookup returns the value for key, if it exists.
func (o *ObjectStore) Lookup(key string) (io.Reader, error) {
func (o *KVStore) Lookup(key string) (io.Reader, error) {
body := HTTPBody{h: invalidBodyHandle}

if err := fastlyObjectStoreLookup(
if err := fastlyKVStoreLookup(
o.h,
prim.NewReadBufferFromString(key).Wstring(),
&body.h,
Expand All @@ -2127,23 +2127,23 @@ func (o *ObjectStore) Lookup(key string) (io.Reader, error) {
// witx:
//
// (@interface func (export "insert")
// (param $store $object_store_handle)
// (param $store $kv_store_handle)
// (param $key string)
// (param $body_handle $body_handle)
// (result $err (expected (error $fastly_status)))
// )

//go:wasm-module fastly_object_store
//go:wasm-module fastly_kv_store
//export insert
//go:noescape
func fastlyObjectStoreInsert(
h objectStoreHandle,
func fastlyKVStoreInsert(
h kvStoreHandle,
key prim.Wstring,
b bodyHandle,
) FastlyStatus

// Insert adds a key/value pair to the object store.
func (o *ObjectStore) Insert(key string, value io.Reader) error {
// Insert adds a key/value pair to the kv store.
func (o *KVStore) Insert(key string, value io.Reader) error {
body, err := NewHTTPBody()
if err != nil {
return err
Expand All @@ -2153,7 +2153,7 @@ func (o *ObjectStore) Insert(key string, value io.Reader) error {
return err
}

if err := fastlyObjectStoreInsert(
if err := fastlyKVStoreInsert(
o.h,
prim.NewReadBufferFromString(key).Wstring(),
body.h,
Expand Down
8 changes: 4 additions & 4 deletions internal/abi/fastly/hostcalls_noguest.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,17 @@ func GeoLookup(ip net.IP) ([]byte, error) {
return nil, fmt.Errorf("not implemented")
}

type ObjectStore struct{}
type KVStore struct{}

func OpenObjectStore(name string) (*ObjectStore, error) {
func OpenKVStore(name string) (*KVStore, error) {
return nil, fmt.Errorf("not implemented")
}

func (o *ObjectStore) Lookup(key string) (io.Reader, error) {
func (o *KVStore) Lookup(key string) (io.Reader, error) {
return nil, fmt.Errorf("not implemented")
}

func (o *ObjectStore) Insert(key string, value io.Reader) error {
func (o *KVStore) Insert(key string, value io.Reader) error {
return fmt.Errorf("not implemented")
}

Expand Down
4 changes: 2 additions & 2 deletions internal/abi/fastly/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,8 @@ const (

// witx:
//
// (typename $object_store_handle (handle))
type objectStoreHandle handle
// (typename $kv_store_handle (handle))
type kvStoreHandle handle

// witx:
//
Expand Down
71 changes: 71 additions & 0 deletions kvstore/kvstore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package kvstore

import (
"errors"
"io"

"github.com/fastly/compute-sdk-go/internal/abi/fastly"
)

var ErrKeyNotFound = errors.New("kvstore: key not found")

type Entry struct {
io.Reader

validString bool
s string
}

func (e *Entry) String() string {
if e.validString {
return e.s
}

// TODO(dgryski): replace with StringBuilder + io.Copy ?
b, err := io.ReadAll(e)
if err != nil {
return ""
}

e.s = string(b)
e.validString = true
return e.s
}

// Store represents a Fastly kv store
type Store struct {
kvstore *fastly.KVStore
}

// Open returns a handle to the named kv store
func Open(name string) (*Store, error) {
o, err := fastly.OpenKVStore(name)
if err != nil {
return nil, err
}

return &Store{kvstore: o}, nil
}

// Lookup fetches a key from the associated kv store. If the key does not
// exist, Lookup returns the sentinel error ErrKeyNotFound.
func (s *Store) Lookup(key string) (*Entry, error) {
val, err := s.kvstore.Lookup(key)
if err != nil {

// turn FastlyStatusNone into NotFound
if status, ok := fastly.IsFastlyError(err); ok && status == fastly.FastlyStatusNone {
return nil, ErrKeyNotFound
}

return nil, err
}

return &Entry{Reader: val}, err
}

// Insert adds a key to the associated kv store.
func (s *Store) Insert(key string, value io.Reader) error {
err := s.kvstore.Insert(key, value)
return err
}
2 changes: 2 additions & 0 deletions objectstore/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Deprecated: Use the kvstore package instead.
package objectstore
70 changes: 10 additions & 60 deletions objectstore/objectstore.go
Original file line number Diff line number Diff line change
@@ -1,72 +1,22 @@
package objectstore

import (
"errors"
"io"

"github.com/fastly/compute-sdk-go/internal/abi/fastly"
"github.com/fastly/compute-sdk-go/kvstore"
)

var ErrKeyNotFound = errors.New("objectstore: key not found")

type Entry struct {
io.Reader

validString bool
s string
}

func (e *Entry) String() string {
if e.validString {
return e.s
}
var ErrKeyNotFound = kvstore.ErrKeyNotFound

// TODO(dgryski): replace with StringBuilder + io.Copy ?
b, err := io.ReadAll(e)
if err != nil {
return ""
}

e.validString = true

return string(b)

}
// Deprecated: Use the kvstore package instead.
type Entry = kvstore.Entry

// Store represents a Fastly object store
type Store struct {
objectstore *fastly.ObjectStore
}
//
// Deprecated: Use the kvstore package instead.
type Store = kvstore.Store

// Open returns a handle to the named object store
//
// Deprecated: Use kvstore.Open() instead.
func Open(name string) (*Store, error) {
o, err := fastly.OpenObjectStore(name)
if err != nil {
return nil, err
}

return &Store{objectstore: o}, nil
}

// Lookup fetches a key from the associated object store. If the key does not
// exist, Lookup returns the sentinel error ErrKeyNotFound.
func (s *Store) Lookup(key string) (*Entry, error) {
val, err := s.objectstore.Lookup(key)
if err != nil {

// turn FastlyStatusNone into NotFound
if status, ok := fastly.IsFastlyError(err); ok && status == fastly.FastlyStatusNone {
return nil, ErrKeyNotFound
}

return nil, err
}

return &Entry{Reader: val}, err
}

// Insert adds a key to the associated object store.
func (s *Store) Insert(key string, value io.Reader) error {
err := s.objectstore.Insert(key, value)
return err
return kvstore.Open(name)
}
1 change: 1 addition & 0 deletions semgrep/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ This is a set of static analysis patterns for [semgrep](https://semgrep.dev).
# List of rules

- edgedict: warn and migrate from the edgedict API to the configstore API
- objectstore: warn and migrate from objectstore API to the kvstore API
21 changes: 21 additions & 0 deletions semgrep/objectstore.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
rules:
- id: deprecated-objectstore-store
pattern: objectstore.Store
fix: kvstore.Store
message: objectstore package is deprecated
languages: [go]
severity: ERROR

- id: deprecated-objectstore-open
pattern: objectstore.Open($N)
fix: kvstore.Open($N)
message: objectstore package is deprecated
languages: [go]
severity: ERROR

- id: deprecated-objectstore-errkeynotfound
pattern: objectstore.ErrKeyNotFound
fix: kvstore.ErrKeyNotFound
message: objectstore package is deprecated
languages: [go]
severity: ERROR

0 comments on commit 9d3171e

Please sign in to comment.