From 2714786b3791d1eed1361c877ab8fea6b4421af7 Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Mon, 29 Mar 2021 21:30:41 -0400 Subject: [PATCH] Add decoder interface --- unmarshaler.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/unmarshaler.go b/unmarshaler.go index 59c6334..bb1aff2 100644 --- a/unmarshaler.go +++ b/unmarshaler.go @@ -3,6 +3,8 @@ package toml import ( "encoding" "fmt" + "io" + "io/ioutil" "reflect" "time" @@ -17,6 +19,28 @@ func Unmarshal(data []byte, v interface{}) error { return d.FromParser(&p, v) } +// Decoder reads and decode a TOML document from an input stream. +type Decoder struct { + r io.Reader +} + +// NewDecoder creates a new Decoder that will read from r. +func NewDecoder(r io.Reader) *Decoder { + return &Decoder{r: r} +} + +// Decode the whole content of r into v. +func (d *Decoder) Decode(v interface{}) error { + b, err := ioutil.ReadAll(d.r) + if err != nil { + return err + } + p := parser{} + p.Reset(b) + dec := decoder{} + return dec.FromParser(&p, v) +} + type decoder struct { // Tracks position in Go arrays. arrayIndexes map[reflect.Value]int