-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple.go
147 lines (131 loc) · 3.12 KB
/
simple.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
package simple
import (
"bytes"
"crypto/sha256"
"encoding/gob"
"fmt"
"reflect"
"github.com/juntaki/transparent"
"github.com/pkg/errors"
)
type StorageWrapper struct {
transparent.BackendStorage
}
// Get is file read
func (f *StorageWrapper) Get(k interface{}) (interface{}, error) {
key, err := f.encodeKey(k)
if err != nil {
return nil, err
}
v, err := f.BackendStorage.Get(key)
if err != nil {
_, ok := err.(*transparent.KeyNotFoundError)
if ok {
return nil, &transparent.KeyNotFoundError{Key: k}
}
return nil, err
}
data, err := f.decodeValue(v.([]byte))
if err != nil {
return nil, err
}
return data, nil
}
// Add is file write
func (f *StorageWrapper) Add(k interface{}, v interface{}) error {
key, err := f.encodeKey(k)
if err != nil {
return err
}
data, err := f.encodeValue(v)
if err != nil {
return err
}
err = f.BackendStorage.Add(key, data)
if err != nil {
return err
}
return nil
}
// Remove is file unlink
func (f *StorageWrapper) Remove(k interface{}) error {
key, err := f.encodeKey(k)
if err != nil {
return err
}
err = f.BackendStorage.Remove(key)
if err != nil {
return err
}
return nil
}
func (f *StorageWrapper) encodeKey(k interface{}) (string, error) {
gob.Register(k)
buf := new(bytes.Buffer)
encoder := gob.NewEncoder(buf)
cause := encoder.Encode(k)
if cause != nil {
return "", errors.Wrap(cause, "failed to encode key")
}
key := fmt.Sprintf("%x", sha256.Sum256(buf.Bytes()))
return key, nil
}
func (f *StorageWrapper) encodeValue(v interface{}) ([]byte, error) {
gob.Register(v)
buf := new(bytes.Buffer)
encoder := gob.NewEncoder(buf)
cause := encoder.Encode(&v)
if cause != nil {
return nil, errors.Wrap(cause, "failed to encode value")
}
return buf.Bytes(), nil
}
func (f *StorageWrapper) decodeValue(v []byte) (interface{}, error) {
var d interface{}
buf := bytes.NewBuffer(v)
decoder := gob.NewDecoder(buf)
cause := decoder.Decode(&d)
if cause != nil {
return nil, errors.Wrap(cause, "failed to decode value")
}
return d, nil
}
// StorageInvalidKeyError means type of key is invalid for storage
type StorageInvalidKeyError struct {
Valid reflect.Type
Invalid reflect.Type
}
func (e *StorageInvalidKeyError) Error() string {
return fmt.Sprintf("%s is not supported key in the storage, use %s", e.Invalid, e.Valid)
}
// StorageInvalidValueError means type of value is invalid for storage
type StorageInvalidValueError struct {
Valid reflect.Type
Invalid reflect.Type
}
func (e *StorageInvalidValueError) Error() string {
return fmt.Sprintf("%s is not supported value in this simple storage, use %s", e.Invalid, e.Valid)
}
// Validator is
type Validator struct {
}
func (s *Validator) ValidateKey(k interface{}) (string, error) {
key, ok := k.(string)
if !ok {
return "", &StorageInvalidKeyError{
Valid: reflect.TypeOf((string)("")),
Invalid: reflect.TypeOf(k),
}
}
return key, nil
}
func (s *Validator) ValidateValue(v interface{}) ([]byte, error) {
value, ok := v.([]byte)
if !ok {
return []byte{}, &StorageInvalidValueError{
Valid: reflect.TypeOf(([]byte)("")),
Invalid: reflect.TypeOf(v),
}
}
return value, nil
}