Blame view

filtering.go 1.51 KB
564ef6971   Marko Tikvić   added simple sql ...
1
2
3
  package webutility
  
  import (
564ef6971   Marko Tikvić   added simple sql ...
4
  	"net/http"
d869bd3f2   Marko Tikvić   filtering fix
5
  	"net/url"
564ef6971   Marko Tikvić   added simple sql ...
6
7
  	"strings"
  )
707782344   Marko Tikvić   lint; vet
8
  // Filter ...
dcfc40497   Marko Tikvić   improved filtering
9
  type Filter map[string][]string
564ef6971   Marko Tikvić   added simple sql ...
10

707782344   Marko Tikvić   lint; vet
11
  // Get ...
dcfc40497   Marko Tikvić   improved filtering
12
13
14
  func (f Filter) Get(key string) (values []string, ok bool) {
  	values, ok = f[key]
  	return values, ok
ab548c502   Marko Tikvić   added Valid() to ...
15
  }
707782344   Marko Tikvić   lint; vet
16
  // Count ...
d7e8e0a94   Marko Tikvić   count; add
17
  func (f Filter) Count() int {
dcfc40497   Marko Tikvić   improved filtering
18
19
  	return len(f)
  }
707782344   Marko Tikvić   lint; vet
20
  // Add ...
d7e8e0a94   Marko Tikvić   count; add
21
22
  func (f Filter) Add(key, val string) {
  	f[key] = append(f[key], val)
ab548c502   Marko Tikvić   added Valid() to ...
23
  }
707782344   Marko Tikvić   lint; vet
24
  // ValueAt ...
d06571d28   Marko Tikvić   filter.ValueAt()
25
26
27
28
29
30
31
32
33
  func (f Filter) ValueAt(val string, index int) string {
  	if filter, ok := f[val]; ok {
  		if len(filter) > index {
  			return filter[index]
  		}
  	}
  
  	return ""
  }
b80ee4b2b   Marko Tikvić   new stuff
34
  func (f Filter) Validate(validFilters []string) (Filter, bool) {
dcfc40497   Marko Tikvić   improved filtering
35
36
  	goodFilters := make(Filter)
  	cnt, len := 0, 0
707782344   Marko Tikvić   lint; vet
37
  	for fi := range f {
564ef6971   Marko Tikvić   added simple sql ...
38
39
  		len++
  		for _, v := range validFilters {
707782344   Marko Tikvić   lint; vet
40
  			if fi == v {
564ef6971   Marko Tikvić   added simple sql ...
41
  				cnt++
707782344   Marko Tikvić   lint; vet
42
  				goodFilters[fi] = f[fi]
564ef6971   Marko Tikvić   added simple sql ...
43
44
45
46
47
48
49
50
51
52
53
54
  			}
  		}
  	}
  
  	result := true
  	if len > 0 && cnt == 0 {
  		// if no valid filters are found declare filtering request as invalid
  		result = false
  	}
  
  	return goodFilters, result
  }
707782344   Marko Tikvić   lint; vet
55
  // ParseFilters requires input in format: "param1::value1|param2::value2..."
564ef6971   Marko Tikvić   added simple sql ...
56
57
58
59
  func ParseFilters(req *http.Request, header string) (filters Filter) {
  	q := req.FormValue(header)
  	q = strings.Trim(q, "\"")
  	kvp := strings.Split(q, "|")
dcfc40497   Marko Tikvić   improved filtering
60
  	filters = make(Filter, len(kvp))
564ef6971   Marko Tikvić   added simple sql ...
61

707782344   Marko Tikvić   lint; vet
62
  	for i := range kvp {
564ef6971   Marko Tikvić   added simple sql ...
63
64
  		kv := strings.Split(kvp[i], "::")
  		if len(kv) == 2 {
d869bd3f2   Marko Tikvić   filtering fix
65
  			key, _ := url.QueryUnescape(kv[0])
dcfc40497   Marko Tikvić   improved filtering
66
67
68
69
70
71
72
  
  			// 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   Marko Tikvić   added simple sql ...
73
74
75
76
77
  		}
  	}
  
  	return filters
  }