generated from koddr/template-go
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filtered.go
52 lines (43 loc) · 1.42 KB
/
filtered.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
package main
import (
"errors"
"fmt"
"github.com/koddr/gosl"
)
// newFiltered provides a new filter instance.
func newFiltered(config *Config, inputs *Inputs) (*Filtered, error) {
// Check, if the input data is not nil or empty.
if inputs.Data == nil || len(inputs.Data) == 0 {
return nil, errors.New("input data is empty or not parsed")
}
// Create a new Filtered instance.
filtered := &Filtered{}
// Check, if the filter has columns.
if config.FilterColumns != nil && len(config.FilterColumns) > 0 {
// Loop for all inputs data.
for _, data := range inputs.Data {
// Create a temp slice.
m := make([]bool, 0)
// Loop for all filtered columns.
for index, column := range config.FilterColumns {
// Matching values with condition between input (in data file) and column (in config file).
match, err := matchValues(data[inputs.Mapping[column.ColumnName]], column.Value, column.Condition)
if err != nil {
return nil, fmt.Errorf(
"%w, column %d ('%v') in row %v",
err, index, column.ColumnName, data,
)
}
// Collect matching result to the temp slice.
m = append(m, match)
}
// Check, if there is no false in the conditions, it means
// that the filter settings have been respected.
if !gosl.ContainsInSlice(m, false) {
// Collect the current data to the filtered result.
filtered.Data = append(filtered.Data, data)
}
}
}
return filtered, nil
}