forked from cayleygraph/cayley
/
value.go
179 lines (159 loc) · 3.46 KB
/
value.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package nosql
import (
"bytes"
"strings"
"time"
)
// Value is a interface that limits a set of types that nosql database can handle.
type Value interface {
isValue()
}
// Document is a type of item stored in nosql database.
type Document map[string]Value
func (Document) isValue() {}
// String is an UTF8 string value.
type String string
func (String) isValue() {}
// Int is an int value.
//
// Some databases might not distinguish Int value from Float.
// In this case implementation will take care of converting it to a correct type.
type Int int64
func (Int) isValue() {}
// Float is an floating point value.
//
// Some databases might not distinguish Int value from Float.
// In this case the package will take care of converting it to a correct type.
type Float float64
func (Float) isValue() {}
// Bool is a boolean value.
type Bool bool
func (Bool) isValue() {}
// Time is a timestamp value.
//
// Some databases has no type to represent time values.
// In this case string/json representation can be used and package will take care of converting it.
type Time time.Time
func (Time) isValue() {}
// Bytes is a raw binary data.
//
// Some databases has no type to represent binary data.
// In this case base64 representation can be used and package will take care of converting it.
type Bytes []byte
func (Bytes) isValue() {}
// Strings is an array of strings. Used mostly to store Keys.
type Strings []string
func (Strings) isValue() {}
// ValuesEqual returns true if values are strictly equal.
func ValuesEqual(v1, v2 Value) bool {
switch v1 := v2.(type) {
case Document:
v2, ok := v2.(Document)
if !ok || len(v1) != len(v2) {
return false
}
for k, s1 := range v1 {
if s2, ok := v2[k]; !ok || !ValuesEqual(s1, s2) {
return false
}
}
return true
case Strings:
v2, ok := v2.(Strings)
if !ok || len(v1) != len(v2) {
return false
}
for i := range v1 {
if v1[i] != v2[i] {
return false
}
}
return true
case Bytes:
v2, ok := v2.(Bytes)
if !ok || len(v1) != len(v2) {
return false
}
return bytes.Equal(v1, v2)
case Time:
v2, ok := v2.(Time)
if !ok {
return false
}
return time.Time(v1).Equal(time.Time(v2))
}
return v1 == v2
}
// CompareValues return 0 if values are equal, positive value if first value sorts after second, and negative otherwise.
func CompareValues(v1, v2 Value) int {
switch v1 := v1.(type) {
case Document:
v2, ok := v2.(Document)
if !ok {
return -1
} else if len(v1) != len(v2) {
return len(v1) - len(v2)
}
return -1 // TODO: implement proper sorting?
case Strings:
v2, ok := v2.(Strings)
if !ok {
return -1
} else if len(v1) != len(v2) {
return len(v1) - len(v2)
}
for i := range v1 {
if dn := CompareValues(String(v1[i]), String(v2[i])); dn != 0 {
return dn
}
}
return 0
case Bytes:
v2, ok := v2.(Bytes)
if !ok {
return -1
}
return bytes.Compare(v1, v2)
case Time:
v2, ok := v2.(Time)
if !ok {
return -1
}
t1, t2 := time.Time(v1), time.Time(v2)
if t1.Equal(t2) {
return 0
} else if t1.Before(t2) {
return -1
}
return +1
case String:
v2, ok := v2.(String)
if !ok {
return -1
}
return strings.Compare(string(v1), string(v2))
case Int:
v2, ok := v2.(Int)
if !ok {
return -1
}
if v1 == v2 {
return 0
} else if v1 < v2 {
return -1
}
return +1
case Float:
v2, ok := v2.(Float)
if !ok {
return -1
}
if v1 == v2 {
return 0
} else if v1 < v2 {
return -1
}
return +1
}
return -1
}