Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Replace interface{} with any #150

Merged
merged 4 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ Features

```go
type Store interface {
Set(k string, v interface{}) error
Get(k string, v interface{}) (found bool, err error)
Set(k string, v any) error
Get(k string, v any) (found bool, err error)
Delete(k string) error
Close() error
}
Expand Down Expand Up @@ -103,7 +103,7 @@ For the Godoc of specific implementations, see <https://pkg.go.dev/github.com/ph

### Value types

Most Go packages for key-value stores just accept a `[]byte` as value, which requires developers for example to marshal (and later unmarshal) their structs. `gokv` is meant to be simple and make developers' lifes easier, so it accepts any type (with using `interface{}` as parameter), including structs, and automatically (un-)marshals the value.
Most Go packages for key-value stores just accept a `[]byte` as value, which requires developers for example to marshal (and later unmarshal) their structs. `gokv` is meant to be simple and make developers' lifes easier, so it accepts any type (with using `any`/`interface{}` as parameter), including structs, and automatically (un-)marshals the value.

The kind of (un-)marshalling is left to the implementation. All implementations in this repository currently support JSON and [gob](https://blog.golang.org/gobs-of-data) by using the `encoding` subpackage in this repository, which wraps the core functionality of the standard library's `encoding/json` and `encoding/gob` packages. See [Marshal formats](#marshal-formats) for details.

Expand Down Expand Up @@ -249,9 +249,9 @@ Project status

Planned interface methods until `v1.0.0`:

- `List(interface{}) error` / `GetAll(interface{}) error` or similar
- `List(any) error` / `GetAll(any) error` or similar

The interface might even change until `v1.0.0`. For example one consideration is to change `Get(string, interface{}) (bool, error)` to `Get(string, interface{}) error` (no boolean return value anymore), with the `error` being something like `gokv.ErrNotFound // "Key-value pair not found"` to fulfill the additional role of indicating that the key-value pair wasn't found. But at the moment we prefer the current method signature.
The interface might even change until `v1.0.0`. For example one consideration is to change `Get(string, any) (bool, error)` to `Get(string, any) error` (no boolean return value anymore), with the `error` being something like `gokv.ErrNotFound // "Key-value pair not found"` to fulfill the additional role of indicating that the key-value pair wasn't found. But at the moment we prefer the current method signature.

Also, more interfaces might be added. For example so that there's a `SimpleStore` and an `AdvancedStore`, with the first one containing only the basic methods and the latter one with advanced features such as key-value pair lifetimes (deletion of key-value pairs after a given time), notification of value changes via Go channels etc. But currently the focus is simplicity, see [Design decisions](#design-decisions).

