@@ -57,6 +57,22 @@ func DecodeFS(fsys fs.FS, path string, v any) (MetaData, error) {
57
57
return NewDecoder (fp ).Decode (v )
58
58
}
59
59
60
+ // Primitive is a TOML value that hasn't been decoded into a Go value.
61
+ //
62
+ // This type can be used for any value, which will cause decoding to be delayed.
63
+ // You can use [PrimitiveDecode] to "manually" decode these values.
64
+ //
65
+ // NOTE: The underlying representation of a `Primitive` value is subject to
66
+ // change. Do not rely on it.
67
+ //
68
+ // NOTE: Primitive values are still parsed, so using them will only avoid the
69
+ // overhead of reflection. They can be useful when you don't know the exact type
70
+ // of TOML data until runtime.
71
+ type Primitive struct {
72
+ undecoded any
73
+ context Key
74
+ }
75
+
60
76
// The significand precision for float32 and float64 is 24 and 53 bits; this is
61
77
// the range a natural number can be stored in a float without loss of data.
62
78
const (
@@ -164,6 +180,22 @@ func (dec *Decoder) Decode(v any) (MetaData, error) {
164
180
return md , md .unify (p .mapping , rv )
165
181
}
166
182
183
+ // PrimitiveDecode is just like the other Decode* functions, except it decodes a
184
+ // TOML value that has already been parsed. Valid primitive values can *only* be
185
+ // obtained from values filled by the decoder functions, including this method.
186
+ // (i.e., v may contain more [Primitive] values.)
187
+ //
188
+ // Meta data for primitive values is included in the meta data returned by the
189
+ // Decode* functions with one exception: keys returned by the Undecoded method
190
+ // will only reflect keys that were decoded. Namely, any keys hidden behind a
191
+ // Primitive will be considered undecoded. Executing this method will update the
192
+ // undecoded keys in the meta data. (See the example.)
193
+ func (md * MetaData ) PrimitiveDecode (primValue Primitive , v any ) error {
194
+ md .context = primValue .context
195
+ defer func () { md .context = nil }()
196
+ return md .unify (primValue .undecoded , rvalue (v ))
197
+ }
198
+
167
199
// unify performs a sort of type unification based on the structure of `rv`,
168
200
// which is the client representation.
169
201
//
0 commit comments