-
Notifications
You must be signed in to change notification settings - Fork 85
/
values.go
98 lines (82 loc) · 2.18 KB
/
values.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2019 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"fmt"
"net/http"
"sort"
"strings"
moovhttp "github.com/moov-io/base/http"
"github.com/go-kit/kit/log"
"github.com/gorilla/mux"
)
// accumulator is a case-insensitive collector for string values.
//
// getValues() will return an orderd distinct array of accumulated strings
// where each string is the first seen instance.
type accumulator struct {
limit int
values map[string]string
}
func newAccumulator(limit int) accumulator {
return accumulator{
limit: limit,
values: make(map[string]string),
}
}
func (acc accumulator) add(value string) {
if len(acc.values) >= acc.limit {
return
}
norm := strings.ToLower(strings.TrimSpace(value))
if norm == "" {
return
}
if _, exists := acc.values[norm]; !exists {
acc.values[norm] = value
}
}
func (acc accumulator) getValues() []string {
var out []string
for _, v := range acc.values {
out = append(out, v)
}
sort.Strings(out)
return out
}
func addValuesRoutes(logger log.Logger, r *mux.Router, searcher *searcher) {
r.Methods("GET").Path("/ui/values/{key}").HandlerFunc(getValues(logger, searcher))
}
func getKey(r *http.Request) string {
return strings.ToLower(mux.Vars(r)["key"])
}
func getValues(logger log.Logger, searcher *searcher) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w = wrapResponseWriter(logger, w, r)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
acc := newAccumulator(extractSearchLimit(r))
key := getKey(r)
if strings.EqualFold(key, "sdnType") {
acc.add("entity")
}
for i := range searcher.SDNs {
// If we add support for other filters (CallSign, Tonnage)
// then we should add those keys here.
switch key {
case "sdntype":
acc.add(searcher.SDNs[i].SDNType)
case "ofacprogram":
for j := range searcher.SDNs[i].Programs {
acc.add(searcher.SDNs[i].Programs[j])
}
default:
moovhttp.Problem(w, fmt.Errorf("unknown key: %s", key))
return
}
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(acc.getValues())
}
}