2015-03-15 20:52:43 +00:00
|
|
|
package logical
|
|
|
|
|
2015-03-20 16:59:48 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
)
|
|
|
|
|
2015-03-15 20:52:43 +00:00
|
|
|
// Storage is the way that logical backends are able read/write data.
|
|
|
|
type Storage interface {
|
|
|
|
List(prefix string) ([]string, error)
|
|
|
|
Get(string) (*StorageEntry, error)
|
|
|
|
Put(*StorageEntry) error
|
|
|
|
Delete(string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// StorageEntry is the entry for an item in a Storage implementation.
|
|
|
|
type StorageEntry struct {
|
|
|
|
Key string
|
|
|
|
Value []byte
|
|
|
|
}
|
2015-03-20 16:59:48 +00:00
|
|
|
|
|
|
|
func (e *StorageEntry) DecodeJSON(out interface{}) error {
|
|
|
|
return json.Unmarshal(e.Value, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StorageEntryJSON creates a StorageEntry with a JSON-encoded value.
|
|
|
|
func StorageEntryJSON(k string, v interface{}) (*StorageEntry, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
enc := json.NewEncoder(&buf)
|
|
|
|
if err := enc.Encode(v); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &StorageEntry{
|
|
|
|
Key: k,
|
|
|
|
Value: buf.Bytes(),
|
|
|
|
}, nil
|
|
|
|
}
|