aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/json/encode_test.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2021-12-01 12:15:45 -0500
committerRuss Cox <rsc@golang.org>2021-12-13 18:45:54 +0000
commit2580d0e08d5e9f979b943758d3c49877fb2324cb (patch)
tree3aafccfd81087734156a1778ce2321adf345f271 /src/encoding/json/encode_test.go
parent083ef5462494e81ee23316245c5d65085a3f62d9 (diff)
downloadgo-2580d0e08d5e9f979b943758d3c49877fb2324cb.tar.xz
all: gofmt -w -r 'interface{} -> any' src
And then revert the bootstrap cmd directories and certain testdata. And adjust tests as needed. Not reverting the changes in std that are bootstrapped, because some of those changes would appear in API docs, and we want to use any consistently. Instead, rewrite 'any' to 'interface{}' in cmd/dist for those directories when preparing the bootstrap copy. A few files changed as a result of running gofmt -w not because of interface{} -> any but because they hadn't been updated for the new //go:build lines. Fixes #49884. Change-Id: Ie8045cba995f65bd79c694ec77a1b3d1fe01bb09 Reviewed-on: https://go-review.googlesource.com/c/go/+/368254 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/encoding/json/encode_test.go')
-rw-r--r--src/encoding/json/encode_test.go112
1 files changed, 56 insertions, 56 deletions
diff --git a/src/encoding/json/encode_test.go b/src/encoding/json/encode_test.go
index 0dad951095..0b021f0074 100644
--- a/src/encoding/json/encode_test.go
+++ b/src/encoding/json/encode_test.go
@@ -28,8 +28,8 @@ type Optionals struct {
Slr []string `json:"slr,random"`
Slo []string `json:"slo,omitempty"`
- Mr map[string]interface{} `json:"mr"`
- Mo map[string]interface{} `json:",omitempty"`
+ Mr map[string]any `json:"mr"`
+ Mo map[string]any `json:",omitempty"`
Fr float64 `json:"fr"`
Fo float64 `json:"fo,omitempty"`
@@ -59,8 +59,8 @@ var optionalsExpected = `{
func TestOmitEmpty(t *testing.T) {
var o Optionals
o.Sw = "something"
- o.Mr = map[string]interface{}{}
- o.Mo = map[string]interface{}{}
+ o.Mr = map[string]any{}
+ o.Mo = map[string]any{}
got, err := MarshalIndent(&o, "", " ")
if err != nil {
@@ -180,16 +180,16 @@ type PointerCycle struct {
var pointerCycle = &PointerCycle{}
type PointerCycleIndirect struct {
- Ptrs []interface{}
+ Ptrs []any
}
type RecursiveSlice []RecursiveSlice
var (
pointerCycleIndirect = &PointerCycleIndirect{}
- mapCycle = make(map[string]interface{})
- sliceCycle = []interface{}{nil}
- sliceNoCycle = []interface{}{nil, nil}
+ mapCycle = make(map[string]any)
+ sliceCycle = []any{nil}
+ sliceNoCycle = []any{nil, nil}
recursiveSliceCycle = []RecursiveSlice{nil}
)
@@ -199,13 +199,13 @@ func init() {
samePointerNoCycle.Ptr2 = ptr
pointerCycle.Ptr = pointerCycle
- pointerCycleIndirect.Ptrs = []interface{}{pointerCycleIndirect}
+ pointerCycleIndirect.Ptrs = []any{pointerCycleIndirect}
mapCycle["x"] = mapCycle
sliceCycle[0] = sliceCycle
sliceNoCycle[1] = sliceNoCycle[:1]
for i := startDetectingCyclesAfter; i > 0; i-- {
- sliceNoCycle = []interface{}{sliceNoCycle}
+ sliceNoCycle = []any{sliceNoCycle}
}
recursiveSliceCycle[0] = recursiveSliceCycle
}
@@ -222,7 +222,7 @@ func TestSliceNoCycle(t *testing.T) {
}
}
-var unsupportedValues = []interface{}{
+var unsupportedValues = []any{
math.NaN(),
math.Inf(-1),
math.Inf(1),
@@ -367,15 +367,15 @@ func TestMarshalerEscaping(t *testing.T) {
func TestAnonymousFields(t *testing.T) {
tests := []struct {
- label string // Test name
- makeInput func() interface{} // Function to create input value
- want string // Expected JSON output
+ label string // Test name
+ makeInput func() any // Function to create input value
+ want string // Expected JSON output
}{{
// Both S1 and S2 have a field named X. From the perspective of S,
// it is ambiguous which one X refers to.
// This should not serialize either field.
label: "AmbiguousField",
- makeInput: func() interface{} {
+ makeInput: func() any {
type (
S1 struct{ x, X int }
S2 struct{ x, X int }
@@ -391,7 +391,7 @@ func TestAnonymousFields(t *testing.T) {
label: "DominantField",
// Both S1 and S2 have a field named X, but since S has an X field as
// well, it takes precedence over S1.X and S2.X.
- makeInput: func() interface{} {
+ makeInput: func() any {
type (
S1 struct{ x, X int }
S2 struct{ x, X int }
@@ -407,7 +407,7 @@ func TestAnonymousFields(t *testing.T) {
}, {
// Unexported embedded field of non-struct type should not be serialized.
label: "UnexportedEmbeddedInt",
- makeInput: func() interface{} {
+ makeInput: func() any {
type (
myInt int
S struct{ myInt }
@@ -418,7 +418,7 @@ func TestAnonymousFields(t *testing.T) {
}, {
// Exported embedded field of non-struct type should be serialized.
label: "ExportedEmbeddedInt",
- makeInput: func() interface{} {
+ makeInput: func() any {
type (
MyInt int
S struct{ MyInt }
@@ -430,7 +430,7 @@ func TestAnonymousFields(t *testing.T) {
// Unexported embedded field of pointer to non-struct type
// should not be serialized.
label: "UnexportedEmbeddedIntPointer",
- makeInput: func() interface{} {
+ makeInput: func() any {
type (
myInt int
S struct{ *myInt }
@@ -444,7 +444,7 @@ func TestAnonymousFields(t *testing.T) {
// Exported embedded field of pointer to non-struct type
// should be serialized.
label: "ExportedEmbeddedIntPointer",
- makeInput: func() interface{} {
+ makeInput: func() any {
type (
MyInt int
S struct{ *MyInt }
@@ -459,7 +459,7 @@ func TestAnonymousFields(t *testing.T) {
// exported fields be serialized regardless of whether the struct types
// themselves are exported.
label: "EmbeddedStruct",
- makeInput: func() interface{} {
+ makeInput: func() any {
type (
s1 struct{ x, X int }
S2 struct{ y, Y int }
@@ -476,7 +476,7 @@ func TestAnonymousFields(t *testing.T) {
// exported fields be serialized regardless of whether the struct types
// themselves are exported.
label: "EmbeddedStructPointer",
- makeInput: func() interface{} {
+ makeInput: func() any {
type (
s1 struct{ x, X int }
S2 struct{ y, Y int }
@@ -492,7 +492,7 @@ func TestAnonymousFields(t *testing.T) {
// Exported fields on embedded unexported structs at multiple levels
// of nesting should still be serialized.
label: "NestedStructAndInts",
- makeInput: func() interface{} {
+ makeInput: func() any {
type (
MyInt1 int
MyInt2 int
@@ -519,7 +519,7 @@ func TestAnonymousFields(t *testing.T) {
// the embedded fields behind it. Not properly doing so may
// result in the wrong output or reflect panics.
label: "EmbeddedFieldBehindNilPointer",
- makeInput: func() interface{} {
+ makeInput: func() any {
type (
S2 struct{ Field string }
S struct{ *S2 }
@@ -589,22 +589,22 @@ func (nm *nilTextMarshaler) MarshalText() ([]byte, error) {
// See golang.org/issue/16042 and golang.org/issue/34235.
func TestNilMarshal(t *testing.T) {
testCases := []struct {
- v interface{}
+ v any
want string
}{
{v: nil, want: `null`},
{v: new(float64), want: `0`},
- {v: []interface{}(nil), want: `null`},
+ {v: []any(nil), want: `null`},
{v: []string(nil), want: `null`},
{v: map[string]string(nil), want: `null`},
{v: []byte(nil), want: `null`},
{v: struct{ M string }{"gopher"}, want: `{"M":"gopher"}`},
{v: struct{ M Marshaler }{}, want: `{"M":null}`},
{v: struct{ M Marshaler }{(*nilJSONMarshaler)(nil)}, want: `{"M":"0zenil0"}`},
- {v: struct{ M interface{} }{(*nilJSONMarshaler)(nil)}, want: `{"M":null}`},
+ {v: struct{ M any }{(*nilJSONMarshaler)(nil)}, want: `{"M":null}`},
{v: struct{ M encoding.TextMarshaler }{}, want: `{"M":null}`},
{v: struct{ M encoding.TextMarshaler }{(*nilTextMarshaler)(nil)}, want: `{"M":"0zenil0"}`},
- {v: struct{ M interface{} }{(*nilTextMarshaler)(nil)}, want: `{"M":null}`},
+ {v: struct{ M any }{(*nilTextMarshaler)(nil)}, want: `{"M":null}`},
}
for _, tt := range testCases {
@@ -864,7 +864,7 @@ type textint int
func (i textint) MarshalText() ([]byte, error) { return tenc(`TI:%d`, i) }
-func tenc(format string, a ...interface{}) ([]byte, error) {
+func tenc(format string, a ...any) ([]byte, error) {
var buf bytes.Buffer
fmt.Fprintf(&buf, format, a...)
return buf.Bytes(), nil
@@ -877,7 +877,7 @@ func (f textfloat) MarshalText() ([]byte, error) { return tenc(`TF:%0.2f`, f) }
// Issue 13783
func TestEncodeBytekind(t *testing.T) {
testdata := []struct {
- data interface{}
+ data any
want string
}{
{byte(7), "7"},
@@ -966,7 +966,7 @@ func TestMarshalFloat(t *testing.T) {
t.Parallel()
nfail := 0
test := func(f float64, bits int) {
- vf := interface{}(f)
+ vf := any(f)
if bits == 32 {
f = float64(float32(f)) // round
vf = float32(f)
@@ -1062,25 +1062,25 @@ func TestMarshalRawMessageValue(t *testing.T) {
)
tests := []struct {
- in interface{}
+ in any
want string
ok bool
}{
// Test with nil RawMessage.
{rawNil, "null", true},
{&rawNil, "null", true},
- {[]interface{}{rawNil}, "[null]", true},
- {&[]interface{}{rawNil}, "[null]", true},
- {[]interface{}{&rawNil}, "[null]", true},
- {&[]interface{}{&rawNil}, "[null]", true},
+ {[]any{rawNil}, "[null]", true},
+ {&[]any{rawNil}, "[null]", true},
+ {[]any{&rawNil}, "[null]", true},
+ {&[]any{&rawNil}, "[null]", true},
{struct{ M RawMessage }{rawNil}, `{"M":null}`, true},
{&struct{ M RawMessage }{rawNil}, `{"M":null}`, true},
{struct{ M *RawMessage }{&rawNil}, `{"M":null}`, true},
{&struct{ M *RawMessage }{&rawNil}, `{"M":null}`, true},
- {map[string]interface{}{"M": rawNil}, `{"M":null}`, true},
- {&map[string]interface{}{"M": rawNil}, `{"M":null}`, true},
- {map[string]interface{}{"M": &rawNil}, `{"M":null}`, true},
- {&map[string]interface{}{"M": &rawNil}, `{"M":null}`, true},
+ {map[string]any{"M": rawNil}, `{"M":null}`, true},
+ {&map[string]any{"M": rawNil}, `{"M":null}`, true},
+ {map[string]any{"M": &rawNil}, `{"M":null}`, true},
+ {&map[string]any{"M": &rawNil}, `{"M":null}`, true},
{T1{rawNil}, "{}", true},
{T2{&rawNil}, `{"M":null}`, true},
{&T1{rawNil}, "{}", true},
@@ -1089,18 +1089,18 @@ func TestMarshalRawMessageValue(t *testing.T) {
// Test with empty, but non-nil, RawMessage.
{rawEmpty, "", false},
{&rawEmpty, "", false},
- {[]interface{}{rawEmpty}, "", false},
- {&[]interface{}{rawEmpty}, "", false},
- {[]interface{}{&rawEmpty}, "", false},
- {&[]interface{}{&rawEmpty}, "", false},
+ {[]any{rawEmpty}, "", false},
+ {&[]any{rawEmpty}, "", false},
+ {[]any{&rawEmpty}, "", false},
+ {&[]any{&rawEmpty}, "", false},
{struct{ X RawMessage }{rawEmpty}, "", false},
{&struct{ X RawMessage }{rawEmpty}, "", false},
{struct{ X *RawMessage }{&rawEmpty}, "", false},
{&struct{ X *RawMessage }{&rawEmpty}, "", false},
- {map[string]interface{}{"nil": rawEmpty}, "", false},
- {&map[string]interface{}{"nil": rawEmpty}, "", false},
- {map[string]interface{}{"nil": &rawEmpty}, "", false},
- {&map[string]interface{}{"nil": &rawEmpty}, "", false},
+ {map[string]any{"nil": rawEmpty}, "", false},
+ {&map[string]any{"nil": rawEmpty}, "", false},
+ {map[string]any{"nil": &rawEmpty}, "", false},
+ {&map[string]any{"nil": &rawEmpty}, "", false},
{T1{rawEmpty}, "{}", true},
{T2{&rawEmpty}, "", false},
{&T1{rawEmpty}, "{}", true},
@@ -1113,18 +1113,18 @@ func TestMarshalRawMessageValue(t *testing.T) {
// See https://golang.org/issues/14493#issuecomment-255857318
{rawText, `"foo"`, true}, // Issue6458
{&rawText, `"foo"`, true},
- {[]interface{}{rawText}, `["foo"]`, true}, // Issue6458
- {&[]interface{}{rawText}, `["foo"]`, true}, // Issue6458
- {[]interface{}{&rawText}, `["foo"]`, true},
- {&[]interface{}{&rawText}, `["foo"]`, true},
+ {[]any{rawText}, `["foo"]`, true}, // Issue6458
+ {&[]any{rawText}, `["foo"]`, true}, // Issue6458
+ {[]any{&rawText}, `["foo"]`, true},
+ {&[]any{&rawText}, `["foo"]`, true},
{struct{ M RawMessage }{rawText}, `{"M":"foo"}`, true}, // Issue6458
{&struct{ M RawMessage }{rawText}, `{"M":"foo"}`, true},
{struct{ M *RawMessage }{&rawText}, `{"M":"foo"}`, true},
{&struct{ M *RawMessage }{&rawText}, `{"M":"foo"}`, true},
- {map[string]interface{}{"M": rawText}, `{"M":"foo"}`, true}, // Issue6458
- {&map[string]interface{}{"M": rawText}, `{"M":"foo"}`, true}, // Issue6458
- {map[string]interface{}{"M": &rawText}, `{"M":"foo"}`, true},
- {&map[string]interface{}{"M": &rawText}, `{"M":"foo"}`, true},
+ {map[string]any{"M": rawText}, `{"M":"foo"}`, true}, // Issue6458
+ {&map[string]any{"M": rawText}, `{"M":"foo"}`, true}, // Issue6458
+ {map[string]any{"M": &rawText}, `{"M":"foo"}`, true},
+ {&map[string]any{"M": &rawText}, `{"M":"foo"}`, true},
{T1{rawText}, `{"M":"foo"}`, true}, // Issue6458
{T2{&rawText}, `{"M":"foo"}`, true},
{&T1{rawText}, `{"M":"foo"}`, true},