Replace all interface{} to any

This commit is contained in:
Tnze
2023-04-05 19:35:20 +08:00
parent 195945e4ca
commit a42267ba31
21 changed files with 52 additions and 52 deletions

View File

@ -12,7 +12,7 @@ import (
// Unmarshal decode binary NBT data and fill into v
// This is a shortcut to `NewDecoder(bytes.NewReader(data)).Decode(v)`.
func Unmarshal(data []byte, v interface{}) error {
func Unmarshal(data []byte, v any) error {
_, err := NewDecoder(bytes.NewReader(data)).Decode(v)
return err
}
@ -28,7 +28,7 @@ func Unmarshal(data []byte, v interface{}) error {
//
// This method also return tag name of the root tag.
// In real world, it is often empty, but the API should allow you to get it when ever you want.
func (d *Decoder) Decode(v interface{}) (string, error) {
func (d *Decoder) Decode(v any) (string, error) {
val := reflect.ValueOf(v)
if val.Kind() != reflect.Ptr {
return "", errors.New("nbt: non-pointer passed to Decode")
@ -329,7 +329,7 @@ func (d *Decoder) unmarshal(val reflect.Value, tagType byte) error {
default:
return errors.New("cannot parse TagList as " + vk.String())
case reflect.Interface:
buf = reflect.ValueOf(make([]interface{}, listLen))
buf = reflect.ValueOf(make([]any, listLen))
case reflect.Slice:
buf = reflect.MakeSlice(val.Type(), int(listLen), int(listLen))
case reflect.Array:
@ -398,7 +398,7 @@ func (d *Decoder) unmarshal(val reflect.Value, tagType byte) error {
val.SetMapIndex(reflect.ValueOf(tn), v.Elem())
}
case reflect.Interface:
buf := make(map[string]interface{})
buf := make(map[string]any)
for {
tt, tn, err := d.readTag()
if err != nil {
@ -407,7 +407,7 @@ func (d *Decoder) unmarshal(val reflect.Value, tagType byte) error {
if tt == TagEnd {
break
}
var value interface{}
var value any
if err = d.unmarshal(reflect.ValueOf(&value).Elem(), tt); err != nil {
return fmt.Errorf("fail to decode tag %q: %w", tn, err)
}
@ -467,7 +467,7 @@ func indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnm
}
// Prevent infinite loop if v is an interface pointing to its own address:
// var v interface{}
// var v any
// v = &v
if v.Elem().Kind() == reflect.Interface && v.Elem().Elem() == v {
v = v.Elem()

View File

@ -27,8 +27,8 @@ func TestUnmarshal_string(t *testing.T) {
t.Errorf("Unmarshal NBT fail: get %q, want %q", Name, "Bananrama")
}
// Unmarshal to interface{}
var infName interface{}
// Unmarshal to any
var infName any
if err := Unmarshal(data, &infName); err != nil {
t.Fatal(err)
}
@ -210,7 +210,7 @@ func TestDecoder_Decode_bigTest(t *testing.T) {
t.Fatal(err)
}
var inf interface{}
var inf any
r, err = gzip.NewReader(bytes.NewReader(bigTestData[:]))
if err != nil {
t.Fatal(err)
@ -313,7 +313,7 @@ func TestDecoder_Decode_LongArray(t *testing.T) {
}
var (
value []int64
infValue interface{}
infValue any
want = []int64{1, 2, 3}
)
@ -342,7 +342,7 @@ func TestDecoder_Decode_ByteArray(t *testing.T) {
}
var (
value []byte
infValue interface{}
infValue any
want = []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}
)
@ -355,7 +355,7 @@ func TestDecoder_Decode_ByteArray(t *testing.T) {
}
// t.Log(value)
// Unmarshal to interface{}
// Unmarshal to any
if err := Unmarshal(data, &infValue); err != nil {
t.Fatal(err)
}

View File

@ -16,7 +16,7 @@ import (
// Marshal is the shortcut of NewEncoder().Encode() with empty tag name.
// Notices that repeatedly init buffers is low efficiency.
// Using Encoder and Reset the buffer in each time is recommended in that cases.
func Marshal(v interface{}) ([]byte, error) {
func Marshal(v any) ([]byte, error) {
var buf bytes.Buffer
err := NewEncoder(&buf).Encode(v, "")
return buf.Bytes(), err
@ -39,7 +39,7 @@ func NewEncoder(w io.Writer) *Encoder {
// expect `[]int8`, `[]int32`, `[]int64`, `[]uint8`, `[]uint32` and `[]uint64`,
// which TagByteArray, TagIntArray and TagLongArray.
// To force encode them as TagList, add a struct field tag.
func (e *Encoder) Encode(v interface{}, tagName string) error {
func (e *Encoder) Encode(v any, tagName string) error {
t, val := getTagType(reflect.ValueOf(v))
return e.marshal(val, t, tagName)
}
@ -242,7 +242,7 @@ func getTagType(v reflect.Value) (byte, reflect.Value) {
}
// Prevent infinite loop if v is an interface pointing to its own address:
// var v interface{}
// var v any
// v = &v
if v.Elem().Kind() == reflect.Interface && v.Elem().Elem() == v {
v = v.Elem()

View File

@ -112,12 +112,12 @@ func TestEncoder_Encode_interfaceArray(t *testing.T) {
tests := []struct {
name string
args []interface{}
args []any
want []byte
}{
{
name: "Two element interface array",
args: []interface{}{Struct1{3}, Struct2{0.3}},
args: []any{Struct1{3}, Struct2{0.3}},
want: []byte{
TagList, 0x00, 0x00 /*no name*/, TagCompound, 0, 0, 0, 2,
// 1st element
@ -136,7 +136,7 @@ func TestEncoder_Encode_interfaceArray(t *testing.T) {
if err != nil {
t.Error(err)
} else if !bytes.Equal(data, tt.want) {
t.Errorf("Marshal([]interface{}) got = % 02x, want % 02x", data, tt.want)
t.Errorf("Marshal([]any) got = % 02x, want % 02x", data, tt.want)
return
}
})
@ -242,7 +242,7 @@ func TestEncoder_Encode_map(t *testing.T) {
}
func TestEncoder_Encode_interface(t *testing.T) {
data := map[string]interface{}{
data := map[string]any{
"Key": int32(12),
"Value": "Tnze",
}

View File

@ -62,7 +62,7 @@ func (m RawMessage) String() string {
}
// Unmarshal decode the data into v.
func (m RawMessage) Unmarshal(v interface{}) error {
func (m RawMessage) Unmarshal(v any) error {
d := NewDecoder(bytes.NewReader(m.Data))
val := reflect.ValueOf(v)
if val.Kind() != reflect.Ptr {