-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathunmarshal.go
53 lines (45 loc) · 1.42 KB
/
unmarshal.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
package kdl
import (
"io"
"github.com/sblinch/kdl-go/document"
"github.com/sblinch/kdl-go/internal/marshaler"
"github.com/sblinch/kdl-go/internal/tokenizer"
)
// Unmarshaler provides an interface for custom unmarshaling of a node into a Go type
type Unmarshaler interface {
UnmarshalKDL(node *document.Node) error
}
// ValueUnmarshaler provides an interface for custom unmarshaling of a Value (such as a node argument or property) into
// a Go type
type ValueUnmarshaler interface {
UnmarshalKDLValue(value *document.Value) error
}
// Decoder implements a decoder for KDL
type Decoder struct {
r io.Reader
Options marshaler.UnmarshalOptions
}
// Decode decodes KDL from the Decoder's reader into v; v must contain a pointer type. Returns a non-nil error on
// failure.
func (d *Decoder) Decode(v interface{}) error {
s := tokenizer.New(d.r)
s.RelaxedNonCompliant = d.Options.RelaxedNonCompliant
if doc, err := parse(s); err != nil {
return err
} else {
return marshaler.UnmarshalWithOptions(doc, v, d.Options)
}
}
// NewDecoder returns a Decoder that reads from r
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{r: r}
}
// Unmarshal unmarshals KDL from data into v; v must contain a pointer type. Returns a non-nil error on failure.
func Unmarshal(data []byte, v interface{}) error {
s := tokenizer.NewSlice(data)
if doc, err := parse(s); err != nil {
return err
} else {
return marshaler.Unmarshal(doc, v)
}
}