Decoder: rename SetStrict to DisallowUnknownFields (#731)

This commit is contained in:
Thomas Pelletier
2022-01-02 14:32:34 -05:00
committed by GitHub
parent c3ba3ef97a
commit e83cf535f5
5 changed files with 15 additions and 15 deletions
+1 -1
View File
@@ -51,7 +51,7 @@ operations should not be shockingly slow. See [benchmarks](#benchmarks).
the TOML document was not prevent in the target structure. This is a great way the TOML document was not prevent in the target structure. This is a great way
to check for typos. [See example in the documentation][strict]. to check for typos. [See example in the documentation][strict].
[strict]: https://pkg.go.dev/github.com/pelletier/go-toml/v2#example-Decoder.SetStrict [strict]: https://pkg.go.dev/github.com/pelletier/go-toml/v2#example-Decoder.DisallowUnknownFields
### Contextualized errors ### Contextualized errors
+1 -1
View File
@@ -27,7 +27,7 @@ type DecodeError struct {
// corresponding field in the target value. It contains all the missing fields // corresponding field in the target value. It contains all the missing fields
// in Errors. // in Errors.
// //
// Emitted by Decoder when SetStrict(true) was called. // Emitted by Decoder when DisallowUnknownFields() was called.
type StrictMissingError struct { type StrictMissingError struct {
// One error per field that could not be found. // One error per field that could not be found.
Errors []DecodeError Errors []DecodeError
@@ -1925,7 +1925,7 @@ func decoder(doc string) *toml.Decoder {
func strictDecoder(doc string) *toml.Decoder { func strictDecoder(doc string) *toml.Decoder {
d := decoder(doc) d := decoder(doc)
d.SetStrict(true) d.DisallowUnknownFields()
return d return d
} }
+9 -8
View File
@@ -42,21 +42,22 @@ func NewDecoder(r io.Reader) *Decoder {
return &Decoder{r: r} return &Decoder{r: r}
} }
// SetStrict toggles decoding in stict mode. // DisallowUnknownFields causes the Decoder to return an error when the
// destination is a struct and the input contains a key that does not match a
// non-ignored field.
// //
// When the decoder is in strict mode, it will record fields from the document // In that case, the Decoder returns a StrictMissingError that can be used to
// that could not be set on the target value. In that case, the decoder returns // retrieve the individual errors as well as generate a human readable
// a StrictMissingError that can be used to retrieve the individual errors as // description of the missing fields.
// well as generate a human readable description of the missing fields. func (d *Decoder) DisallowUnknownFields() *Decoder {
func (d *Decoder) SetStrict(strict bool) *Decoder { d.strict = true
d.strict = strict
return d return d
} }
// Decode the whole content of r into v. // Decode the whole content of r into v.
// //
// By default, values in the document that don't exist in the target Go value // By default, values in the document that don't exist in the target Go value
// are ignored. See Decoder.SetStrict() to change this behavior. // are ignored. See Decoder.DisallowUnknownFields() to change this behavior.
// //
// When a TOML local date, time, or date-time is decoded into a time.Time, its // When a TOML local date, time, or date-time is decoded into a time.Time, its
// value is represented in time.Local timezone. Otherwise the approriate Local* // value is represented in time.Local timezone. Otherwise the approriate Local*
+3 -4
View File
@@ -16,7 +16,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func ExampleDecoder_SetStrict() { func ExampleDecoder_DisallowUnknownFields() {
type S struct { type S struct {
Key1 string Key1 string
Key3 string Key3 string
@@ -28,7 +28,7 @@ key3 = "value3"
` `
r := strings.NewReader(doc) r := strings.NewReader(doc)
d := toml.NewDecoder(r) d := toml.NewDecoder(r)
d.SetStrict(true) d.DisallowUnknownFields()
s := S{} s := S{}
err := d.Decode(&s) err := d.Decode(&s)
@@ -1901,7 +1901,7 @@ bar = 42
t.Run("strict", func(t *testing.T) { t.Run("strict", func(t *testing.T) {
r := strings.NewReader(e.input) r := strings.NewReader(e.input)
d := toml.NewDecoder(r) d := toml.NewDecoder(r)
d.SetStrict(true) d.DisallowUnknownFields()
x := e.target x := e.target
if x == nil { if x == nil {
x = &struct{}{} x = &struct{}{}
@@ -1919,7 +1919,6 @@ bar = 42
t.Run("default", func(t *testing.T) { t.Run("default", func(t *testing.T) {
r := strings.NewReader(e.input) r := strings.NewReader(e.input)
d := toml.NewDecoder(r) d := toml.NewDecoder(r)
d.SetStrict(false)
x := e.target x := e.target
if x == nil { if x == nil {
x = &struct{}{} x = &struct{}{}