-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice_filter.go
53 lines (43 loc) · 940 Bytes
/
slice_filter.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
41
42
43
44
45
46
47
48
49
50
51
52
53
// 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 sliceFilter struct {
filters []Filter
}
// NewSliceFilter constructor.
func NewSliceFilter(filters ...Filter) Filter {
return &sliceFilter{
filters: filters,
}
}
func (f sliceFilter) Filter(value Value) (Value, error) {
s := reflect.ValueOf(value)
if s.Kind() != reflect.Slice {
return value, fmt.Errorf("value is not a slice type: %v", s)
}
/*
if s.IsNil() {
return value, nil
}
*/
items := make([]Value, s.Len())
for i := 0; i < s.Len(); i++ {
items[i] = s.Index(i).Interface()
}
for i, val := range items {
for _, ftr := range f.filters {
v, err := ftr.Filter(val)
if err != nil {
return value, fmt.Errorf("apply filter: %w", err)
}
val = v
}
items[i] = val
}
return items, nil
}