Skip to content

Commit

Permalink
storage: add 'Has' feature.
Browse files Browse the repository at this point in the history
I debated for a while if there's any way to avoid doing interfaces that
refer to other interfaces here, because I usually find that's a smell,
but any idea I can think of to try to avoid it seems worse (e.g.
would threaten to spawn a 'Has(R)' *and* a 'Has2(W)' function on the
package scope, due to golang's lack of polymorphic parameters, etc).
  • Loading branch information
warpfork committed Oct 22, 2021
1 parent ebf675a commit 6f153d0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions storage/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ import (

// --- basics --->

type Storage interface {
Has(ctx context.Context, key string) (bool, error)
}

type ReadableStorage interface {
Storage
Get(ctx context.Context, key string) ([]byte, error)
}

type WritableStorage interface {
Storage
Put(ctx context.Context, key string, content []byte) error
}

Expand Down
5 changes: 5 additions & 0 deletions storage/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import (
regardless of how much explicit support the storage implementation has for the exact behavior you requested.
*/

func Has(ctx context.Context, store Storage, key string) (bool, error) {
// Okay, not much going on here -- this function is only here for consistency of style.
return store.Has(ctx, key)
}

func Get(ctx context.Context, store ReadableStorage, key string) ([]byte, error) {
// Okay, not much going on here -- this function is only here for consistency of style.
return store.Get(ctx, key)
Expand Down

0 comments on commit 6f153d0

Please sign in to comment.