-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
entry.go
64 lines (57 loc) · 1.29 KB
/
entry.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
package gdstore
import (
"encoding/base64"
"errors"
"fmt"
"strings"
)
var (
ErrCannotDecodeElement = errors.New("failed to decode element")
ErrBadLine = errors.New("bad line")
)
type Entry struct {
Action Action
Key string
Value []byte
}
func (e *Entry) toLine() []byte {
return []byte(fmt.Sprintf("%s,%s,%s\n", e.Action, base64.StdEncoding.EncodeToString([]byte(e.Key)), base64.StdEncoding.EncodeToString(e.Value)))
}
func newEntry(action Action, key string, value []byte) *Entry {
return &Entry{
Action: action,
Key: key,
Value: value,
}
}
func newBulkEntries(action Action, keyValues map[string][]byte) []*Entry {
var entries []*Entry
for k, v := range keyValues {
entries = append(entries, &Entry{
Action: action,
Key: k,
Value: v,
})
}
return entries
}
func newEntryFromLine(line string) (*Entry, error) {
elements := strings.Split(line, ",")
if len(elements) != 3 {
return nil, ErrBadLine
}
keyAsBytes, err := base64.StdEncoding.DecodeString(elements[1])
if err != nil {
return nil, ErrCannotDecodeElement
}
key := string(keyAsBytes)
value, err := base64.StdEncoding.DecodeString(elements[2])
if err != nil {
return nil, ErrCannotDecodeElement
}
return &Entry{
Action: Action(elements[0]),
Key: key,
Value: value,
}, nil
}