-
Notifications
You must be signed in to change notification settings - Fork 1
/
only.go
43 lines (33 loc) · 1010 Bytes
/
only.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
package filter
import (
"fmt"
"github.com/Sirupsen/logrus"
"github.com/marema31/kamino/provider/types"
)
// OnlyFilter specific type for Only filter operation.
type OnlyFilter struct {
columns []string
}
func newOnlyFilter(log *logrus.Entry, aParam []string) (Filter, error) {
logFilter := log.WithField("filter", "only")
if aParam == nil {
logFilter.Error("Missing AParameters")
return nil, fmt.Errorf("no parameter to filter only: %w", errMissingParameter)
}
if len(aParam) == 0 {
logFilter.Error("Refuse to filter everything")
return nil, fmt.Errorf("filter only refuse to filter everything: %w", errWrongParameterValue)
}
return &OnlyFilter{columns: aParam}, nil
}
// Filter : Only the content of column by provided values (insert the column if not present).
func (of *OnlyFilter) Filter(in types.Record) (types.Record, error) {
out := make(types.Record, len(in))
for _, col := range of.columns {
value, ok := in[col]
if ok {
out[col] = value
}
}
return out, nil
}