Skip to content

Commit 9d3171e

Browse files
authored
Merge pull request #34 from fastly/dm/rename_object_store_to_kv_store
2 parents e600cde + 28fe9ed commit 9d3171e

File tree

9 files changed

+139
-94
lines changed

9 files changed

+139
-94
lines changed

_examples/objectstore/main.go renamed to _examples/kvstore/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88
"strings"
99

1010
"github.com/fastly/compute-sdk-go/fsthttp"
11-
"github.com/fastly/compute-sdk-go/objectstore"
11+
"github.com/fastly/compute-sdk-go/kvstore"
1212
)
1313

1414
func main() {
1515
fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
16-
o, err := objectstore.Open("example_objectstore")
16+
o, err := kvstore.Open("example_kvstore")
1717
if err != nil {
1818
fsthttp.Error(w, err.Error(), fsthttp.StatusBadGateway)
1919
return
@@ -31,7 +31,7 @@ func main() {
3131
// We can detect when a key does not exist and supply a default value instead.
3232
var reader io.Reader
3333
v, err = o.Lookup("might-not-exist")
34-
if err != nil && err == objectstore.ErrKeyNotFound {
34+
if err != nil && err == kvstore.ErrKeyNotFound {
3535
reader = strings.NewReader("default value")
3636
} else if err != nil {
3737
fsthttp.Error(w, err.Error(), fsthttp.StatusBadGateway)

internal/abi/fastly/hostcalls_guest.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,31 +2051,31 @@ func GeoLookup(ip net.IP) ([]byte, error) {
20512051

20522052
// witx:
20532053
//
2054-
// (module $fastly_object_store
2054+
// (module $fastly_kv_store
20552055
// (@interface func (export "open")
20562056
// (param $name string)
2057-
// (result $err (expected $object_store_handle (error $fastly_status)))
2057+
// (result $err (expected $kv_store_handle (error $fastly_status)))
20582058
// )
20592059

2060-
//go:wasm-module fastly_object_store
2060+
//go:wasm-module fastly_kv_store
20612061
//export open
20622062
//go:noescape
2063-
func fastlyObjectStoreOpen(
2063+
func fastlyKVStoreOpen(
20642064
name prim.Wstring,
2065-
h *objectStoreHandle,
2065+
h *kvStoreHandle,
20662066
) FastlyStatus
20672067

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

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

2078-
if err := fastlyObjectStoreOpen(
2078+
if err := fastlyKVStoreOpen(
20792079
prim.NewReadBufferFromString(name).Wstring(),
20802080
&o.h,
20812081
).toError(); err != nil {
@@ -2088,26 +2088,26 @@ func OpenObjectStore(name string) (*ObjectStore, error) {
20882088
// witx:
20892089
//
20902090
// (@interface func (export "lookup")
2091-
// (param $store $object_store_handle)
2091+
// (param $store $kv_store_handle)
20922092
// (param $key string)
20932093
// (param $body_handle_out (@witx pointer $body_handle))
20942094
// (result $err (expected (error $fastly_status)))
20952095
// )
20962096

2097-
//go:wasm-module fastly_object_store
2097+
//go:wasm-module fastly_kv_store
20982098
//export lookup
20992099
//go:noescape
2100-
func fastlyObjectStoreLookup(
2101-
h objectStoreHandle,
2100+
func fastlyKVStoreLookup(
2101+
h kvStoreHandle,
21022102
key prim.Wstring,
21032103
b *bodyHandle,
21042104
) FastlyStatus
21052105

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

2110-
if err := fastlyObjectStoreLookup(
2110+
if err := fastlyKVStoreLookup(
21112111
o.h,
21122112
prim.NewReadBufferFromString(key).Wstring(),
21132113
&body.h,
@@ -2127,23 +2127,23 @@ func (o *ObjectStore) Lookup(key string) (io.Reader, error) {
21272127
// witx:
21282128
//
21292129
// (@interface func (export "insert")
2130-
// (param $store $object_store_handle)
2130+
// (param $store $kv_store_handle)
21312131
// (param $key string)
21322132
// (param $body_handle $body_handle)
21332133
// (result $err (expected (error $fastly_status)))
21342134
// )
21352135

2136-
//go:wasm-module fastly_object_store
2136+
//go:wasm-module fastly_kv_store
21372137
//export insert
21382138
//go:noescape
2139-
func fastlyObjectStoreInsert(
2140-
h objectStoreHandle,
2139+
func fastlyKVStoreInsert(
2140+
h kvStoreHandle,
21412141
key prim.Wstring,
21422142
b bodyHandle,
21432143
) FastlyStatus
21442144

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

2156-
if err := fastlyObjectStoreInsert(
2156+
if err := fastlyKVStoreInsert(
21572157
o.h,
21582158
prim.NewReadBufferFromString(key).Wstring(),
21592159
body.h,

internal/abi/fastly/hostcalls_noguest.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,17 +245,17 @@ func GeoLookup(ip net.IP) ([]byte, error) {
245245
return nil, fmt.Errorf("not implemented")
246246
}
247247

248-
type ObjectStore struct{}
248+
type KVStore struct{}
249249

250-
func OpenObjectStore(name string) (*ObjectStore, error) {
250+
func OpenKVStore(name string) (*KVStore, error) {
251251
return nil, fmt.Errorf("not implemented")
252252
}
253253

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

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

internal/abi/fastly/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,8 @@ const (
465465

466466
// witx:
467467
//
468-
// (typename $object_store_handle (handle))
469-
type objectStoreHandle handle
468+
// (typename $kv_store_handle (handle))
469+
type kvStoreHandle handle
470470

471471
// witx:
472472
//

kvstore/kvstore.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package kvstore
2+
3+
import (
4+
"errors"
5+
"io"
6+
7+
"github.com/fastly/compute-sdk-go/internal/abi/fastly"
8+
)
9+
10+
var ErrKeyNotFound = errors.New("kvstore: key not found")
11+
12+
type Entry struct {
13+
io.Reader
14+
15+
validString bool
16+
s string
17+
}
18+
19+
func (e *Entry) String() string {
20+
if e.validString {
21+
return e.s
22+
}
23+
24+
// TODO(dgryski): replace with StringBuilder + io.Copy ?
25+
b, err := io.ReadAll(e)
26+
if err != nil {
27+
return ""
28+
}
29+
30+
e.s = string(b)
31+
e.validString = true
32+
return e.s
33+
}
34+
35+
// Store represents a Fastly kv store
36+
type Store struct {
37+
kvstore *fastly.KVStore
38+
}
39+
40+
// Open returns a handle to the named kv store
41+
func Open(name string) (*Store, error) {
42+
o, err := fastly.OpenKVStore(name)
43+
if err != nil {
44+
return nil, err
45+
}
46+
47+
return &Store{kvstore: o}, nil
48+
}
49+
50+
// Lookup fetches a key from the associated kv store. If the key does not
51+
// exist, Lookup returns the sentinel error ErrKeyNotFound.
52+
func (s *Store) Lookup(key string) (*Entry, error) {
53+
val, err := s.kvstore.Lookup(key)
54+
if err != nil {
55+
56+
// turn FastlyStatusNone into NotFound
57+
if status, ok := fastly.IsFastlyError(err); ok && status == fastly.FastlyStatusNone {
58+
return nil, ErrKeyNotFound
59+
}
60+
61+
return nil, err
62+
}
63+
64+
return &Entry{Reader: val}, err
65+
}
66+
67+
// Insert adds a key to the associated kv store.
68+
func (s *Store) Insert(key string, value io.Reader) error {
69+
err := s.kvstore.Insert(key, value)
70+
return err
71+
}

objectstore/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Deprecated: Use the kvstore package instead.
2+
package objectstore

objectstore/objectstore.go

Lines changed: 10 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,22 @@
11
package objectstore
22

33
import (
4-
"errors"
5-
"io"
6-
7-
"github.com/fastly/compute-sdk-go/internal/abi/fastly"
4+
"github.com/fastly/compute-sdk-go/kvstore"
85
)
96

10-
var ErrKeyNotFound = errors.New("objectstore: key not found")
11-
12-
type Entry struct {
13-
io.Reader
14-
15-
validString bool
16-
s string
17-
}
18-
19-
func (e *Entry) String() string {
20-
if e.validString {
21-
return e.s
22-
}
7+
var ErrKeyNotFound = kvstore.ErrKeyNotFound
238

24-
// TODO(dgryski): replace with StringBuilder + io.Copy ?
25-
b, err := io.ReadAll(e)
26-
if err != nil {
27-
return ""
28-
}
29-
30-
e.validString = true
31-
32-
return string(b)
33-
34-
}
9+
// Deprecated: Use the kvstore package instead.
10+
type Entry = kvstore.Entry
3511

3612
// Store represents a Fastly object store
37-
type Store struct {
38-
objectstore *fastly.ObjectStore
39-
}
13+
//
14+
// Deprecated: Use the kvstore package instead.
15+
type Store = kvstore.Store
4016

4117
// Open returns a handle to the named object store
18+
//
19+
// Deprecated: Use kvstore.Open() instead.
4220
func Open(name string) (*Store, error) {
43-
o, err := fastly.OpenObjectStore(name)
44-
if err != nil {
45-
return nil, err
46-
}
47-
48-
return &Store{objectstore: o}, nil
49-
}
50-
51-
// Lookup fetches a key from the associated object store. If the key does not
52-
// exist, Lookup returns the sentinel error ErrKeyNotFound.
53-
func (s *Store) Lookup(key string) (*Entry, error) {
54-
val, err := s.objectstore.Lookup(key)
55-
if err != nil {
56-
57-
// turn FastlyStatusNone into NotFound
58-
if status, ok := fastly.IsFastlyError(err); ok && status == fastly.FastlyStatusNone {
59-
return nil, ErrKeyNotFound
60-
}
61-
62-
return nil, err
63-
}
64-
65-
return &Entry{Reader: val}, err
66-
}
67-
68-
// Insert adds a key to the associated object store.
69-
func (s *Store) Insert(key string, value io.Reader) error {
70-
err := s.objectstore.Insert(key, value)
71-
return err
21+
return kvstore.Open(name)
7222
}

semgrep/README.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ This is a set of static analysis patterns for [semgrep](https://semgrep.dev).
33
# List of rules
44

55
- edgedict: warn and migrate from the edgedict API to the configstore API
6+
- objectstore: warn and migrate from objectstore API to the kvstore API

semgrep/objectstore.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
rules:
2+
- id: deprecated-objectstore-store
3+
pattern: objectstore.Store
4+
fix: kvstore.Store
5+
message: objectstore package is deprecated
6+
languages: [go]
7+
severity: ERROR
8+
9+
- id: deprecated-objectstore-open
10+
pattern: objectstore.Open($N)
11+
fix: kvstore.Open($N)
12+
message: objectstore package is deprecated
13+
languages: [go]
14+
severity: ERROR
15+
16+
- id: deprecated-objectstore-errkeynotfound
17+
pattern: objectstore.ErrKeyNotFound
18+
fix: kvstore.ErrKeyNotFound
19+
message: objectstore package is deprecated
20+
languages: [go]
21+
severity: ERROR

0 commit comments

Comments
 (0)