-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
43 lines (36 loc) · 1.06 KB
/
encoder.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
package tell
import (
"io"
"github.com/ionous/tell/encode"
)
// Encoder - follows the pattern of encoding/json
type Encoder encode.Encoder
// NewEncoder -
func NewEncoder(w io.Writer) *Encoder {
enc := encode.MakeEncoder(w)
return (*Encoder)(&enc)
}
// Encode - serializes the passed document to the encoder's stream
// followed by a newline character.
// tell doesnt support multiple documents in the same file,
// but this interface doesn't stop callers from trying
func (enc *Encoder) Encode(v any) (err error) {
inner := (*encode.Encoder)(enc)
return inner.Encode(v)
}
// configure how mappings are encoded
// returns self for chaining
func (enc *Encoder) SetMapper(n encode.StartCollection, c encode.Commenting) *Encoder {
inner := (*encode.Encoder)(enc)
inner.Mapper = n
inner.MapComments = c
return enc
}
// configure how sequences are encoded
// returns self for chaining
func (enc *Encoder) SetSequencer(n encode.StartCollection, c encode.Commenting) *Encoder {
inner := (*encode.Encoder)(enc)
inner.Sequencer = n
inner.SequenceComments = c
return enc
}