Skip to content

Commit a42267b

Browse files
committed
Replace all interface{} to any
1 parent 195945e commit a42267b

File tree

21 files changed

+52
-52
lines changed

21 files changed

+52
-52
lines changed

chat/message.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func (m Message) ClearString() string {
246246

247247
// handle translate
248248
if m.Translate != "" {
249-
args := make([]interface{}, len(m.With))
249+
args := make([]any, len(m.With))
250250
for i, v := range m.With {
251251
args[i] = v.ClearString()
252252
}
@@ -291,7 +291,7 @@ func (m Message) String() string {
291291

292292
// handle translate
293293
if m.Translate != "" {
294-
args := make([]interface{}, len(m.With))
294+
args := make([]any, len(m.With))
295295
for i, v := range m.With {
296296
args[i] = v
297297
}

nbt/decode.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
// Unmarshal decode binary NBT data and fill into v
1414
// This is a shortcut to `NewDecoder(bytes.NewReader(data)).Decode(v)`.
15-
func Unmarshal(data []byte, v interface{}) error {
15+
func Unmarshal(data []byte, v any) error {
1616
_, err := NewDecoder(bytes.NewReader(data)).Decode(v)
1717
return err
1818
}
@@ -28,7 +28,7 @@ func Unmarshal(data []byte, v interface{}) error {
2828
//
2929
// This method also return tag name of the root tag.
3030
// In real world, it is often empty, but the API should allow you to get it when ever you want.
31-
func (d *Decoder) Decode(v interface{}) (string, error) {
31+
func (d *Decoder) Decode(v any) (string, error) {
3232
val := reflect.ValueOf(v)
3333
if val.Kind() != reflect.Ptr {
3434
return "", errors.New("nbt: non-pointer passed to Decode")
@@ -329,7 +329,7 @@ func (d *Decoder) unmarshal(val reflect.Value, tagType byte) error {
329329
default:
330330
return errors.New("cannot parse TagList as " + vk.String())
331331
case reflect.Interface:
332-
buf = reflect.ValueOf(make([]interface{}, listLen))
332+
buf = reflect.ValueOf(make([]any, listLen))
333333
case reflect.Slice:
334334
buf = reflect.MakeSlice(val.Type(), int(listLen), int(listLen))
335335
case reflect.Array:
@@ -398,7 +398,7 @@ func (d *Decoder) unmarshal(val reflect.Value, tagType byte) error {
398398
val.SetMapIndex(reflect.ValueOf(tn), v.Elem())
399399
}
400400
case reflect.Interface:
401-
buf := make(map[string]interface{})
401+
buf := make(map[string]any)
402402
for {
403403
tt, tn, err := d.readTag()
404404
if err != nil {
@@ -407,7 +407,7 @@ func (d *Decoder) unmarshal(val reflect.Value, tagType byte) error {
407407
if tt == TagEnd {
408408
break
409409
}
410-
var value interface{}
410+
var value any
411411
if err = d.unmarshal(reflect.ValueOf(&value).Elem(), tt); err != nil {
412412
return fmt.Errorf("fail to decode tag %q: %w", tn, err)
413413
}
@@ -467,7 +467,7 @@ func indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnm
467467
}
468468

469469
// Prevent infinite loop if v is an interface pointing to its own address:
470-
// var v interface{}
470+
// var v any
471471
// v = &v
472472
if v.Elem().Kind() == reflect.Interface && v.Elem().Elem() == v {
473473
v = v.Elem()

nbt/decode_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ func TestUnmarshal_string(t *testing.T) {
2727
t.Errorf("Unmarshal NBT fail: get %q, want %q", Name, "Bananrama")
2828
}
2929

30-
// Unmarshal to interface{}
31-
var infName interface{}
30+
// Unmarshal to any
31+
var infName any
3232
if err := Unmarshal(data, &infName); err != nil {
3333
t.Fatal(err)
3434
}
@@ -210,7 +210,7 @@ func TestDecoder_Decode_bigTest(t *testing.T) {
210210
t.Fatal(err)
211211
}
212212

213-
var inf interface{}
213+
var inf any
214214
r, err = gzip.NewReader(bytes.NewReader(bigTestData[:]))
215215
if err != nil {
216216
t.Fatal(err)
@@ -313,7 +313,7 @@ func TestDecoder_Decode_LongArray(t *testing.T) {
313313
}
314314
var (
315315
value []int64
316-
infValue interface{}
316+
infValue any
317317
want = []int64{1, 2, 3}
318318
)
319319

@@ -342,7 +342,7 @@ func TestDecoder_Decode_ByteArray(t *testing.T) {
342342
}
343343
var (
344344
value []byte
345-
infValue interface{}
345+
infValue any
346346
want = []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}
347347
)
348348

@@ -355,7 +355,7 @@ func TestDecoder_Decode_ByteArray(t *testing.T) {
355355
}
356356
// t.Log(value)
357357

358-
// Unmarshal to interface{}
358+
// Unmarshal to any
359359
if err := Unmarshal(data, &infValue); err != nil {
360360
t.Fatal(err)
361361
}

nbt/encode.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
// Marshal is the shortcut of NewEncoder().Encode() with empty tag name.
1717
// Notices that repeatedly init buffers is low efficiency.
1818
// Using Encoder and Reset the buffer in each time is recommended in that cases.
19-
func Marshal(v interface{}) ([]byte, error) {
19+
func Marshal(v any) ([]byte, error) {
2020
var buf bytes.Buffer
2121
err := NewEncoder(&buf).Encode(v, "")
2222
return buf.Bytes(), err
@@ -39,7 +39,7 @@ func NewEncoder(w io.Writer) *Encoder {
3939
// expect `[]int8`, `[]int32`, `[]int64`, `[]uint8`, `[]uint32` and `[]uint64`,
4040
// which TagByteArray, TagIntArray and TagLongArray.
4141
// To force encode them as TagList, add a struct field tag.
42-
func (e *Encoder) Encode(v interface{}, tagName string) error {
42+
func (e *Encoder) Encode(v any, tagName string) error {
4343
t, val := getTagType(reflect.ValueOf(v))
4444
return e.marshal(val, t, tagName)
4545
}
@@ -242,7 +242,7 @@ func getTagType(v reflect.Value) (byte, reflect.Value) {
242242
}
243243

244244
// Prevent infinite loop if v is an interface pointing to its own address:
245-
// var v interface{}
245+
// var v any
246246
// v = &v
247247
if v.Elem().Kind() == reflect.Interface && v.Elem().Elem() == v {
248248
v = v.Elem()

nbt/encode_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ func TestEncoder_Encode_interfaceArray(t *testing.T) {
112112

113113
tests := []struct {
114114
name string
115-
args []interface{}
115+
args []any
116116
want []byte
117117
}{
118118
{
119119
name: "Two element interface array",
120-
args: []interface{}{Struct1{3}, Struct2{0.3}},
120+
args: []any{Struct1{3}, Struct2{0.3}},
121121
want: []byte{
122122
TagList, 0x00, 0x00 /*no name*/, TagCompound, 0, 0, 0, 2,
123123
// 1st element
@@ -136,7 +136,7 @@ func TestEncoder_Encode_interfaceArray(t *testing.T) {
136136
if err != nil {
137137
t.Error(err)
138138
} else if !bytes.Equal(data, tt.want) {
139-
t.Errorf("Marshal([]interface{}) got = % 02x, want % 02x", data, tt.want)
139+
t.Errorf("Marshal([]any) got = % 02x, want % 02x", data, tt.want)
140140
return
141141
}
142142
})
@@ -242,7 +242,7 @@ func TestEncoder_Encode_map(t *testing.T) {
242242
}
243243

244244
func TestEncoder_Encode_interface(t *testing.T) {
245-
data := map[string]interface{}{
245+
data := map[string]any{
246246
"Key": int32(12),
247247
"Value": "Tnze",
248248
}

nbt/rawmsg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (m RawMessage) String() string {
6262
}
6363

6464
// Unmarshal decode the data into v.
65-
func (m RawMessage) Unmarshal(v interface{}) error {
65+
func (m RawMessage) Unmarshal(v any) error {
6666
d := NewDecoder(bytes.NewReader(m.Data))
6767
val := reflect.ValueOf(v)
6868
if val.Kind() != reflect.Ptr {

net/packet/packet.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (p Packet) Scan(fields ...FieldDecoder) error {
3838
}
3939

4040
var bufPool = sync.Pool{
41-
New: func() interface{} {
41+
New: func() any {
4242
return new(bytes.Buffer)
4343
},
4444
}

net/packet/packet_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ func ExamplePacket_Scan_joinGame() {
146146
PreGamemode pk.Byte
147147
WorldNames = []pk.Identifier{} // This cannot replace with "var DimensionNames []pk.Identifier" because "nil" has no type information
148148
DimensionCodec struct {
149-
DimensionType interface{} `nbt:"minecraft:dimension_type"`
150-
WorldgenBiome interface{} `nbt:"minecraft:worldgen/biome"`
149+
DimensionType any `nbt:"minecraft:dimension_type"`
150+
WorldgenBiome any `nbt:"minecraft:worldgen/biome"`
151151
}
152-
Dimension interface{}
152+
Dimension any
153153
WorldName pk.Identifier
154154
HashedSeed pk.Long
155155
MaxPlayers pk.VarInt
@@ -181,7 +181,7 @@ func ExampleMarshal_setSlot() {
181181
Present bool
182182
ItemID int
183183
ItemCount byte
184-
NBT interface{}
184+
NBT any
185185
}{
186186
{WindowID: 0, Slot: 5, Present: false},
187187
{WindowID: 0, Slot: 5, Present: true, ItemID: 0x01, ItemCount: 1, NBT: pk.Byte(0)},

net/packet/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ func (d *Double) ReadFrom(r io.Reader) (n int64, err error) {
467467
}
468468

469469
// NBT encode a value as Named Binary Tag
470-
func NBT(v interface{}, optionalTagName ...string) Field {
470+
func NBT(v any, optionalTagName ...string) Field {
471471
if len(optionalTagName) > 0 {
472472
return nbtField{V: v, FieldName: optionalTagName[0]}
473473
}

net/packet/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var (
2626
//
2727
// Note that Ary DO read or write the Len. You aren't need to do so by your self.
2828
type Ary[LEN VarInt | VarLong | Byte | UnsignedByte | Short | UnsignedShort | Int | Long] struct {
29-
Ary interface{} // Slice or Pointer of Slice of FieldEncoder, FieldDecoder or both (Field)
29+
Ary any // Slice or Pointer of Slice of FieldEncoder, FieldDecoder or both (Field)
3030
}
3131

3232
func (a Ary[LEN]) WriteTo(w io.Writer) (n int64, err error) {

0 commit comments

Comments
 (0)