-
Notifications
You must be signed in to change notification settings - Fork 5
/
marshal.go
62 lines (51 loc) · 1.46 KB
/
marshal.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
package kdl
import (
"bytes"
"io"
"github.com/sblinch/kdl-go/document"
"github.com/sblinch/kdl-go/internal/generator"
"github.com/sblinch/kdl-go/internal/marshaler"
)
// Marshaler provides an interface for custom marshaling of a Go type into a Node
type Marshaler interface {
MarshalKDL(node *document.Node) error
}
// ValueMarshaler provides an interface for custom marshaling of a Go type into a Value (such as a node argument or
// property)
type ValueMarshaler interface {
MarshalKDLValue(value *document.Value) error
}
// Encoder implements an encoder for KDL
type Encoder struct {
w io.Writer
Options marshaler.MarshalOptions
}
// Encode encodes v into KDL and writes it to the Encoder's writer, and returns a non-nil error on failure
func (e *Encoder) Encode(v interface{}) error {
doc := document.New()
if err := marshaler.MarshalWithOptions(v, doc, e.Options); err != nil {
return err
}
g := generator.New(e.w)
if err := g.Generate(doc); err != nil {
return err
}
return nil
}
// NewEncoder creates a new Encoder that writes to w
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{w: w}
}
// Marshal returns the KDL representation of v, or a non-nil error on failure
func Marshal(v interface{}) ([]byte, error) {
doc := document.New()
if err := marshaler.Marshal(v, doc); err != nil {
return nil, err
}
b := bytes.Buffer{}
g := generator.New(&b)
if err := g.Generate(doc); err != nil {
return nil, err
}
return b.Bytes(), nil
}