Skip to content

Commit

Permalink
detect dup and empty keys
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaqiuxy committed Jun 12, 2021
1 parent c57ce8b commit 53bc796
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion wfcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 53bc796

Please sign in to comment.