mirror of https://github.com/perkeep/perkeep.git
sorted: add Foreach and ForeachInRange helpers
Change-Id: Ifeba7ed6719468f01585aa7f7f9a9006fdd661de
This commit is contained in:
parent
a87bde76c7
commit
c4f0a62e84
|
@ -191,3 +191,24 @@ func NewKeyValue(cfg jsonconfig.Obj) (KeyValue, error) {
|
|||
}
|
||||
return s, cfg.Validate()
|
||||
}
|
||||
|
||||
// Foreach runs fn for each key/value pair in kv. If fn returns an error,
|
||||
// that same error is returned from Foreach and iteration stops.
|
||||
func Foreach(kv KeyValue, fn func(key, value string) error) error {
|
||||
return ForeachInRange(kv, "", "", fn)
|
||||
}
|
||||
|
||||
// ForeachInRange runs fn for each key/value pair in kv in the range
|
||||
// of start and end, which behave the same as kv.Find. If fn returns
|
||||
// an error, that same error is returned from Foreach and iteration
|
||||
// stops.
|
||||
func ForeachInRange(kv KeyValue, start, end string, fn func(key, value string) error) error {
|
||||
it := kv.Find(start, end)
|
||||
for it.Next() {
|
||||
if err := fn(it.Key(), it.Value()); err != nil {
|
||||
it.Close()
|
||||
return err
|
||||
}
|
||||
}
|
||||
return it.Close()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue