-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.go
More file actions
37 lines (30 loc) · 894 Bytes
/
encode.go
File metadata and controls
37 lines (30 loc) · 894 Bytes
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
package msgpack
import (
"io"
)
// Encoder defines an interface for types which are able to encode
// themselves into an MessagePack encoding.
type Encoder interface {
EncodeMsgpack(w *Writer) error
}
// Encode encodes v into the MessagePack encoding and writes it to w.
func Encode(w io.Writer, v Encoder) error {
return v.EncodeMsgpack(NewWriter(w))
}
// Marshal encodes v into the MessagePack encoding and returns its encoding.
func Marshal(v Encoder) ([]byte, error) {
return AppendMarshal(v, nil)
}
// AppendMarshal encodes v into the MessagePack encoding and appends it to buf.
func AppendMarshal(v Encoder, buf []byte) ([]byte, error) {
appender := &byteAppender{buf: buf}
err := Encode(appender, v)
return appender.buf, err
}
type byteAppender struct {
buf []byte
}
func (a *byteAppender) Write(p []byte) (int, error) {
a.buf = append(a.buf, p...)
return len(p), nil
}