/
plain.go
38 lines (32 loc) · 1.07 KB
/
plain.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
package storage
import (
"bytes"
"fmt"
)
func init() {
registerWriteParser([]string{"-", "plain"}, []string{}, &PlainParser{})
}
// PlainParser is a Parser implementation to handle plain files.
type PlainParser struct {
}
// ErrPlainParserNotImplemented is returned when you try to use the PlainParser.FromBytes func.
var ErrPlainParserNotImplemented = fmt.Errorf("PlainParser.FromBytes not implemented")
// FromBytes returns some data that is represented by the given bytes.
func (p *PlainParser) FromBytes(byteData []byte) (interface{}, error) {
return nil, ErrPlainParserNotImplemented
}
// ToBytes returns a slice of bytes that represents the given value.
func (p *PlainParser) ToBytes(value interface{}, options ...ReadWriteOption) ([]byte, error) {
buf := new(bytes.Buffer)
switch val := value.(type) {
case SingleDocument:
buf.Write([]byte(fmt.Sprintf("%v\n", val.Document())))
case MultiDocument:
for _, doc := range val.Documents() {
buf.Write([]byte(fmt.Sprintf("%v\n", doc)))
}
default:
buf.Write([]byte(fmt.Sprintf("%v\n", val)))
}
return buf.Bytes(), nil
}