-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.go
40 lines (35 loc) · 912 Bytes
/
process.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Created by Yakka (https://theyakka.com)
//
// Copyright (c) 2020 Yakka LLC.
// All rights reserved.
// See the LICENSE file for licensing details and requirements.
package ystore
type ProcessElementFunc func(key string, val interface{}) interface{}
func (ds *Store) Process(processor ProcessElementFunc) *Store {
if ds.Len() == 0 {
return NewStore()
}
processedStore := NewStore()
ds.Each(func(k string, v interface{}) {
switch v.(type) {
case Store:
store := v.(Store)
sp := &store
processedStore.Set(k, sp.Process(processor))
case *Store:
processedStore.Set(k, v.(*Store).Process(processor))
default:
processedVal := processor(k, v)
if processedVal != nil {
processedStore.Set(k, processedVal)
}
}
})
return processedStore
}
type EachFunc func(string, interface{})
func (ds *Store) Each(eachFunc EachFunc) {
for k, v := range ds.data {
eachFunc(k, v)
}
}