1
1
package toml
2
2
3
3
import (
4
+ "bytes"
4
5
"encoding"
5
6
"fmt"
6
7
"io"
@@ -18,11 +19,29 @@ type Unmarshaler interface {
18
19
}
19
20
20
21
// 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 )
23
24
return err
24
25
}
25
26
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
+
26
45
// Primitive is a TOML value that hasn't been decoded into a Go value.
27
46
//
28
47
// This type can be used for any value, which will cause decoding to be delayed.
@@ -46,23 +65,6 @@ const (
46
65
maxSafeFloat64Int = int64 (9007199254740991 ) // 2^53-1
47
66
)
48
67
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
-
66
68
// Decoder decodes TOML data.
67
69
//
68
70
// 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) {
158
160
return md , md .unify (p .mapping , rv )
159
161
}
160
162
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.)
162
168
//
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 ))
177
178
}
178
179
179
180
// unify performs a sort of type unification based on the structure of `rv`,
0 commit comments