From 53bc79647bb4ce00c00b21f03594d428615bc4bb Mon Sep 17 00:00:00 2001 From: Julia Qiu Date: Sat, 12 Jun 2021 16:28:05 -0700 Subject: [PATCH] detect dup and empty keys --- wfcache.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/wfcache.go b/wfcache.go index f9931e6..dd4a2b6 100644 --- a/wfcache.go +++ b/wfcache.go @@ -47,6 +47,28 @@ var nosop = func(ctx context.Context, opName string) interface{} { } var nofop = func(input interface{}) {} +func hasDuplicates(keys []string) bool { + encountered := map[string]bool{} + + for i := range keys { + if encountered[keys[i]] { + return true + } else { + encountered[keys[i]] = true + } + } + return false +} + +func hasEmptyString(keys []string) bool { + for i := range keys { + if keys[i] == "" { + return true + } + } + return false +} + func New(maker StorageMaker, otherMakers ...StorageMaker) (*Cache, error) { return NewWithHooks( nosop, @@ -148,7 +170,13 @@ func (c *Cache) BatchGet(keys []string) ([]*CacheItem, error) { } func (c *Cache) BatchGetWithContext(ctx context.Context, keys []string) ([]*CacheItem, error) { - // TODO(juliaqiuxy) Detect dupes, empty keys, then bail + if hasDuplicates(keys) { + return nil, errors.New("duplicated keys are not allowed") + } + + if hasEmptyString(keys) { + return nil, errors.New("empty keys are not allowed") + } storages, err := c.Storages() if err != nil {