Skip to content

Commit 1e2c053

Browse files
committed
Undeprecate PrimitiveDecode and MetaData.PrimitiveDecode()
Updates #404
1 parent f8f7e48 commit 1e2c053

File tree

5 files changed

+35
-41
lines changed

5 files changed

+35
-41
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"test": {
66
"strategy": {
77
"matrix": {
8-
"go-version": ["1.18.x", "1.21.x"],
8+
"go-version": ["1.18.x", "1.22.x"],
99
"os": ["ubuntu-latest", "macos-latest", "windows-latest"]
1010
}
1111
},

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ See the [releases page](https://github.com/BurntSushi/toml/releases) for a
99
changelog; this information is also in the git tag annotations (e.g. `git show
1010
v0.4.0`).
1111

12-
This library requires Go 1.13 or newer; add it to your go.mod with:
12+
This library requires Go 1.18 or newer; add it to your go.mod with:
1313

1414
% go get github.com/BurntSushi/toml@latest
1515

_example/example.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ times = [
2323
# Durations.
2424
duration = ["4m49s", "8m03s", "1231h15m55s"]
2525

26-
# Table with inline tables.
26+
# Array with inline tables.
2727
distros = [
2828
{name = "Arch Linux", packages = "pacman"},
2929
{name = "Void Linux", packages = "xbps"},

decode.go

+32
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ func DecodeFS(fsys fs.FS, path string, v any) (MetaData, error) {
5757
return NewDecoder(fp).Decode(v)
5858
}
5959

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+
6076
// The significand precision for float32 and float64 is 24 and 53 bits; this is
6177
// the range a natural number can be stored in a float without loss of data.
6278
const (
@@ -164,6 +180,22 @@ func (dec *Decoder) Decode(v any) (MetaData, error) {
164180
return md, md.unify(p.mapping, rv)
165181
}
166182

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+
167199
// unify performs a sort of type unification based on the structure of `rv`,
168200
// which is the client representation.
169201
//

deprecated.go

-38
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,3 @@ func PrimitiveDecode(primValue Primitive, v any) error {
2727
md := MetaData{decoded: make(map[string]struct{})}
2828
return md.unify(primValue.undecoded, rvalue(v))
2929
}
30-
31-
// Primitive is a TOML value that hasn't been decoded into a Go value.
32-
//
33-
// This type can be used for any value, which will cause decoding to be delayed.
34-
// You can use [PrimitiveDecode] to "manually" decode these values.
35-
//
36-
// NOTE: The underlying representation of a `Primitive` value is subject to
37-
// change. Do not rely on it.
38-
//
39-
// NOTE: Primitive values are still parsed, so using them will only avoid the
40-
// overhead of reflection. They can be useful when you don't know the exact type
41-
// of TOML data until runtime.
42-
//
43-
// Deprecated: use Marshaler interface for customer decoding. Or "any"
44-
// parameters for varying types.
45-
type Primitive struct {
46-
undecoded any
47-
context Key
48-
}
49-
50-
// PrimitiveDecode is just like the other Decode* functions, except it decodes a
51-
// TOML value that has already been parsed. Valid primitive values can *only* be
52-
// obtained from values filled by the decoder functions, including this method.
53-
// (i.e., v may contain more [Primitive] values.)
54-
//
55-
// Meta data for primitive values is included in the meta data returned by the
56-
// Decode* functions with one exception: keys returned by the Undecoded method
57-
// will only reflect keys that were decoded. Namely, any keys hidden behind a
58-
// Primitive will be considered undecoded. Executing this method will update the
59-
// undecoded keys in the meta data. (See the example.)
60-
//
61-
// Deprecated: use Marshaler interface for customer decoding. Or "any"
62-
// parameters for varying types.
63-
func (md *MetaData) PrimitiveDecode(primValue Primitive, v any) error {
64-
md.context = primValue.context
65-
defer func() { md.context = nil }()
66-
return md.unify(primValue.undecoded, rvalue(v))
67-
}

0 commit comments

Comments
 (0)