forked from ssdb/gossdb
/
value.go
207 lines (185 loc) · 3.91 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package ssdb
import (
"bytes"
"fmt"
"strconv"
"strings"
"time"
)
// Values Value Slice
type Values []Value
func NewValues(arg []interface{}) (Values, error) {
vs := make(Values, 0, len(arg))
for _, v := range arg {
d, err := NewValue(v)
if err != nil {
return nil, err
}
vs = append(vs, d)
}
return vs, nil
}
// String return first value
func (v Values) First() Value {
if len(v) == 0 {
return nil
}
return v[0]
}
// String return string
func (v Values) String() string {
if len(v) == 0 {
return ""
}
return strings.Join(v.Strings(), "\n")
}
// Strings get []string
func (v Values) Strings() []string {
if len(v) == 0 {
return nil
}
s := make([]string, 0, len(v))
for _, v := range v {
s = append(s, v.String())
}
return s
}
// MapStringInt get map[string]int64
func (v Values) MapStringInt() map[string]int64 {
if len(v) == 0 {
return nil
}
val := map[string]int64{}
size := len(v)
for i := 0; i+1 < size; i += 2 {
val[v[i].String()] = v[i+1].Int()
}
return val
}
// MapStringValue get map[string]Value
func (v Values) MapStringValue() map[string]Value {
if len(v) == 0 {
return nil
}
val := map[string]Value{}
size := len(v)
for i := 0; i+1 < size; i += 2 {
val[v[i].String()] = v[i+1]
}
return val
}
// Pairs get original key-value pairs
func (v Values) Pairs() Pairs {
if len(v) == 0 {
return nil
}
val := make(Pairs, 0, len(v)/2)
size := len(v)
for i := 0; i+1 < size; i += 2 {
val = append(val, Pair{
Key: v[i],
Value: v[i+1],
})
}
return val
}
// Value return val
type Value []byte
func NewValue(arg interface{}) (Value, error) {
switch arg := arg.(type) {
case Value:
return arg, nil
case time.Duration:
return Value(strconv.AppendUint(nil, uint64(arg/time.Second), 10)), nil
case fmt.Stringer:
return Value(arg.String()), nil
case string:
return Value(arg), nil
case []byte:
return Value(arg), nil
case int:
return Value(strconv.AppendInt(nil, int64(arg), 10)), nil
case int8:
return Value(strconv.AppendInt(nil, int64(arg), 10)), nil
case int16:
return Value(strconv.AppendInt(nil, int64(arg), 10)), nil
case int32:
return Value(strconv.AppendInt(nil, int64(arg), 10)), nil
case int64:
return Value(strconv.AppendInt(nil, int64(arg), 10)), nil
case uint:
return Value(strconv.AppendUint(nil, uint64(arg), 10)), nil
case uint8:
return Value(strconv.AppendUint(nil, uint64(arg), 10)), nil
case uint16:
return Value(strconv.AppendUint(nil, uint64(arg), 10)), nil
case uint32:
return Value(strconv.AppendUint(nil, uint64(arg), 10)), nil
case uint64:
return Value(strconv.AppendUint(nil, uint64(arg), 10)), nil
case float32:
return Value(strconv.AppendFloat(nil, float64(arg), 'f', -1, 64)), nil
case float64:
return Value(strconv.AppendFloat(nil, float64(arg), 'f', -1, 64)), nil
case bool:
if arg {
return one, nil
} else {
return zero, nil
}
case nil:
return Value(""), nil
default:
return nil, fmt.Errorf("error type")
}
}
// String
func (v Value) String() string {
if len(v) == 0 {
return ""
}
return string(v)
}
// Duration get time.Duration
func (v Value) Duration() time.Duration {
return time.Duration(v.Int()) * time.Second
}
// Int get int
func (v Value) Int() int64 {
i, _ := strconv.ParseInt(string(v), 0, 0)
return i
}
// Uint get uint
func (v Value) Uint() uint64 {
i, _ := strconv.ParseUint(string(v), 0, 0)
return i
}
// Float get float
func (v Value) Float() float64 {
i, _ := strconv.ParseFloat(string(v), 0)
return i
}
// Bool get bool
func (v Value) Bool() bool {
i, _ := strconv.ParseBool(string(v))
return i
}
// Bytes get bytes
func (v Value) Bytes() []byte {
return []byte(v)
}
// IsEmpty is empty
func (v Value) IsEmpty() bool {
return len(v) == 0
}
// Equal value equal
func (v Value) Equal(y Value) bool {
return bytes.Equal([]byte(v), []byte(y))
}
// Pair the original key-value pair
type Pair struct {
Key Value
Value Value
}
// Pairs is the list of pair
type Pairs []Pair