Commit c99e4944e570f5885c87c33671921278e766e3bf

Authored by Marko Tikvić
1 parent d869bd3f2f
Exists in master

filtering

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