Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 nop/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Package nop contains an implementation of the `gokv.Store` interface that does nothing.
*/
package nop
Comment thread
peczenyj marked this conversation as resolved.
Outdated
11 changes: 11 additions & 0 deletions nop/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/philippgille/gokv/nop

go 1.18

require (
github.com/philippgille/gokv/util v0.6.0
)

require (
github.com/philippgille/gokv v0.6.0 // indirect
)
4 changes: 4 additions & 0 deletions nop/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 nop/nop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package nop

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 nop Store that implements gokv.Store interface.
func NewStore() Store {
return Store{}
}
89 changes: 89 additions & 0 deletions nop/nop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package nop_test

import (
"errors"
"testing"

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

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

var s gokv.Store = nop.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 = nop.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")
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker, but checking all other *_test.go files in this repo, they use either "Expected an error" or "An error was expected". The exact phrasing doesn't matter, but I'd suggest uppercase and past tense for consistency, e.g.

Suggested change
t.Error("expect error, got nil")
t.Error("Expected error, got nil")

Errors are usually lowercase as they can be chained in a log message (e.g. "couldn't x: failed to y: ...", but here it's a log message and not an error that might be chained, where I like the differentiation.

Again, totally not a blocker though.

} 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")
)