Expand Down
4 changes: 2 additions & 2 deletions badgerdb/badgerdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Store struct {
// Set stores the given value for the given key.
// Values are automatically marshalled to JSON or gob (depending on the configuration).
// The key must not be "" and the value must not be nil.
func (s Store) Set(k string, v interface{}) error {
func (s Store) Set(k string, v any) error {
if err := util.CheckKeyAndValue(k, v); err != nil {
return err
}
Expand All @@ -42,7 +42,7 @@ func (s Store) Set(k string, v interface{}) error {
// that v points to with the values of the retrieved object's values.
// If no value is found it returns (false, nil).
// The key must not be "" and the pointer must not be nil.
func (s Store) Get(k string, v interface{}) (found bool, err error) {
func (s Store) Get(k string, v any) (found bool, err error) {
if err := util.CheckKeyAndValue(k, v); err != nil {
return false, err
}
Expand Down
2 changes: 1 addition & 1 deletion badgerdb/badgerdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestNil(t *testing.T) {
t.Error("An error was expected")
}

var i interface{} // actually nil
var i any // actually nil
_, err = store.Get("foo", i)
if err == nil {
t.Error("An error was expected")
Expand Down
4 changes: 2 additions & 2 deletions bbolt/bbolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Store struct {
// Set stores the given value for the given key.
// Values are automatically marshalled to JSON or gob (depending on the configuration).
// The key must not be "" and the value must not be nil.
func (s Store) Set(k string, v interface{}) error {
func (s Store) Set(k string, v any) error {
if err := util.CheckKeyAndValue(k, v); err != nil {
return err
}
Expand All @@ -44,7 +44,7 @@ func (s Store) Set(k string, v interface{}) error {
// that v points to with the values of the retrieved object's values.
// If no value is found it returns (false, nil).
// The key must not be "" and the pointer must not be nil.
func (s Store) Get(k string, v interface{}) (found bool, err error) {
func (s Store) Get(k string, v any) (found bool, err error) {
if err := util.CheckKeyAndValue(k, v); err != nil {
return false, err
}
Expand Down
2 changes: 1 addition & 1 deletion bbolt/bbolt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestNil(t *testing.T) {
t.Error("An error was expected")
}

var i interface{} // actually nil
var i any // actually nil
_, err = store.Get("foo", i)
if err == nil {
t.Error("An error was expected")
Expand Down
4 changes: 2 additions & 2 deletions bigcache/bigcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Store struct {
// Set stores the given value for the given key.
// Values are automatically marshalled to JSON or gob (depending on the configuration).
// The key must not be "" and the value must not be nil.
func (s Store) Set(k string, v interface{}) error {
func (s Store) Set(k string, v any) error {
if err := util.CheckKeyAndValue(k, v); err != nil {
return err
}
Expand All @@ -37,7 +37,7 @@ func (s Store) Set(k string, v interface{}) error {
// that v points to with the values of the retrieved object's values.
// If no value is found it returns (false, nil).
// The key must not be "" and the pointer must not be nil.
func (s Store) Get(k string, v interface{}) (found bool, err error) {
func (s Store) Get(k string, v any) (found bool, err error) {
if err := util.CheckKeyAndValue(k, v); err != nil {
return false, err
}
Expand Down
2 changes: 1 addition & 1 deletion bigcache/bigcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestNil(t *testing.T) {
t.Error("An error was expected")
}

var i interface{} // actually nil
var i any // actually nil
_, err = store.Get("foo", i)
if err == nil {
t.Error("An error was expected")
Expand Down
2 changes: 1 addition & 1 deletion cockroachdb/cockroachdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestNil(t *testing.T) {
t.Error("An error was expected")
}

var i interface{} // actually nil
var i any // actually nil
_, err = client.Get("foo", i)
if err == nil {
t.Error("An error was expected")
Expand Down
4 changes: 2 additions & 2 deletions consul/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Client struct {
// Set stores the given value for the given key.
// Values are automatically marshalled to JSON or gob (depending on the configuration).
// The key must not be "" and the value must not be nil.
func (c Client) Set(k string, v interface{}) error {
func (c Client) Set(k string, v any) error {
if err := util.CheckKeyAndValue(k, v); err != nil {
return err
}
Expand Down Expand Up @@ -49,7 +49,7 @@ func (c Client) Set(k string, v interface{}) error {
// that v points to with the values of the retrieved object's values.
// If no value is found it returns (false, nil).
// The key must not be "" and the pointer must not be nil.
func (c Client) Get(k string, v interface{}) (found bool, err error) {
func (c Client) Get(k string, v any) (found bool, err error) {
if err := util.CheckKeyAndValue(k, v); err != nil {
return false, err
}
Expand Down
2 changes: 1 addition & 1 deletion consul/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestNil(t *testing.T) {
t.Error("An error was expected")
}

var i interface{} // actually nil
var i any // actually nil
_, err = client.Get("foo", i)
if err == nil {
t.Error("An error was expected")
Expand Down
4 changes: 2 additions & 2 deletions datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Client struct {
// Set stores the given value for the given key.
// Values are automatically marshalled to JSON or gob (depending on the configuration).
// The key must not be "" and the value must not be nil.
func (c Client) Set(k string, v interface{}) error {
func (c Client) Set(k string, v any) error {
if err := util.CheckKeyAndValue(k, v); err != nil {
return err
}
Expand Down Expand Up @@ -65,7 +65,7 @@ func (c Client) Set(k string, v interface{}) error {
// that v points to with the values of the retrieved object's values.
// If no value is found it returns (false, nil).
// The key must not be "" and the pointer must not be nil.
func (c Client) Get(k string, v interface{}) (found bool, err error) {
func (c Client) Get(k string, v any) (found bool, err error) {
if err := util.CheckKeyAndValue(k, v); err != nil {
return false, err
}
Expand Down
2 changes: 1 addition & 1 deletion datastore/datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestNil(t *testing.T) {
t.Error("An error was expected")
}

var i interface{} // actually nil
var i any // actually nil
_, err = client.Get("foo", i)
if err == nil {
t.Error("An error was expected")
Expand Down
4 changes: 2 additions & 2 deletions dynamodb/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Client struct {
// Set stores the given value for the given key.
// Values are automatically marshalled to JSON or gob (depending on the configuration).
// The key must not be "" and the value must not be nil.
func (c Client) Set(k string, v interface{}) error {
func (c Client) Set(k string, v any) error {
if err := util.CheckKeyAndValue(k, v); err != nil {
return err
}
Expand Down Expand Up @@ -66,7 +66,7 @@ func (c Client) Set(k string, v interface{}) error {
// that v points to with the values of the retrieved object's values.
// If no value is found it returns (false, nil).
// The key must not be "" and the pointer must not be nil.
func (c Client) Get(k string, v interface{}) (found bool, err error) {
func (c Client) Get(k string, v any) (found bool, err error) {
if err := util.CheckKeyAndValue(k, v); err != nil {
return false, err
}
Expand Down
2 changes: 1 addition & 1 deletion dynamodb/dynamodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func TestNil(t *testing.T) {
t.Error("An error was expected")
}

var i interface{} // actually nil
var i any // actually nil
_, err = client.Get("foo", i)
if err == nil {
t.Error("An error was expected")
Expand Down
4 changes: 2 additions & 2 deletions encoding/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package encoding
// Codec encodes/decodes Go values to/from slices of bytes.
type Codec interface {
// Marshal encodes a Go value to a slice of bytes.
Marshal(v interface{}) ([]byte, error)
Marshal(v any) ([]byte, error)
// Unmarshal decodes a slice of bytes into a Go value.
Unmarshal(data []byte, v interface{}) error
Unmarshal(data []byte, v any) error
}

// Convenience variables
Expand Down
4 changes: 2 additions & 2 deletions encoding/gob.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type GobCodec struct{}

// Marshal encodes a Go value to gob.
func (c GobCodec) Marshal(v interface{}) ([]byte, error) {
func (c GobCodec) Marshal(v any) ([]byte, error) {
buffer := new(bytes.Buffer)
encoder := gob.NewEncoder(buffer)
err := encoder.Encode(v)
Expand All @@ -21,7 +21,7 @@ func (c GobCodec) Marshal(v interface{}) ([]byte, error) {
}

// Unmarshal decodes a gob value into a Go value.
func (c GobCodec) Unmarshal(data []byte, v interface{}) error {
func (c GobCodec) Unmarshal(data []byte, v any) error {
reader := bytes.NewReader(data)
decoder := gob.NewDecoder(reader)
return decoder.Decode(v)
Expand Down
4 changes: 2 additions & 2 deletions encoding/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
type JSONcodec struct{}

// Marshal encodes a Go value to JSON.
func (c JSONcodec) Marshal(v interface{}) ([]byte, error) {
func (c JSONcodec) Marshal(v any) ([]byte, error) {
return json.Marshal(v)
}

// Unmarshal decodes a JSON value into a Go value.
func (c JSONcodec) Unmarshal(data []byte, v interface{}) error {
func (c JSONcodec) Unmarshal(data []byte, v any) error {
return json.Unmarshal(data, v)
}
4 changes: 2 additions & 2 deletions encoding/proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type PBcodec struct{}

// Marshal encodes a proto message struct into the binary wire format.
// Passed value can't be any Go value, but must be an object of a proto message struct.
func (c PBcodec) Marshal(v interface{}) ([]byte, error) {
func (c PBcodec) Marshal(v any) ([]byte, error) {
msg, ok := v.(proto.Message)
if !ok {
return nil, errors.New("error casting interface to proto")
Expand All @@ -28,7 +28,7 @@ func (c PBcodec) Marshal(v interface{}) ([]byte, error) {

// Unmarshal parses a wire-format message in proto message struct.
// Passed value can't be any Go value, but must be an object of a proto message struct.
func (c PBcodec) Unmarshal(data []byte, v interface{}) error {
func (c PBcodec) Unmarshal(data []byte, v any) error {
msg, ok := v.(proto.Message)
if !ok {
return errors.New("error casting interface to proto")
Expand Down
4 changes: 2 additions & 2 deletions etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Client struct {
// Set stores the given value for the given key.
// Values are automatically marshalled to JSON or gob (depending on the configuration).
// The key must not be "" and the value must not be nil.
func (c Client) Set(k string, v interface{}) error {
func (c Client) Set(k string, v any) error {
if err := util.CheckKeyAndValue(k, v); err != nil {
return err
}
Expand All @@ -50,7 +50,7 @@ func (c Client) Set(k string, v interface{}) error {
// that v points to with the values of the retrieved object's values.
// If no value is found it returns (false, nil).
// The key must not be "" and the pointer must not be nil.
func (c Client) Get(k string, v interface{}) (found bool, err error) {
func (c Client) Get(k string, v any) (found bool, err error) {
if err := util.CheckKeyAndValue(k, v); err != nil {
return false, err
}
Expand Down
4 changes: 2 additions & 2 deletions etcd/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
"time"

"go.etcd.io/etcd/client/v3"
clientv3 "go.etcd.io/etcd/client/v3"

"github.com/philippgille/gokv/encoding"
"github.com/philippgille/gokv/etcd"
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestNil(t *testing.T) {
t.Error("An error was expected")
}

var i interface{} // actually nil
var i any // actually nil
_, err = client.Get("foo", i)
if err == nil {
t.Error("An error was expected")
Expand Down
4 changes: 2 additions & 2 deletions file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Store struct {
// Set stores the given value for the given key.
// Values are automatically marshalled to JSON or gob (depending on the configuration).
// The key must not be "" and the value must not be nil.
func (s Store) Set(k string, v interface{}) error {
func (s Store) Set(k string, v any) error {
if err := util.CheckKeyAndValue(k, v); err != nil {
return err
}
Expand Down Expand Up @@ -61,7 +61,7 @@ func (s Store) Set(k string, v interface{}) error {
// that v points to with the values of the retrieved object's values.
// If no value is found it returns (false, nil).
// The key must not be "" and the pointer must not be nil.
func (s Store) Get(k string, v interface{}) (found bool, err error) {
func (s Store) Get(k string, v any) (found bool, err error) {
if err := util.CheckKeyAndValue(k, v); err != nil {
return false, err
}
Expand Down
2 changes: 1 addition & 1 deletion file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestNil(t *testing.T) {
t.Error("An error was expected")
}

var i interface{} // actually nil
var i any // actually nil
_, err = store.Get("foo", i)
if err == nil {
t.Error("An error was expected")
Expand Down
4 changes: 2 additions & 2 deletions freecache/freecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Store struct {
// Set stores the given value for the given key.
// Values are automatically marshalled to JSON or gob (depending on the configuration).
// The key must not be "" and the value must not be nil.
func (s Store) Set(k string, v interface{}) error {
func (s Store) Set(k string, v any) error {
if err := util.CheckKeyAndValue(k, v); err != nil {
return err
}
Expand All @@ -37,7 +37,7 @@ func (s Store) Set(k string, v interface{}) error {
// that v points to with the values of the retrieved object's values.
// If no value is found it returns (false, nil).
// The key must not be "" and the pointer must not be nil.
func (s Store) Get(k string, v interface{}) (found bool, err error) {
func (s Store) Get(k string, v any) (found bool, err error) {
if err := util.CheckKeyAndValue(k, v); err != nil {
return false, err
}
Expand Down
2 changes: 1 addition & 1 deletion freecache/freecache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestNil(t *testing.T) {
t.Error("An error was expected")
}

var i interface{} // actually nil
var i any // actually nil
_, err = store.Get("foo", i)
if err == nil {
t.Error("An error was expected")
Expand Down
4 changes: 2 additions & 2 deletions gomap/gomap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Store struct {
// Set stores the given value for the given key.
// Values are automatically marshalled to JSON or gob (depending on the configuration).
// The key must not be "" and the value must not be nil.
func (s Store) Set(k string, v interface{}) error {
func (s Store) Set(k string, v any) error {
if err := util.CheckKeyAndValue(k, v); err != nil {
return err
}
Expand All @@ -39,7 +39,7 @@ func (s Store) Set(k string, v interface{}) error {
// that v points to with the values of the retrieved object's values.
// If no value is found it returns (false, nil).
// The key must not be "" and the pointer must not be nil.
func (s Store) Get(k string, v interface{}) (found bool, err error) {
func (s Store) Get(k string, v any) (found bool, err error) {
if err := util.CheckKeyAndValue(k, v); err != nil {
return false, err
}
Expand Down
2 changes: 1 addition & 1 deletion gomap/gomap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestNil(t *testing.T) {
t.Error("An error was expected")
}

var i interface{} // actually nil
var i any // actually nil
_, err = store.Get("foo", i)
if err == nil {
t.Error("An error was expected")
Expand Down
4 changes: 2 additions & 2 deletions hazelcast/hazelcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Client struct {
// Set stores the given value for the given key.
// Values are automatically marshalled to JSON or gob (depending on the configuration).
// The key must not be "" and the value must not be nil.
func (c Client) Set(k string, v interface{}) error {
func (c Client) Set(k string, v any) error {
if err := util.CheckKeyAndValue(k, v); err != nil {
return err
}
Expand All @@ -47,7 +47,7 @@ func (c Client) Set(k string, v interface{}) error {
// that v points to with the values of the retrieved object's values.
// If no value is found it returns (false, nil).
// The key must not be "" and the pointer must not be nil.
func (c Client) Get(k string, v interface{}) (found bool, err error) {
func (c Client) Get(k string, v any) (found bool, err error) {
if err := util.CheckKeyAndValue(k, v); err != nil {
return false, err
}
Expand Down
Loading
Loading