-
Notifications
You must be signed in to change notification settings - Fork 78
add nop store #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add nop store #126
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ leveldb | |
| memcached | ||
| mongodb | ||
| mysql | ||
| nop | ||
| postgresql | ||
| redis | ||
| s3 | ||
|
|
||
| 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 | ||
| 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 | ||
| ) |
| 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= |
| 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{} | ||
| } |
| 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") | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a blocker, but checking all other
Suggested change
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") | ||||||
| ) | ||||||
Uh oh!
There was an error while loading. Please reload this page.