Skip to content

Commit ef65e34

Browse files
committed
Don't run Unmarshal() through Decode()
It's just the same, but with extra steps. Ref: #349
1 parent 573cad4 commit ef65e34

File tree

1 file changed

+35
-34
lines changed

1 file changed

+35
-34
lines changed

decode.go

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package toml
22

33
import (
4+
"bytes"
45
"encoding"
56
"fmt"
67
"io"
@@ -18,11 +19,29 @@ type Unmarshaler interface {
1819
}
1920

2021
// Unmarshal decodes the contents of `p` in TOML format into a pointer `v`.
21-
func Unmarshal(p []byte, v interface{}) error {
22-
_, err := Decode(string(p), v)
22+
func Unmarshal(data []byte, v interface{}) error {
23+
_, err := NewDecoder(bytes.NewReader(data)).Decode(v)
2324
return err
2425
}
2526

27+
// Decode the TOML data in to the pointer v.
28+
//
29+
// See the documentation on Decoder for a description of the decoding process.
30+
func Decode(data string, v interface{}) (MetaData, error) {
31+
return NewDecoder(strings.NewReader(data)).Decode(v)
32+
}
33+
34+
// DecodeFile is just like Decode, except it will automatically read the
35+
// contents of the file at path and decode it for you.
36+
func DecodeFile(path string, v interface{}) (MetaData, error) {
37+
fp, err := os.Open(path)
38+
if err != nil {
39+
return MetaData{}, err
40+
}
41+
defer fp.Close()
42+
return NewDecoder(fp).Decode(v)
43+
}
44+
2645
// Primitive is a TOML value that hasn't been decoded into a Go value.
2746
//
2847
// This type can be used for any value, which will cause decoding to be delayed.
@@ -46,23 +65,6 @@ const (
4665
maxSafeFloat64Int = int64(9007199254740991) // 2^53-1
4766
)
4867

49-
// PrimitiveDecode is just like the other `Decode*` functions, except it
50-
// decodes a TOML value that has already been parsed. Valid primitive values
51-
// can *only* be obtained from values filled by the decoder functions,
52-
// including this method. (i.e., `v` may contain more `Primitive`
53-
// values.)
54-
//
55-
// Meta data for primitive values is included in the meta data returned by
56-
// the `Decode*` functions with one exception: keys returned by the Undecoded
57-
// method will only reflect keys that were decoded. Namely, any keys hidden
58-
// behind a Primitive will be considered undecoded. Executing this method will
59-
// update the undecoded keys in the meta data. (See the example.)
60-
func (md *MetaData) PrimitiveDecode(primValue Primitive, v interface{}) error {
61-
md.context = primValue.context
62-
defer func() { md.context = nil }()
63-
return md.unify(primValue.undecoded, rvalue(v))
64-
}
65-
6668
// Decoder decodes TOML data.
6769
//
6870
// TOML tables correspond to Go structs or maps (dealer's choice – they can be
@@ -158,22 +160,21 @@ func (dec *Decoder) Decode(v interface{}) (MetaData, error) {
158160
return md, md.unify(p.mapping, rv)
159161
}
160162

161-
// Decode the TOML data in to the pointer v.
163+
// PrimitiveDecode is just like the other `Decode*` functions, except it
164+
// decodes a TOML value that has already been parsed. Valid primitive values
165+
// can *only* be obtained from values filled by the decoder functions,
166+
// including this method. (i.e., `v` may contain more `Primitive`
167+
// values.)
162168
//
163-
// See the documentation on Decoder for a description of the decoding process.
164-
func Decode(data string, v interface{}) (MetaData, error) {
165-
return NewDecoder(strings.NewReader(data)).Decode(v)
166-
}
167-
168-
// DecodeFile is just like Decode, except it will automatically read the
169-
// contents of the file at path and decode it for you.
170-
func DecodeFile(path string, v interface{}) (MetaData, error) {
171-
fp, err := os.Open(path)
172-
if err != nil {
173-
return MetaData{}, err
174-
}
175-
defer fp.Close()
176-
return NewDecoder(fp).Decode(v)
169+
// Meta data for primitive values is included in the meta data returned by
170+
// the `Decode*` functions with one exception: keys returned by the Undecoded
171+
// method will only reflect keys that were decoded. Namely, any keys hidden
172+
// behind a Primitive will be considered undecoded. Executing this method will
173+
// update the undecoded keys in the meta data. (See the example.)
174+
func (md *MetaData) PrimitiveDecode(primValue Primitive, v interface{}) error {
175+
md.context = primValue.context
176+
defer func() { md.context = nil }()
177+
return md.unify(primValue.undecoded, rvalue(v))
177178
}
178179

179180
// unify performs a sort of type unification based on the structure of `rv`,

0 commit comments

Comments
 (0)