Skip to content
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ For the Godoc of specific implementations, see <https://pkg.go.dev/github.com/ph
- [X] [Apache Ignite](https://github.com/apache/ignite)
- [ ] [ArangoDB](https://github.com/arangodb/arangodb)
- [ ] [OrientDB](https://github.com/orientechnologies/orientdb)
- Misc
- [X] Go `nop` does nothing except validate the inputs, if applicable.

Again:
For differences between the implementations, see [Choosing an implementation](docs/choosing-implementation.md).
Expand Down
1 change: 1 addition & 0 deletions build/implementations
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ leveldb
memcached
mongodb
mysql
nop
postgresql
redis
s3
Expand Down
2 changes: 2 additions & 0 deletions docs/choosing-implementation.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,5 @@ Implementations
- [ArangoDB](https://github.com/arangodb/arangodb) (⚠️Not implemented yet!)
- [OrientDB](https://github.com/orientechnologies/orientdb) (⚠️Not implemented yet!)
- Claims to be the fastest graph database
- Misc
- Go `nop` does nothing except validate the inputs, if applicable.
4 changes: 4 additions & 0 deletions noop/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Package noop contains an implementation of the `gokv.Store` interface that does nothing.
*/
package noop
8 changes: 8 additions & 0 deletions noop/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/philippgille/gokv/noop

go 1.18

require (
github.com/philippgille/gokv v0.6.0
github.com/philippgille/gokv/util v0.6.0
)
4 changes: 4 additions & 0 deletions noop/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/philippgille/gokv v0.6.0 h1:fNEx/tSwV73nzlYd3iRYB8F+SEVJNNFzH1gsaT8SK2c=
github.com/philippgille/gokv v0.6.0/go.mod h1:tjXRFw9xDHgxLS8WJdfYotKGWp8TWqu4RdXjMDG/XBo=
github.com/philippgille/gokv/util v0.6.0 h1:GrTxVENzKBxs8lB3tnaA88mKOuVPT7atZPplxX+PNmo=
github.com/philippgille/gokv/util v0.6.0/go.mod h1:ovoDHZ2Svr7YX972SPPJQRXbhHEy3Gb20HRH/Tr9BiQ=
43 changes: 43 additions & 0 deletions noop/noop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package noop

import "github.com/philippgille/gokv/util"

// Store is a gokv.Store implementation that does nothing except validate the arguments if applicable.
type Store struct{}

// Set pretends if stores the key. Always return nil error unless the key or value are invalid.
func (s Store) Set(k string, v interface{}) error {
if err := util.CheckKeyAndValue(k, v); err != nil {
return err
}

return nil
}

// Get pretends it fetches the key. Always return not found and nil error unless the key or value are invalid.
func (s Store) Get(k string, v interface{}) (found bool, err error) {
if err := util.CheckKeyAndValue(k, v); err != nil {
return false, err
}

return false, nil
}

// Delete pretends it deletes the key. Always return nil error unless the key is invalid.
func (s Store) Delete(k string) error {
if err := util.CheckKey(k); err != nil {
return err
}

return nil
}

// Close pretends it closes the store. Always return nil error.
func (s Store) Close() error {
return nil
}

// NewStore creates a new noop Store that implements gokv.Store interface.
func NewStore() Store {
return Store{}
}
89 changes: 89 additions & 0 deletions noop/noop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package noop_test

import (
"errors"
"testing"

"github.com/philippgille/gokv"
"github.com/philippgille/gokv/noop"
)

func TestNop(t *testing.T) {
t.Parallel()

var s gokv.Store = noop.NewStore()

if err := s.Set("foo", 1); err != nil {
t.Error(err)
}

var v int
found, err := s.Get("foo", &v)
if err != nil {
t.Error(err)
}
if found {
t.Error("A value was found, but no value was expected")
}

if err := s.Delete("foo"); err != nil {
t.Error(err)
}

if err := s.Close(); err != nil {
t.Error(err)
}
}

func TestInputValidation(t *testing.T) {
t.Parallel()

var s gokv.Store = noop.NewStore()

{
err := s.Set("", 1)
assertEqualError(t, err, errInvalidKey.Error())
}

{
err := s.Set("foo", nil)
assertEqualError(t, err, errInvalidValue.Error())
}

{
var v int
found, err := s.Get("", &v)
assertEqualError(t, err, errInvalidKey.Error())
if found {
t.Error("A value was found, but no value was expected")
}
}

{
found, err := s.Get("foo", nil)
assertEqualError(t, err, errInvalidValue.Error())
if found {
t.Error("A value was found, but no value was expected")
}
}

{
err := s.Delete("")
assertEqualError(t, err, errInvalidKey.Error())
}
}

func assertEqualError(t *testing.T, err error, expectedErrMsg string) {
t.Helper()

if err == nil {
t.Error("expect error, got nil")
} else if err.Error() != expectedErrMsg {
t.Error(err)
}
}

var (
errInvalidKey = errors.New("The passed key is an empty string, which is invalid")
errInvalidValue = errors.New("The passed value is nil, which is not allowed")
)