-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
encode.go
56 lines (48 loc) · 1.16 KB
/
encode.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
package kv
import (
"errors"
"strings"
"github.com/influxdata/influxdb/v2"
)
// EncodeFn returns an encoding when called. Closures are your friend here.
type EncodeFn func() ([]byte, error)
// Encode concatenates a list of encodings together.
func Encode(encodings ...EncodeFn) EncodeFn {
return func() ([]byte, error) {
var key []byte
for _, enc := range encodings {
part, err := enc()
if err != nil {
return key, err
}
key = append(key, part...)
}
return key, nil
}
}
// EncString encodes a string.
func EncString(str string) EncodeFn {
return func() ([]byte, error) {
return []byte(str), nil
}
}
// EncStringCaseInsensitive encodes a string and makes it case insensitive by lower casing
// everything.
func EncStringCaseInsensitive(str string) EncodeFn {
return EncString(strings.ToLower(str))
}
// EncID encodes an influx ID.
func EncID(id influxdb.ID) EncodeFn {
return func() ([]byte, error) {
if id == 0 {
return nil, errors.New("no ID was provided")
}
return id.Encode()
}
}
// EncBytes is a basic pass through for providing raw bytes.
func EncBytes(b []byte) EncodeFn {
return func() ([]byte, error) {
return b, nil
}
}