Commit dcfc40497987d8c2eceff79446bf29135ced7b10

Authored by Marko Tikvić
1 parent edd7c4f4d7
Exists in master

improved filtering

Showing 1 changed file with 47 additions and 23 deletions   Show diff stats
... ... @@ -7,20 +7,24 @@ import (
7 7 "strings"
8 8 )
9 9  
10   -type Filter map[string]string
  10 +type Filter map[string][]string
11 11  
12   -func (f *Filter) Size() int {
13   - return len(*f)
  12 +func (f Filter) Get(key string) (values []string, ok bool) {
  13 + values, ok = f[key]
  14 + return values, ok
14 15 }
15 16  
16   -func (f *Filter) Valid() bool {
17   - return len(*f) > 0
  17 +func (f Filter) Size() int {
  18 + return len(f)
  19 +}
  20 +
  21 +func (f Filter) IsNotEmpty() bool {
  22 + return len(f) > 0
18 23 }
19 24  
20 25 func (fs Filter) validate(validFilters []string) (Filter, bool) {
21   - goodFilters := make(map[string]string)
22   - cnt := 0
23   - len := 0
  26 + goodFilters := make(Filter)
  27 + cnt, len := 0, 0
24 28 for f, _ := range fs {
25 29 len++
26 30 for _, v := range validFilters {
... ... @@ -45,14 +49,19 @@ func ParseFilters(req *http.Request, header string) (filters Filter) {
45 49 q := req.FormValue(header)
46 50 q = strings.Trim(q, "\"")
47 51 kvp := strings.Split(q, "|")
48   - filters = make(map[string]string, len(kvp))
  52 + filters = make(Filter, len(kvp))
49 53  
50 54 for i, _ := range kvp {
51 55 kv := strings.Split(kvp[i], "::")
52 56 if len(kv) == 2 {
53 57 key, _ := url.QueryUnescape(kv[0])
54   - val, _ := url.QueryUnescape(kv[1])
55   - filters[key] = val
  58 +
  59 + // get values (if more than 1)
  60 + vals := strings.Split(kv[1], ",")
  61 + for _, v := range vals {
  62 + u, _ := url.QueryUnescape(v)
  63 + filters[key] = append(filters[key], u)
  64 + }
56 65 }
57 66 }
58 67  
... ... @@ -64,30 +73,45 @@ func MakeFilterString(prefix string, filters Filter, validFilters []string) (res
64 73 if prefix != "" {
65 74 prefix += "."
66 75 }
67   - if len(filters) == 0 {
  76 +
  77 + if !filters.IsNotEmpty() {
68 78 return "", true
69 79 }
70 80  
71 81 filters, ok = filters.validate(validFilters)
72 82 if !ok {
73   - return "", ok
  83 + return "", false
74 84 }
75 85  
76   - symbol := "="
77   - for k, v := range filters {
78   - if res != "" {
  86 + for k, filter := range filters {
  87 + first := true
  88 + symbol := "="
  89 +
  90 + if first {
79 91 res += " and "
  92 + first = false
80 93 } else {
81 94 res += " where "
82 95 }
83   - c := string(v[0])
84   - if c == "<" || c == ">" {
85   - symbol = "" // examples: >3, >=3
86   - } else {
87   - symbol = "="
88   - }
89 96  
90   - res += fmt.Sprintf("%s%s %s '%s'", prefix, k, symbol, v)
  97 + res += "("
  98 + for i, f := range filter {
  99 + if strings.HasPrefix(f, "<") || strings.HasPrefix(f, ">") {
  100 + symbol = string(f[0])
  101 + f = strings.TrimLeft(f, "<>")
  102 + if strings.HasPrefix(f, "=") {
  103 + f = strings.TrimLeft(f, "=")
  104 + symbol += "="
  105 + }
  106 + }
  107 +
  108 + res += fmt.Sprintf("%s%s %s '%s'", prefix, k, symbol, f)
  109 +
  110 + if i < len(filter)-1 {
  111 + res += " or "
  112 + }
  113 + }
  114 + res += ")"
91 115 }
92 116  
93 117 return res, ok
... ...