mirror of
https://github.com/golang/go.git
synced 2025-10-19 11:03:18 +00:00
encoding/json/v2: restrict presence of default options
Originally, DefaultOptionsV1 and DefaultOptionsV2 represented the full set of all options with specific ones set to true or false. However, there are certain options such as WithIndent or WithMarshalers that are neither v1 or v2 specific. At some point we removed whitespace related options from the set: https://github.com/go-json-experiment/json/pull/26 This avoids DefaultOptionsV1 or DefaultOptionsV2 from affecting any previously set whitespace. However, why are whitespace options special and thus excluded from the set? What about Marshalers? As a more principaled way to address this, we restrict DefaultOptionsV1 and DefaultOptionsV2 to only be the options where the default setting changes between v1 and v2. All other options are unpopulated. This avoids a panic with GetOption(DefaultOptionsV2, WithMarshalers) since DefaultOptionsV2 previously had the presence bit for Marshalers set to true, but had no actual value. Now, the presence bit is set to false, so the value is not consulted. Fixes #75149 Change-Id: I30b45abd35404578b4135cc3bad1a1a2993cb0cf Reviewed-on: https://go-review.googlesource.com/c/go/+/710878 Reviewed-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com> Reviewed-by: Michael Pratt <mpratt@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
parent
1abc6b0204
commit
6fd8e88d07
4 changed files with 10 additions and 10 deletions
|
@ -48,16 +48,16 @@ type ArshalValues struct {
|
|||
// DefaultOptionsV2 is the set of all options that define default v2 behavior.
|
||||
var DefaultOptionsV2 = Struct{
|
||||
Flags: jsonflags.Flags{
|
||||
Presence: uint64(jsonflags.AllFlags & ^jsonflags.WhitespaceFlags),
|
||||
Values: uint64(0),
|
||||
Presence: uint64(jsonflags.DefaultV1Flags),
|
||||
Values: uint64(0), // all flags in DefaultV1Flags are false
|
||||
},
|
||||
}
|
||||
|
||||
// DefaultOptionsV1 is the set of all options that define default v1 behavior.
|
||||
var DefaultOptionsV1 = Struct{
|
||||
Flags: jsonflags.Flags{
|
||||
Presence: uint64(jsonflags.AllFlags & ^jsonflags.WhitespaceFlags),
|
||||
Values: uint64(jsonflags.DefaultV1Flags),
|
||||
Presence: uint64(jsonflags.DefaultV1Flags),
|
||||
Values: uint64(jsonflags.DefaultV1Flags), // all flags in DefaultV1Flags are true
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
@ -200,6 +200,9 @@ func TestGet(t *testing.T) {
|
|||
if v, ok := json.GetOption(opts, json.WithUnmarshalers); v != nil || ok {
|
||||
t.Errorf(`GetOption(..., WithUnmarshalers) = (%v, %v), want (nil, false)`, v, ok)
|
||||
}
|
||||
if v, ok := json.GetOption(json.DefaultOptionsV2(), json.WithMarshalers); v != nil || ok {
|
||||
t.Errorf(`GetOption(..., WithMarshalers) = (%v, %v), want (nil, false)`, v, ok)
|
||||
}
|
||||
}
|
||||
|
||||
var sink struct {
|
||||
|
|
|
@ -97,9 +97,8 @@ func GetOption[T any](opts Options, setter func(T) Options) (T, bool) {
|
|||
}
|
||||
|
||||
// DefaultOptionsV2 is the full set of all options that define v2 semantics.
|
||||
// It is equivalent to all options under [Options], [encoding/json.Options],
|
||||
// and [encoding/json/jsontext.Options] being set to false or the zero value,
|
||||
// except for the options related to whitespace formatting.
|
||||
// It is equivalent to the set of options in [encoding/json.DefaultOptionsV1]
|
||||
// all being set to false. All other options are not present.
|
||||
func DefaultOptionsV2() Options {
|
||||
return &jsonopts.DefaultOptionsV2
|
||||
}
|
||||
|
|
|
@ -227,9 +227,7 @@ type Options = jsonopts.Options
|
|||
// - [jsontext.EscapeForJS]
|
||||
// - [jsontext.PreserveRawStrings]
|
||||
//
|
||||
// All other boolean options are set to false.
|
||||
// All non-boolean options are set to the zero value,
|
||||
// except for [jsontext.WithIndent], which defaults to "\t".
|
||||
// All other options are not present.
|
||||
//
|
||||
// The [Marshal] and [Unmarshal] functions in this package are
|
||||
// semantically identical to calling the v2 equivalents with this option:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue