Blame view
filtering.go
2.43 KB
564ef6971 added simple sql ... |
1 2 3 4 5 |
package webutility import ( "fmt" "net/http" |
d869bd3f2 filtering fix |
6 |
"net/url" |
564ef6971 added simple sql ... |
7 8 |
"strings" ) |
707782344 lint; vet |
9 |
// Filter ... |
dcfc40497 improved filtering |
10 |
type Filter map[string][]string |
564ef6971 added simple sql ... |
11 |
|
707782344 lint; vet |
12 |
// Get ... |
dcfc40497 improved filtering |
13 14 15 |
func (f Filter) Get(key string) (values []string, ok bool) { values, ok = f[key] return values, ok |
ab548c502 added Valid() to ... |
16 |
} |
707782344 lint; vet |
17 |
// Count ... |
d7e8e0a94 count; add |
18 |
func (f Filter) Count() int { |
dcfc40497 improved filtering |
19 20 |
return len(f) } |
707782344 lint; vet |
21 |
// Add ... |
d7e8e0a94 count; add |
22 23 |
func (f Filter) Add(key, val string) { f[key] = append(f[key], val) |
ab548c502 added Valid() to ... |
24 |
} |
707782344 lint; vet |
25 |
// ValueAt ... |
d06571d28 filter.ValueAt() |
26 27 28 29 30 31 32 33 34 |
func (f Filter) ValueAt(val string, index int) string { if filter, ok := f[val]; ok { if len(filter) > index { return filter[index] } } return "" } |
707782344 lint; vet |
35 |
func (f Filter) validate(validFilters []string) (Filter, bool) { |
dcfc40497 improved filtering |
36 37 |
goodFilters := make(Filter) cnt, len := 0, 0 |
707782344 lint; vet |
38 |
for fi := range f { |
564ef6971 added simple sql ... |
39 40 |
len++ for _, v := range validFilters { |
707782344 lint; vet |
41 |
if fi == v { |
564ef6971 added simple sql ... |
42 |
cnt++ |
707782344 lint; vet |
43 |
goodFilters[fi] = f[fi] |
564ef6971 added simple sql ... |
44 45 46 47 48 49 50 51 52 53 54 55 |
} } } result := true if len > 0 && cnt == 0 { // if no valid filters are found declare filtering request as invalid result = false } return goodFilters, result } |
707782344 lint; vet |
56 |
// ParseFilters requires input in format: "param1::value1|param2::value2..." |
564ef6971 added simple sql ... |
57 58 59 60 |
func ParseFilters(req *http.Request, header string) (filters Filter) { q := req.FormValue(header) q = strings.Trim(q, "\"") kvp := strings.Split(q, "|") |
dcfc40497 improved filtering |
61 |
filters = make(Filter, len(kvp)) |
564ef6971 added simple sql ... |
62 |
|
707782344 lint; vet |
63 |
for i := range kvp { |
564ef6971 added simple sql ... |
64 65 |
kv := strings.Split(kvp[i], "::") if len(kv) == 2 { |
d869bd3f2 filtering fix |
66 |
key, _ := url.QueryUnescape(kv[0]) |
dcfc40497 improved filtering |
67 68 69 70 71 72 73 |
// get values (if more than 1) vals := strings.Split(kv[1], ",") for _, v := range vals { u, _ := url.QueryUnescape(v) filters[key] = append(filters[key], u) } |
564ef6971 added simple sql ... |
74 75 76 77 78 |
} } return filters } |
707782344 lint; vet |
79 80 |
// MakeFilterString is very dodgy, needs more robustness. // TODO(marko) |
564ef6971 added simple sql ... |
81 82 83 84 |
func MakeFilterString(prefix string, filters Filter, validFilters []string) (res string, ok bool) { if prefix != "" { prefix += "." } |
dcfc40497 improved filtering |
85 |
|
d7e8e0a94 count; add |
86 |
if filters.Count() == 0 { |
564ef6971 added simple sql ... |
87 88 89 90 91 |
return "", true } filters, ok = filters.validate(validFilters) if !ok { |
dcfc40497 improved filtering |
92 |
return "", false |
564ef6971 added simple sql ... |
93 |
} |
ad6d508b9 minor fix |
94 |
first := true |
dcfc40497 improved filtering |
95 |
for k, filter := range filters { |
dcfc40497 improved filtering |
96 97 98 |
symbol := "=" if first { |
4f226ef30 fixed filterd ord... |
99 |
res += " where " |
dcfc40497 improved filtering |
100 |
first = false |
564ef6971 added simple sql ... |
101 |
} else { |
4f226ef30 fixed filterd ord... |
102 |
res += " and " |
564ef6971 added simple sql ... |
103 |
} |
d3cc6a289 < > filtering |
104 |
|
dcfc40497 improved filtering |
105 106 |
res += "(" for i, f := range filter { |
66643e090 added support for != |
107 |
if strings.HasPrefix(f, "<") || strings.HasPrefix(f, ">") || strings.HasPrefix(f, "!") { |
dcfc40497 improved filtering |
108 |
symbol = string(f[0]) |
66643e090 added support for != |
109 |
f = strings.TrimLeft(f, "<>!") |
dcfc40497 improved filtering |
110 111 112 113 114 115 116 117 118 119 120 121 122 |
if strings.HasPrefix(f, "=") { f = strings.TrimLeft(f, "=") symbol += "=" } } res += fmt.Sprintf("%s%s %s '%s'", prefix, k, symbol, f) if i < len(filter)-1 { res += " or " } } res += ")" |
564ef6971 added simple sql ... |
123 124 125 126 |
} return res, ok } |