-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
132 lines (116 loc) · 2.61 KB
/
helpers.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package mongorest
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
"gopkg.in/mgo.v2/bson"
)
func toString(val interface{}) string {
switch val.(type) {
case int, int32, int64, float32, float64:
return fmt.Sprintf("%v", val)
case bson.ObjectId:
o, _ := val.(bson.ObjectId)
return o.Hex()
}
return "unknown"
}
func writeHtml(w http.ResponseWriter, items []Document) {
for _, item := range items {
for key, value := range item {
fmt.Fprintf(w, "%v: %v<br />", key, value)
}
fmt.Fprint(w, "<br />")
}
}
func createIdLookup(id string) bson.M {
// TODO: add dates to this
hex := regexp.MustCompile("^[0-9a-f]{24}$")
number := regexp.MustCompile("^[0-9]+$")
if hex.MatchString(id) {
return bson.M{"_id": bson.ObjectIdHex(id)}
} else if number.MatchString(id) {
if i, err := strconv.Atoi(id); err == nil {
return bson.M{"_id": i}
}
}
return bson.M{"_id": id}
}
type queryOptions struct {
criteria Document
selector Document
/*
sort Document
skip int
limit int
*/
}
func parseQuery(query map[string][]string) (queryOptions, error) {
var err error
var options queryOptions
for key, values := range query {
switch key {
case "criteria":
if len(values) > 1 {
return options, errors.New("Can only have one criteria specified")
}
value := []byte(values[0])
if err := json.Unmarshal(value, &options.criteria); err != nil {
return options, err
}
case "selector":
if len(values) > 1 {
return options, errors.New("Can only have one selector specified")
}
value := []byte(values[0])
if err := json.Unmarshal(value, &options.selector); err != nil {
return options, err
}
}
}
return options, err
}
func parseAccept(accept string) []string {
types := strings.Split(accept, ",")
result := make([]string, 0)
for _, atype := range types {
typequal := strings.Split(atype, ";")
clean := strings.TrimSpace(typequal[0])
if !contains(result, clean) {
result = append(result, clean)
}
}
return result
}
func contains(haystack []string, needle string) bool {
for _, val := range haystack {
if val == needle {
return true
}
}
return false
}
func mediaType(accept string) string {
if accept == "" {
return "application/json"
}
var media string
types := parseAccept(accept)
switch {
case contains(types, "application/json"):
media = "application/json"
case contains(types, "text/html"):
media = "text/html"
case contains(types, "application/*"):
media = "application/json"
case contains(types, "text/*"):
media = "text/html"
case contains(types, "*/*"):
media = "application/json"
}
return media
}