/
collate.go
71 lines (63 loc) · 1.19 KB
/
collate.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
package collate // import "github.com/flimzy/kivik/collate"
import (
"math"
"reflect"
)
// Collation provides an interface around a CouchDB collation definition.
type Collation interface {
Eq(i, j interface{}) bool
LT(i, j interface{}) bool
LTE(i, j interface{}) bool
GT(i, j interface{}) bool
GTE(i, j interface{}) bool
}
type comparison int
const (
lt comparison = iota - 1
eq
gt
)
func (c comparison) String() string {
switch {
case c > 0:
return "greater than"
case c < 0:
return "less than"
default:
return "equal"
}
}
type couchType int
const (
couchNull couchType = iota
couchBool
couchNumber
couchString
couchArray
couchObject
)
func couchTypeOf(i interface{}) couchType {
if i == nil {
return couchNull
}
switch t := i.(type) {
case bool:
return couchBool
case float64:
if math.IsInf(t, 0) || math.IsNaN(t) {
return couchNull
}
return couchNumber
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32:
return couchNumber
case string:
return couchString
case map[string]interface{}:
return couchObject
}
switch reflect.ValueOf(i).Kind() {
case reflect.Slice, reflect.Array:
return couchArray
}
panic("unknown type")
}