all: gofmt -w -r 'interface{} -> any' (#2874)

This commit is contained in:
Ariel Mashraki
2022-08-19 18:23:04 +03:00
committed by GitHub
parent b6c185a660
commit 2c63d1d70e
619 changed files with 3449 additions and 3449 deletions

View File

@@ -66,7 +66,7 @@ func BenchmarkMarshalInterface(b *testing.B) {
b.Fatal(err)
}
var obj interface{}
var obj any
if err = jsoniter.Unmarshal(data, &obj); err != nil {
b.Fatal(err)
}
@@ -87,7 +87,7 @@ func BenchmarkUnmarshalInterface(b *testing.B) {
b.Fatal(err)
}
var obj interface{}
var obj any
b.ResetTimer()
for n := 0; n < b.N; n++ {

View File

@@ -18,19 +18,19 @@ type decodeExtension struct {
// Unmarshal parses the graphson encoded data and stores the result
// in the value pointed to by v.
func Unmarshal(data []byte, v interface{}) error {
func Unmarshal(data []byte, v any) error {
return config.Unmarshal(data, v)
}
// UnmarshalFromString parses the graphson encoded str and stores the result
// in the value pointed to by v.
func UnmarshalFromString(str string, v interface{}) error {
func UnmarshalFromString(str string, v any) error {
return config.UnmarshalFromString(str, v)
}
// Decoder defines a graphson decoder.
type Decoder interface {
Decode(interface{}) error
Decode(any) error
}
// NewDecoder create a graphson decoder.

View File

@@ -17,18 +17,18 @@ type encodeExtension struct {
}
// Marshal returns the graphson encoding of v.
func Marshal(v interface{}) ([]byte, error) {
func Marshal(v any) ([]byte, error) {
return config.Marshal(v)
}
// MarshalToString returns the graphson encoding of v as string.
func MarshalToString(v interface{}) (string, error) {
func MarshalToString(v any) (string, error) {
return config.MarshalToString(v)
}
// Encoder defines a graphson encoder.
type Encoder interface {
Encode(interface{}) error
Encode(any) error
}
// NewEncoder create a graphson encoder.

View File

@@ -12,16 +12,16 @@ import (
)
// EncoderOfError returns a value encoder which always fails to encode.
func (encodeExtension) EncoderOfError(format string, args ...interface{}) jsoniter.ValEncoder {
func (encodeExtension) EncoderOfError(format string, args ...any) jsoniter.ValEncoder {
return decoratorOfError(format, args...)
}
// DecoderOfError returns a value decoder which always fails to decode.
func (decodeExtension) DecoderOfError(format string, args ...interface{}) jsoniter.ValDecoder {
func (decodeExtension) DecoderOfError(format string, args ...any) jsoniter.ValDecoder {
return decoratorOfError(format, args...)
}
func decoratorOfError(format string, args ...interface{}) errorCodec {
func decoratorOfError(format string, args ...any) errorCodec {
err := fmt.Errorf(format, args...)
return errorCodec{err}
}

View File

@@ -55,7 +55,7 @@ func (dec efaceDecoder) decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
it := config.BorrowIterator(data)
defer config.ReturnIterator(it)
var val interface{}
var val any
if rtype != nil {
val = rtype.New()
it.ReadVal(val)
@@ -120,13 +120,13 @@ func (efaceDecoder) reflectType(typ Type) reflect2.Type {
}
func (efaceDecoder) reflectSlice(data []byte) (reflect2.Type, error) {
var elem interface{}
if err := Unmarshal(data, &[...]*interface{}{&elem}); err != nil {
var elem any
if err := Unmarshal(data, &[...]*any{&elem}); err != nil {
return nil, fmt.Errorf("cannot read first list element: %w", err)
}
if elem == nil {
return reflect2.TypeOf([]interface{}{}), nil
return reflect2.TypeOf([]any{}), nil
}
sliceType := reflect.SliceOf(reflect.TypeOf(elem))
@@ -134,16 +134,16 @@ func (efaceDecoder) reflectSlice(data []byte) (reflect2.Type, error) {
}
func (efaceDecoder) reflectMap(data []byte) (reflect2.Type, error) {
var key, elem interface{}
var key, elem any
if err := Unmarshal(
bytes.Replace(data, []byte(mapType), []byte(listType), 1),
&[...]*interface{}{&key, &elem},
&[...]*any{&key, &elem},
); err != nil {
return nil, fmt.Errorf("cannot unmarshal first map item: %w", err)
}
if key == nil {
return reflect2.TypeOf(map[interface{}]interface{}{}), nil
return reflect2.TypeOf(map[any]any{}), nil
} else if elem == nil {
return nil, errors.New("expect map element, but found only key")
}

View File

@@ -16,7 +16,7 @@ func TestDecodeInterface(t *testing.T) {
tests := []struct {
name string
in string
want interface{}
want any
wantErr bool
}{
{
@@ -155,7 +155,7 @@ func TestDecodeInterface(t *testing.T) {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
var got interface{}
var got any
err := UnmarshalFromString(tc.in, &got)
if !tc.wantErr {
require.NoError(t, err)
@@ -170,14 +170,14 @@ func TestDecodeInterface(t *testing.T) {
func TestDecodeInterfaceSlice(t *testing.T) {
tests := []struct {
in string
want interface{}
want any
}{
{
in: `{
"@type": "g:List",
"@value": []
}`,
want: []interface{}{},
want: []any{},
},
{
in: `{
@@ -219,7 +219,7 @@ func TestDecodeInterfaceSlice(t *testing.T) {
tc := tc
t.Run(fmt.Sprintf("%T", tc.want), func(t *testing.T) {
t.Parallel()
var got interface{}
var got any
err := UnmarshalFromString(tc.in, &got)
require.NoError(t, err)
assert.Equal(t, tc.want, got)
@@ -230,14 +230,14 @@ func TestDecodeInterfaceSlice(t *testing.T) {
func TestDecodeInterfaceMap(t *testing.T) {
tests := []struct {
in string
want interface{}
want any
}{
{
in: `{
"@type": "g:Map",
"@value": []
}`,
want: map[interface{}]interface{}{},
want: map[any]any{},
},
{
in: `{
@@ -314,7 +314,7 @@ func TestDecodeInterfaceMap(t *testing.T) {
tc := tc
t.Run(fmt.Sprintf("%T", tc.want), func(t *testing.T) {
t.Parallel()
var got interface{}
var got any
err := UnmarshalFromString(tc.in, &got)
require.NoError(t, err)
assert.Equal(t, tc.want, got)
@@ -339,11 +339,11 @@ func TestDecodeInterfaceObject(t *testing.T) {
data, err := Marshal(book)
require.NoError(t, err)
var v interface{}
var v any
err = Unmarshal(data, &v)
require.NoError(t, err)
obj := v.(map[string]interface{})
obj := v.(map[string]any)
assert.Equal(t, book.ID, obj["id"])
assert.Equal(t, book.Title, obj["title"])
assert.Equal(t, book.Author, obj["author"])

View File

@@ -17,7 +17,7 @@ import (
func TestEncodeMap(t *testing.T) {
tests := []struct {
name string
in interface{}
in any
want string
}{
{
@@ -47,7 +47,7 @@ func TestEncodeMap(t *testing.T) {
},
{
name: "mixed",
in: map[string]interface{}{
in: map[string]any{
"byte": byte('a'),
"string": "str",
"slice": []int{1, 2, 3},
@@ -119,11 +119,11 @@ func TestEncodeMap(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "g:Map", jsoniter.Get(data, "@type").ToString())
var want []interface{}
var want []any
err = jsoniter.UnmarshalFromString(tc.want, &want)
require.NoError(t, err)
got, ok := jsoniter.Get(data, "@value").GetInterface().([]interface{})
got, ok := jsoniter.Get(data, "@value").GetInterface().([]any)
require.True(t, ok)
assert.ElementsMatch(t, want, got)
})
@@ -134,7 +134,7 @@ func TestDecodeMap(t *testing.T) {
tests := []struct {
name string
in string
want interface{}
want any
}{
{
name: "empty",

View File

@@ -23,7 +23,7 @@ func TestMarshalerEncode(t *testing.T) {
call := m.On("MarshalGraphson").Return(want, nil)
defer m.AssertExpectations(t)
tests := []interface{}{m, &m, func() *Marshaler { marshaler := Marshaler(m); return &marshaler }(), Marshaler(nil)}
tests := []any{m, &m, func() *Marshaler { marshaler := Marshaler(m); return &marshaler }(), Marshaler(nil)}
call.Times(len(tests) - 1)
for _, tc := range tests {

View File

@@ -17,7 +17,7 @@ import (
func TestEncodeNative(t *testing.T) {
tests := []struct {
in interface{}
in any
want string
wantErr bool
}{
@@ -142,7 +142,7 @@ func TestEncodeNative(t *testing.T) {
}`,
},
{
in: func() interface{} { v := int16(6116); return &v }(),
in: func() any { v := int16(6116); return &v }(),
want: `{
"@type": "gx:Int16",
"@value": 6116
@@ -177,7 +177,7 @@ func TestEncodeNative(t *testing.T) {
func TestDecodeNative(t *testing.T) {
tests := []struct {
in string
want interface{}
want any
}{
{
in: `{"@type": "g:Float", "@value": 3.14}`,

View File

@@ -25,7 +25,7 @@ func TestEncodeArray(t *testing.T) {
func TestEncodeSlice(t *testing.T) {
tests := []struct {
in interface{}
in any
want string
}{
{
@@ -95,7 +95,7 @@ func TestEncodeSlice(t *testing.T) {
func TestDecodeSlice(t *testing.T) {
tests := []struct {
in string
want interface{}
want any
}{
{
in: `{
@@ -170,7 +170,7 @@ func TestDecodeBadSlice(t *testing.T) {
tests := []struct {
name string
in string
new func() interface{}
new func() any
}{
{
name: "TypeMismatch",
@@ -187,7 +187,7 @@ func TestDecodeBadSlice(t *testing.T) {
}
]
}`,
new: func() interface{} { return &[]int{} },
new: func() any { return &[]int{} },
},
{
name: "BadValue",
@@ -204,7 +204,7 @@ func TestDecodeBadSlice(t *testing.T) {
}
]
}`,
new: func() interface{} { return &[2]int{} },
new: func() any { return &[2]int{} },
},
}

View File

@@ -15,7 +15,7 @@ import (
func TestEncodeStruct(t *testing.T) {
tests := []struct {
name string
in interface{}
in any
want string
}{
{
@@ -108,7 +108,7 @@ func TestDecodeStruct(t *testing.T) {
tests := []struct {
name string
in string
want interface{}
want any
}{
{
name: "Simple",

View File

@@ -16,7 +16,7 @@ func TestTimeEncoding(t *testing.T) {
const ms = 1481750076295
ts := time.Unix(0, ms*time.Millisecond.Nanoseconds())
for _, v := range []interface{}{ts, &ts} {
for _, v := range []any{ts, &ts} {
got, err := MarshalToString(v)
require.NoError(t, err)
assert.JSONEq(t, `{ "@type": "g:Timestamp", "@value": 1481750076295 }`, got)

View File

@@ -65,7 +65,7 @@ func TestEncodeTyper(t *testing.T) {
}
}`
for _, tc := range []interface{}{m, &m, v, vv, &vv} {
for _, tc := range []any{m, &m, v, vv, &vv} {
got, err := MarshalToString(tc)
assert.NoError(t, err)
assert.JSONEq(t, want, got)