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

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

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 {

View File

@ -38,7 +38,7 @@ func (p Packet) Scan(fields ...FieldDecoder) error {
}
var bufPool = sync.Pool{
New: func() interface{} {
New: func() any {
return new(bytes.Buffer)
},
}

View File

@ -146,10 +146,10 @@ func ExamplePacket_Scan_joinGame() {
PreGamemode pk.Byte
WorldNames = []pk.Identifier{} // This cannot replace with "var DimensionNames []pk.Identifier" because "nil" has no type information
DimensionCodec struct {
DimensionType interface{} `nbt:"minecraft:dimension_type"`
WorldgenBiome interface{} `nbt:"minecraft:worldgen/biome"`
DimensionType any `nbt:"minecraft:dimension_type"`
WorldgenBiome any `nbt:"minecraft:worldgen/biome"`
}
Dimension interface{}
Dimension any
WorldName pk.Identifier
HashedSeed pk.Long
MaxPlayers pk.VarInt
@ -181,7 +181,7 @@ func ExampleMarshal_setSlot() {
Present bool
ItemID int
ItemCount byte
NBT interface{}
NBT any
}{
{WindowID: 0, Slot: 5, Present: false},
{WindowID: 0, Slot: 5, Present: true, ItemID: 0x01, ItemCount: 1, NBT: pk.Byte(0)},

View File

@ -467,7 +467,7 @@ func (d *Double) ReadFrom(r io.Reader) (n int64, err error) {
}
// NBT encode a value as Named Binary Tag
func NBT(v interface{}, optionalTagName ...string) Field {
func NBT(v any, optionalTagName ...string) Field {
if len(optionalTagName) > 0 {
return nbtField{V: v, FieldName: optionalTagName[0]}
}

View File

@ -26,7 +26,7 @@ var (
//
// Note that Ary DO read or write the Len. You aren't need to do so by your self.
type Ary[LEN VarInt | VarLong | Byte | UnsignedByte | Short | UnsignedShort | Int | Long] struct {
Ary interface{} // Slice or Pointer of Slice of FieldEncoder, FieldDecoder or both (Field)
Ary any // Slice or Pointer of Slice of FieldEncoder, FieldDecoder or both (Field)
}
func (a Ary[LEN]) WriteTo(w io.Writer) (n int64, err error) {

View File

@ -90,7 +90,7 @@ func (r *RCONConn) ReadPacket() (RequestID, Type int32, Payload string, err erro
func (r *RCONConn) WritePacket(RequestID, Type int32, Payload string) error {
buf := new(bytes.Buffer)
for _, v := range []interface{}{
for _, v := range []any{
int32(4 + 4 + len(Payload) + 2), // Length
RequestID, // Request ID
Type, // Type

View File

@ -52,7 +52,7 @@ func New(version, user, astk, uuid string) *Realms {
return r
}
func (r *Realms) get(endpoint string, resp interface{}) error {
func (r *Realms) get(endpoint string, resp any) error {
rawResp, err := r.c.Get(Domain + endpoint)
if err != nil {
return err
@ -67,7 +67,7 @@ func (r *Realms) get(endpoint string, resp interface{}) error {
return nil
}
func (r *Realms) post(endpoint string, payload, resp interface{}) error {
func (r *Realms) post(endpoint string, payload, resp any) error {
data, err := json.Marshal(payload)
if err != nil {
return err

View File

@ -23,7 +23,7 @@ type Server struct {
MiniGameID *int
MinigameImage *string
ActiveSlot int
// Slots interface{}
// Slots any
Member bool
}

View File

@ -38,8 +38,8 @@ type WorldGenSettings struct {
}
type DimensionGenerator struct {
Type string `nbt:"type"`
Generator map[string]interface{} `nbt:"generator"`
Type string `nbt:"type"`
Generator map[string]any `nbt:"generator"`
}
var (
@ -47,7 +47,7 @@ var (
DefaultDimensionsGenerators = map[string]DimensionGenerator{
"minecraft:overworld": {
Type: "minecraft:overworld",
Generator: map[string]interface{}{
Generator: map[string]any{
"type": "minecraft:noise",
"settings": "minecraft:overworld",
"biome_source": map[string]string{
@ -58,7 +58,7 @@ var (
},
"minecraft:the_end": {
Type: "minecraft:the_end",
Generator: map[string]interface{}{
Generator: map[string]any{
"type": "minecraft:noise",
"settings": "minecraft:end",
"biome_source": map[string]string{
@ -68,7 +68,7 @@ var (
},
"minecraft:the_nether": {
Type: "minecraft:the_nether",
Generator: map[string]interface{}{
Generator: map[string]any{
"type": "minecraft:noise",
"settings": "minecraft:nether",
"biome_source": map[string]string{

View File

@ -46,7 +46,7 @@ type LevelData struct {
LastPlayed int64
LevelName string
MapFeatures bool
Player map[string]interface{}
Player map[string]any
Raining bool `nbt:"raining"`
RainTime int32 `nbt:"rainTime"`
RandomSeed int64

View File

@ -72,8 +72,8 @@ type PlayerData struct {
type Item struct {
Count byte
Slot byte
ID string `nbt:"id"`
Tag map[string]interface{} `nbt:"tag"`
ID string `nbt:"id"`
Tag map[string]any `nbt:"tag"`
}
func ReadPlayerData(r io.Reader) (data PlayerData, err error) {

View File

@ -43,7 +43,7 @@ func TestReadRegion(t *testing.T) {
if err != nil {
t.Error(err)
}
var b interface{}
var b any
_, err = nbt.NewDecoder(r).Decode(&b)
if err != nil {
t.Error(err)

View File

@ -54,7 +54,7 @@ func (g *Graph) Execute(ctx context.Context, cmd string) error {
}
}
type ParsedData interface{}
type ParsedData any
type HandlerFunc func(ctx context.Context, args []ParsedData) error

View File

@ -121,7 +121,7 @@ func (t *Tree[I, B, V]) Insert(leaf B, value V) (n *Node[I, B, V]) {
return
}
func (t *Tree[I, B, V]) Delete(n *Node[I, B, V]) interface{} {
func (t *Tree[I, B, V]) Delete(n *Node[I, B, V]) any {
if n.parent == nil {
// n is the root
t.root = nil
@ -204,11 +204,11 @@ type (
}
)
func (h searchHeap[I, V]) Len() int { return len(h) }
func (h searchHeap[I, V]) Less(i, j int) bool { return h[i].inheritedCost < h[j].inheritedCost }
func (h searchHeap[I, V]) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *searchHeap[I, V]) Push(x interface{}) { *h = append(*h, x.(searchItem[I, V])) }
func (h *searchHeap[I, V]) Pop() interface{} {
func (h searchHeap[I, V]) Len() int { return len(h) }
func (h searchHeap[I, V]) Less(i, j int) bool { return h[i].inheritedCost < h[j].inheritedCost }
func (h searchHeap[I, V]) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *searchHeap[I, V]) Push(x any) { *h = append(*h, x.(searchItem[I, V])) }
func (h *searchHeap[I, V]) Pop() any {
old := *h
n := len(old)
x := old[n-1]

View File

@ -54,7 +54,7 @@ func fetchKeyPair(accessToken string) (KeyPairResp, error) {
return keyPairResp, err
}
func post(endpoint string, accessToken string, resp interface{}) error {
func post(endpoint string, accessToken string, resp any) error {
rowResp, err := rawPost(endpoint, accessToken)
if err != nil {
return fmt.Errorf("request fail: %v", err)

View File

@ -28,7 +28,7 @@ var AuthURL = "https://authserver.mojang.com"
var client = http.DefaultClient
func post(endpoint string, payload interface{}, resp interface{}) error {
func post(endpoint string, payload any, resp any) error {
rowResp, err := rawPost(endpoint, payload)
if err != nil {
return fmt.Errorf("request fail: %v", err)
@ -43,7 +43,7 @@ func post(endpoint string, payload interface{}, resp interface{}) error {
return nil
}
func rawPost(endpoint string, payload interface{}) (*http.Response, error) {
func rawPost(endpoint string, payload any) (*http.Response, error) {
data, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("marshal payload fail: %v", err)