-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_filter.go
More file actions
61 lines (48 loc) · 1.06 KB
/
map_filter.go
File metadata and controls
61 lines (48 loc) · 1.06 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2021 Hyperscale. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package filter
import (
"fmt"
"reflect"
)
type mapFilter struct {
def map[interface{}][]Filter
}
// NewMapFilter constructor.
func NewMapFilter(opts ...MapOption) Filter {
f := &mapFilter{
def: map[interface{}][]Filter{},
}
for _, opt := range opts {
opt(f)
}
return f
}
func (f mapFilter) Filter(value Value) (Value, error) {
s := reflect.ValueOf(value)
if s.Kind() != reflect.Map {
return value, fmt.Errorf("value is not a map type: %v", s)
}
/*
if s.IsNil() {
return value, nil
}
*/
data := make(map[string]Value, s.Len())
for _, key := range s.MapKeys() {
v := s.MapIndex(key)
data[key.String()] = v.Interface()
filters, ok := f.def[key.Interface()]
if ok {
for _, filter := range filters {
val, err := filter.Filter(data[key.String()])
if err != nil {
return value, fmt.Errorf("apply filter: %w", err)
}
data[key.String()] = val
}
}
}
return data, nil
}