Commit d3cc6a2895889a782508006653637d860e384574

Authored by Marko Tikvić
1 parent 564ef69718
Exists in master

< > filtering

Showing 1 changed file with 11 additions and 1 deletions   Show diff stats
1 package webutility 1 package webutility
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "net/http" 5 "net/http"
6 "strings" 6 "strings"
7 ) 7 )
8 8
9 type Filter map[string]string 9 type Filter map[string]string
10 10
11 func (fs Filter) validate(validFilters []string) (Filter, bool) { 11 func (fs Filter) validate(validFilters []string) (Filter, bool) {
12 goodFilters := make(map[string]string) 12 goodFilters := make(map[string]string)
13 cnt := 0 13 cnt := 0
14 len := 0 14 len := 0
15 for f, _ := range fs { 15 for f, _ := range fs {
16 len++ 16 len++
17 for _, v := range validFilters { 17 for _, v := range validFilters {
18 if f == v { 18 if f == v {
19 cnt++ 19 cnt++
20 goodFilters[f] = fs[f] 20 goodFilters[f] = fs[f]
21 } 21 }
22 } 22 }
23 } 23 }
24 24
25 result := true 25 result := true
26 if len > 0 && cnt == 0 { 26 if len > 0 && cnt == 0 {
27 // if no valid filters are found declare filtering request as invalid 27 // if no valid filters are found declare filtering request as invalid
28 result = false 28 result = false
29 } 29 }
30 30
31 return goodFilters, result 31 return goodFilters, result
32 } 32 }
33 33
34 // requires input in format: "param1::value1|param2::value2..." 34 // requires input in format: "param1::value1|param2::value2..."
35 func ParseFilters(req *http.Request, header string) (filters Filter) { 35 func ParseFilters(req *http.Request, header string) (filters Filter) {
36 q := req.FormValue(header) 36 q := req.FormValue(header)
37 q = strings.Trim(q, "\"") 37 q = strings.Trim(q, "\"")
38 kvp := strings.Split(q, "|") 38 kvp := strings.Split(q, "|")
39 filters = make(map[string]string, len(kvp)) 39 filters = make(map[string]string, len(kvp))
40 40
41 for i, _ := range kvp { 41 for i, _ := range kvp {
42 kv := strings.Split(kvp[i], "::") 42 kv := strings.Split(kvp[i], "::")
43 if len(kv) == 2 { 43 if len(kv) == 2 {
44 key := kv[0] 44 key := kv[0]
45 val := kv[1] 45 val := kv[1]
46 filters[key] = val 46 filters[key] = val
47 } 47 }
48 } 48 }
49 49
50 return filters 50 return filters
51 } 51 }
52 52
53 // TODO(marko): very dodgy, needs more robustness
53 func MakeFilterString(prefix string, filters Filter, validFilters []string) (res string, ok bool) { 54 func MakeFilterString(prefix string, filters Filter, validFilters []string) (res string, ok bool) {
54 if prefix != "" { 55 if prefix != "" {
55 prefix += "." 56 prefix += "."
56 } 57 }
57 if len(filters) == 0 { 58 if len(filters) == 0 {
58 return "", true 59 return "", true
59 } 60 }
60 61
61 filters, ok = filters.validate(validFilters) 62 filters, ok = filters.validate(validFilters)
62 if !ok { 63 if !ok {
63 return "", ok 64 return "", ok
64 } 65 }
65 66
67 symbol := "="
66 for k, v := range filters { 68 for k, v := range filters {
67 if res != "" { 69 if res != "" {
68 res += " and " 70 res += " and "
69 } else { 71 } else {
70 res += " where " 72 res += " where "
71 } 73 }
72 res += fmt.Sprintf("%s%s = '%s'", prefix, k, v) 74 c := string(v[0])
75 if c == "<" || c == ">" {
76 symbol = c
77 v = string(v[1:])
78 } else {
79 symbol = "="
80 }
81
82 res += fmt.Sprintf("%s%s %s '%s'", prefix, k, symbol, v)
73 } 83 }
74 84
75 return res, ok 85 return res, ok
76 } 86 }
77 87