From 493ec396fab32d9e8859e34ad497fdb0a910a33c Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 1 Jan 2023 17:49:11 -0700 Subject: lint: Turn on forcetypeassert --- lib/binstruct/size.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/binstruct') diff --git a/lib/binstruct/size.go b/lib/binstruct/size.go index 365da85..52fa380 100644 --- a/lib/binstruct/size.go +++ b/lib/binstruct/size.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -30,6 +30,7 @@ var ( func staticSize(typ reflect.Type) (int, error) { if typ.Implements(staticSizerType) { + //nolint:forcetypeassert // Already did a type check via reflection. return reflect.New(typ).Elem().Interface().(StaticSizer).BinaryStaticSize(), nil } if typ.Implements(marshalerType) || typ.Implements(unmarshalerType) { -- cgit v1.2.3-2-g168b From a06a7fb2d5bbf1ca5659de06fc9e975666bdcf9f Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 1 Jan 2023 19:33:03 -0700 Subject: lint: Turn on gofumpt All edits to .go files are made by `tools/bin/golangci-lint run --fix ./...`, not by me as a human. --- lib/binstruct/binint/builtins.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'lib/binstruct') diff --git a/lib/binstruct/binint/builtins.go b/lib/binstruct/binint/builtins.go index 01186bc..e4be2a6 100644 --- a/lib/binstruct/binint/builtins.go +++ b/lib/binstruct/binint/builtins.go @@ -34,6 +34,7 @@ func (x U16le) MarshalBinary() ([]byte, error) { binary.LittleEndian.PutUint16(buf[:], uint16(x)) return buf[:], nil } + func (x *U16le) UnmarshalBinary(dat []byte) (int, error) { if err := binutil.NeedNBytes(dat, 2); err != nil { return 0, err @@ -50,6 +51,7 @@ func (x U32le) MarshalBinary() ([]byte, error) { binary.LittleEndian.PutUint32(buf[:], uint32(x)) return buf[:], nil } + func (x *U32le) UnmarshalBinary(dat []byte) (int, error) { if err := binutil.NeedNBytes(dat, 4); err != nil { return 0, err @@ -66,6 +68,7 @@ func (x U64le) MarshalBinary() ([]byte, error) { binary.LittleEndian.PutUint64(buf[:], uint64(x)) return buf[:], nil } + func (x *U64le) UnmarshalBinary(dat []byte) (int, error) { if err := binutil.NeedNBytes(dat, 8); err != nil { return 0, err @@ -84,6 +87,7 @@ func (x U16be) MarshalBinary() ([]byte, error) { binary.BigEndian.PutUint16(buf[:], uint16(x)) return buf[:], nil } + func (x *U16be) UnmarshalBinary(dat []byte) (int, error) { if err := binutil.NeedNBytes(dat, 2); err != nil { return 0, err @@ -100,6 +104,7 @@ func (x U32be) MarshalBinary() ([]byte, error) { binary.BigEndian.PutUint32(buf[:], uint32(x)) return buf[:], nil } + func (x *U32be) UnmarshalBinary(dat []byte) (int, error) { if err := binutil.NeedNBytes(dat, 4); err != nil { return 0, err @@ -116,6 +121,7 @@ func (x U64be) MarshalBinary() ([]byte, error) { binary.BigEndian.PutUint64(buf[:], uint64(x)) return buf[:], nil } + func (x *U64be) UnmarshalBinary(dat []byte) (int, error) { if err := binutil.NeedNBytes(dat, 8); err != nil { return 0, err @@ -148,6 +154,7 @@ func (x I16le) MarshalBinary() ([]byte, error) { binary.LittleEndian.PutUint16(buf[:], uint16(x)) return buf[:], nil } + func (x *I16le) UnmarshalBinary(dat []byte) (int, error) { if err := binutil.NeedNBytes(dat, 2); err != nil { return 0, err @@ -164,6 +171,7 @@ func (x I32le) MarshalBinary() ([]byte, error) { binary.LittleEndian.PutUint32(buf[:], uint32(x)) return buf[:], nil } + func (x *I32le) UnmarshalBinary(dat []byte) (int, error) { if err := binutil.NeedNBytes(dat, 4); err != nil { return 0, err @@ -180,6 +188,7 @@ func (x I64le) MarshalBinary() ([]byte, error) { binary.LittleEndian.PutUint64(buf[:], uint64(x)) return buf[:], nil } + func (x *I64le) UnmarshalBinary(dat []byte) (int, error) { if err := binutil.NeedNBytes(dat, 8); err != nil { return 0, err @@ -198,6 +207,7 @@ func (x I16be) MarshalBinary() ([]byte, error) { binary.BigEndian.PutUint16(buf[:], uint16(x)) return buf[:], nil } + func (x *I16be) UnmarshalBinary(dat []byte) (int, error) { if err := binutil.NeedNBytes(dat, 2); err != nil { return 0, err @@ -214,6 +224,7 @@ func (x I32be) MarshalBinary() ([]byte, error) { binary.BigEndian.PutUint32(buf[:], uint32(x)) return buf[:], nil } + func (x *I32be) UnmarshalBinary(dat []byte) (int, error) { if err := binutil.NeedNBytes(dat, 4); err != nil { return 0, err @@ -230,6 +241,7 @@ func (x I64be) MarshalBinary() ([]byte, error) { binary.BigEndian.PutUint64(buf[:], uint64(x)) return buf[:], nil } + func (x *I64be) UnmarshalBinary(dat []byte) (int, error) { if err := binutil.NeedNBytes(dat, 8); err != nil { return 0, err -- cgit v1.2.3-2-g168b From 97fa22c161056c289a9978f20e3fb6b05d779a23 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 1 Jan 2023 20:48:22 -0700 Subject: lint: Turn on gomnd --- lib/binstruct/binint/builtins.go | 117 +++++++++++++++++++++------------------ lib/binstruct/size.go | 15 +++-- 2 files changed, 73 insertions(+), 59 deletions(-) (limited to 'lib/binstruct') diff --git a/lib/binstruct/binint/builtins.go b/lib/binstruct/binint/builtins.go index e4be2a6..cfd0fc2 100644 --- a/lib/binstruct/binint/builtins.go +++ b/lib/binstruct/binint/builtins.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -10,242 +10,249 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/binstruct/binutil" ) +const ( + sizeof8 = 1 + sizeof16 = 2 + sizeof32 = 4 + sizeof64 = 8 +) + // unsigned type U8 uint8 -func (U8) BinaryStaticSize() int { return 1 } +func (U8) BinaryStaticSize() int { return sizeof8 } func (x U8) MarshalBinary() ([]byte, error) { return []byte{byte(x)}, nil } func (x *U8) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 1); err != nil { + if err := binutil.NeedNBytes(dat, sizeof8); err != nil { return 0, err } *x = U8(dat[0]) - return 1, nil + return sizeof8, nil } // unsigned little endian type U16le uint16 -func (U16le) BinaryStaticSize() int { return 2 } +func (U16le) BinaryStaticSize() int { return sizeof16 } func (x U16le) MarshalBinary() ([]byte, error) { - var buf [2]byte + var buf [sizeof16]byte binary.LittleEndian.PutUint16(buf[:], uint16(x)) return buf[:], nil } func (x *U16le) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 2); err != nil { + if err := binutil.NeedNBytes(dat, sizeof16); err != nil { return 0, err } *x = U16le(binary.LittleEndian.Uint16(dat)) - return 2, nil + return sizeof16, nil } type U32le uint32 -func (U32le) BinaryStaticSize() int { return 4 } +func (U32le) BinaryStaticSize() int { return sizeof32 } func (x U32le) MarshalBinary() ([]byte, error) { - var buf [4]byte + var buf [sizeof32]byte binary.LittleEndian.PutUint32(buf[:], uint32(x)) return buf[:], nil } func (x *U32le) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 4); err != nil { + if err := binutil.NeedNBytes(dat, sizeof32); err != nil { return 0, err } *x = U32le(binary.LittleEndian.Uint32(dat)) - return 4, nil + return sizeof32, nil } type U64le uint64 -func (U64le) BinaryStaticSize() int { return 8 } +func (U64le) BinaryStaticSize() int { return sizeof64 } func (x U64le) MarshalBinary() ([]byte, error) { - var buf [8]byte + var buf [sizeof64]byte binary.LittleEndian.PutUint64(buf[:], uint64(x)) return buf[:], nil } func (x *U64le) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 8); err != nil { + if err := binutil.NeedNBytes(dat, sizeof64); err != nil { return 0, err } *x = U64le(binary.LittleEndian.Uint64(dat)) - return 8, nil + return sizeof64, nil } // unsigned big endian type U16be uint16 -func (U16be) BinaryStaticSize() int { return 2 } +func (U16be) BinaryStaticSize() int { return sizeof16 } func (x U16be) MarshalBinary() ([]byte, error) { - var buf [2]byte + var buf [sizeof16]byte binary.BigEndian.PutUint16(buf[:], uint16(x)) return buf[:], nil } func (x *U16be) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 2); err != nil { + if err := binutil.NeedNBytes(dat, sizeof16); err != nil { return 0, err } *x = U16be(binary.BigEndian.Uint16(dat)) - return 2, nil + return sizeof16, nil } type U32be uint32 -func (U32be) BinaryStaticSize() int { return 4 } +func (U32be) BinaryStaticSize() int { return sizeof32 } func (x U32be) MarshalBinary() ([]byte, error) { - var buf [4]byte + var buf [sizeof32]byte binary.BigEndian.PutUint32(buf[:], uint32(x)) return buf[:], nil } func (x *U32be) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 4); err != nil { + if err := binutil.NeedNBytes(dat, sizeof32); err != nil { return 0, err } *x = U32be(binary.BigEndian.Uint32(dat)) - return 4, nil + return sizeof32, nil } type U64be uint64 -func (U64be) BinaryStaticSize() int { return 8 } +func (U64be) BinaryStaticSize() int { return sizeof64 } func (x U64be) MarshalBinary() ([]byte, error) { - var buf [8]byte + var buf [sizeof64]byte binary.BigEndian.PutUint64(buf[:], uint64(x)) return buf[:], nil } func (x *U64be) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 8); err != nil { + if err := binutil.NeedNBytes(dat, sizeof64); err != nil { return 0, err } *x = U64be(binary.BigEndian.Uint64(dat)) - return 8, nil + return sizeof64, nil } // signed type I8 int8 -func (I8) BinaryStaticSize() int { return 1 } +func (I8) BinaryStaticSize() int { return sizeof8 } func (x I8) MarshalBinary() ([]byte, error) { return []byte{byte(x)}, nil } func (x *I8) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 1); err != nil { + if err := binutil.NeedNBytes(dat, sizeof8); err != nil { return 0, err } *x = I8(dat[0]) - return 1, nil + return sizeof8, nil } // signed little endian type I16le int16 -func (I16le) BinaryStaticSize() int { return 2 } +func (I16le) BinaryStaticSize() int { return sizeof16 } func (x I16le) MarshalBinary() ([]byte, error) { - var buf [2]byte + var buf [sizeof16]byte binary.LittleEndian.PutUint16(buf[:], uint16(x)) return buf[:], nil } func (x *I16le) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 2); err != nil { + if err := binutil.NeedNBytes(dat, sizeof16); err != nil { return 0, err } *x = I16le(binary.LittleEndian.Uint16(dat)) - return 2, nil + return sizeof16, nil } type I32le int32 -func (I32le) BinaryStaticSize() int { return 4 } +func (I32le) BinaryStaticSize() int { return sizeof32 } func (x I32le) MarshalBinary() ([]byte, error) { - var buf [4]byte + var buf [sizeof32]byte binary.LittleEndian.PutUint32(buf[:], uint32(x)) return buf[:], nil } func (x *I32le) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 4); err != nil { + if err := binutil.NeedNBytes(dat, sizeof32); err != nil { return 0, err } *x = I32le(binary.LittleEndian.Uint32(dat)) - return 4, nil + return sizeof32, nil } type I64le int64 -func (I64le) BinaryStaticSize() int { return 8 } +func (I64le) BinaryStaticSize() int { return sizeof64 } func (x I64le) MarshalBinary() ([]byte, error) { - var buf [8]byte + var buf [sizeof64]byte binary.LittleEndian.PutUint64(buf[:], uint64(x)) return buf[:], nil } func (x *I64le) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 8); err != nil { + if err := binutil.NeedNBytes(dat, sizeof64); err != nil { return 0, err } *x = I64le(binary.LittleEndian.Uint64(dat)) - return 8, nil + return sizeof64, nil } // signed big endian type I16be int16 -func (I16be) BinaryStaticSize() int { return 2 } +func (I16be) BinaryStaticSize() int { return sizeof16 } func (x I16be) MarshalBinary() ([]byte, error) { - var buf [2]byte + var buf [sizeof16]byte binary.BigEndian.PutUint16(buf[:], uint16(x)) return buf[:], nil } func (x *I16be) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 2); err != nil { + if err := binutil.NeedNBytes(dat, sizeof16); err != nil { return 0, err } *x = I16be(binary.BigEndian.Uint16(dat)) - return 2, nil + return sizeof16, nil } type I32be int32 -func (I32be) BinaryStaticSize() int { return 4 } +func (I32be) BinaryStaticSize() int { return sizeof32 } func (x I32be) MarshalBinary() ([]byte, error) { - var buf [4]byte + var buf [sizeof32]byte binary.BigEndian.PutUint32(buf[:], uint32(x)) return buf[:], nil } func (x *I32be) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 4); err != nil { + if err := binutil.NeedNBytes(dat, sizeof32); err != nil { return 0, err } *x = I32be(binary.BigEndian.Uint32(dat)) - return 4, nil + return sizeof32, nil } type I64be int64 -func (I64be) BinaryStaticSize() int { return 8 } +func (I64be) BinaryStaticSize() int { return sizeof64 } func (x I64be) MarshalBinary() ([]byte, error) { - var buf [8]byte + var buf [sizeof64]byte binary.BigEndian.PutUint64(buf[:], uint64(x)) return buf[:], nil } func (x *I64be) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 8); err != nil { + if err := binutil.NeedNBytes(dat, sizeof64); err != nil { return 0, err } *x = I64be(binary.BigEndian.Uint64(dat)) - return 8, nil + return sizeof64, nil } diff --git a/lib/binstruct/size.go b/lib/binstruct/size.go index 52fa380..d6d70c6 100644 --- a/lib/binstruct/size.go +++ b/lib/binstruct/size.go @@ -28,6 +28,13 @@ var ( unmarshalerType = reflect.TypeOf((*Unmarshaler)(nil)).Elem() ) +const ( + sizeof8 = 1 + sizeof16 = 2 + sizeof32 = 4 + sizeof64 = 8 +) + func staticSize(typ reflect.Type) (int, error) { if typ.Implements(staticSizerType) { //nolint:forcetypeassert // Already did a type check via reflection. @@ -43,13 +50,13 @@ func staticSize(typ reflect.Type) (int, error) { } switch typ.Kind() { case reflect.Uint8, reflect.Int8: - return 1, nil + return sizeof8, nil case reflect.Uint16, reflect.Int16: - return 2, nil + return sizeof16, nil case reflect.Uint32, reflect.Int32: - return 4, nil + return sizeof32, nil case reflect.Uint64, reflect.Int64: - return 8, nil + return sizeof64, nil case reflect.Ptr: return staticSize(typ.Elem()) case reflect.Array: -- cgit v1.2.3-2-g168b From 5edd0d7be38595c4777d5130ca20f907d8a74963 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 1 Jan 2023 21:12:41 -0700 Subject: lint: Turn on paralleltest --- lib/binstruct/binstruct_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/binstruct') diff --git a/lib/binstruct/binstruct_test.go b/lib/binstruct/binstruct_test.go index 105e790..8780acc 100644 --- a/lib/binstruct/binstruct_test.go +++ b/lib/binstruct/binstruct_test.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -13,6 +13,7 @@ import ( ) func TestSmoke(t *testing.T) { + t.Parallel() type UUID [16]byte type PhysicalAddr int64 type DevItem struct { -- cgit v1.2.3-2-g168b From 6912dad34102be1ddea5f3b7710abd0cc8922d20 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 1 Jan 2023 22:13:14 -0700 Subject: lint: Turn on stylecheck --- lib/binstruct/structs.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/binstruct') diff --git a/lib/binstruct/structs.go b/lib/binstruct/structs.go index 9bc556c..7eea600 100644 --- a/lib/binstruct/structs.go +++ b/lib/binstruct/structs.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -120,7 +120,7 @@ func genStructHandler(structInfo reflect.Type) (structHandler, error) { var curOffset, endOffset int for i := 0; i < structInfo.NumField(); i++ { - var fieldInfo reflect.StructField = structInfo.Field(i) + fieldInfo := structInfo.Field(i) if fieldInfo.Anonymous && fieldInfo.Type != endType { err := fmt.Errorf("binstruct does not support embedded fields") -- cgit v1.2.3-2-g168b