-
Notifications
You must be signed in to change notification settings - Fork 38
/
query_filters.go
37 lines (31 loc) · 941 Bytes
/
query_filters.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
package adminapi
import (
"encoding/json"
"go.uber.org/zap"
"github.com/transcom/mymove/pkg/services"
)
// generateQueryFilters is helper to convert filter params from a json string into a slice of services.QueryFilter
//
// The convertFns is a mapping from the filter parameter name(s) to the function that will operate on the
// associated non-empty filter parameter value
func generateQueryFilters(log *zap.Logger, params *string, convertFns map[string]func(string) []services.QueryFilter) (results []services.QueryFilter) {
if params == nil {
return results
}
input := map[string]string{}
if err := json.Unmarshal([]byte(*params), &input); err != nil {
log.Warn(
"unable to decode param",
zap.Error(err),
zap.String("filters", *params),
)
return results
}
for key, fn := range convertFns {
content := input[key]
if content != "" {
results = append(results, fn(content)...)
}
}
return results
}