From 03575b4950291d5f3cef2503529cbc69013640d4 Mon Sep 17 00:00:00 2001 From: Faye Amacker <33205765+fxamacker@users.noreply.github.com> Date: Mon, 27 May 2024 15:33:34 -0500 Subject: [PATCH] Refactor code and lint --- bench_test.go | 2 +- decode.go | 21 ++++++++++++++------- decode_test.go | 9 +++++---- diagnose.go | 12 ++++++------ encode.go | 8 ++++---- encode_test.go | 6 +++--- structfields.go | 12 ++++++++++-- 7 files changed, 43 insertions(+), 27 deletions(-) diff --git a/bench_test.go b/bench_test.go index b88249b..5e64a83 100644 --- a/bench_test.go +++ b/bench_test.go @@ -864,7 +864,7 @@ func BenchmarkUnmarshalMapToStruct(b *testing.B) { reject bool } - for _, tc := range []struct { + for _, tc := range []*struct { name string opts DecOptions inputs []input diff --git a/decode.go b/decode.go index 66599e1..96003cb 100644 --- a/decode.go +++ b/decode.go @@ -802,13 +802,13 @@ type DecOptions struct { } // DecMode returns DecMode with immutable options and no tags (safe for concurrency). -func (opts DecOptions) DecMode() (DecMode, error) { +func (opts DecOptions) DecMode() (DecMode, error) { //nolint:gocritic // ignore hugeParam return opts.decMode() } // validForTags checks that the provided tag set is compatible with these options and returns a // non-nil error if and only if the provided tag set is incompatible. -func (opts DecOptions) validForTags(tags TagSet) error { +func (opts DecOptions) validForTags(tags TagSet) error { //nolint:gocritic // ignore hugeParam if opts.TagsMd == TagsForbidden { return errors.New("cbor: cannot create DecMode with TagSet when TagsMd is TagsForbidden") } @@ -831,7 +831,7 @@ func (opts DecOptions) validForTags(tags TagSet) error { } // DecModeWithTags returns DecMode with options and tags that are both immutable (safe for concurrency). -func (opts DecOptions) DecModeWithTags(tags TagSet) (DecMode, error) { +func (opts DecOptions) DecModeWithTags(tags TagSet) (DecMode, error) { //nolint:gocritic // ignore hugeParam if err := opts.validForTags(tags); err != nil { return nil, err } @@ -859,7 +859,7 @@ func (opts DecOptions) DecModeWithTags(tags TagSet) (DecMode, error) { } // DecModeWithSharedTags returns DecMode with immutable options and mutable shared tags (safe for concurrency). -func (opts DecOptions) DecModeWithSharedTags(tags TagSet) (DecMode, error) { +func (opts DecOptions) DecModeWithSharedTags(tags TagSet) (DecMode, error) { //nolint:gocritic // ignore hugeParam if err := opts.validForTags(tags); err != nil { return nil, err } @@ -894,7 +894,7 @@ var defaultSimpleValues = func() *SimpleValueRegistry { }() //nolint:gocyclo // Each option comes with some manageable boilerplate -func (opts DecOptions) decMode() (*decMode, error) { +func (opts DecOptions) decMode() (*decMode, error) { //nolint:gocritic // ignore hugeParam if !opts.DupMapKey.valid() { return nil, errors.New("cbor: invalid DupMapKey " + strconv.Itoa(int(opts.DupMapKey))) } @@ -1334,7 +1334,7 @@ func (d *decoder) parseToValue(v reflect.Value, tInfo *typeInfo) error { //nolin // Use value type v = v.Elem() tInfo = getTypeInfo(v.Type()) - } else { + } else { //nolint:gocritic // Create and use registered type if CBOR data is registered tag if d.dm.tags != nil && d.nextCBORType() == cborTypeTag { @@ -2036,7 +2036,14 @@ func (d *decoder) parseByteString() ([]byte, bool) { // encoding. If no transformation was performed (because it was not required), the original byte // slice is returned and the bool return value is false. Otherwise, a new slice containing the // converted bytes is returned along with the bool value true. -func (d *decoder) applyByteStringTextConversion(src []byte, dstType reflect.Type) ([]byte, bool, error) { +func (d *decoder) applyByteStringTextConversion( + src []byte, + dstType reflect.Type, +) ( + dst []byte, + transformed bool, + err error, +) { switch dstType.Kind() { case reflect.String: if d.dm.byteStringToString != ByteStringToStringAllowedWithExpectedLaterEncoding || len(d.expectedLaterEncodingTags) == 0 { diff --git a/decode_test.go b/decode_test.go index 440f53e..6eebb94 100644 --- a/decode_test.go +++ b/decode_test.go @@ -8255,7 +8255,7 @@ func TestDecodeBignumToEmptyInterface(t *testing.T) { err := decMode.Unmarshal(tc.data, &v) if err != nil { t.Errorf("Unmarshal(0x%x) to empty interface returned error %v", tc.data, err) - } else { + } else { //nolint:gocritic // ignore elseif if !reflect.DeepEqual(v, tc.wantValue) { t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", tc.data, v, v, tc.wantValue, tc.wantValue) } @@ -8706,10 +8706,11 @@ func TestUnmarshalSimpleValues(t *testing.T) { assertExactError := func(want error) func(*testing.T, error) { return func(t *testing.T, got error) { - if reflect.DeepEqual(want, got) { - return + targetErr := reflect.New(reflect.TypeOf(want)).Interface() + if !errors.As(got, &targetErr) || + got.Error() != want.Error() { + t.Errorf("want %#v, got %#v", want, got) } - t.Errorf("want %#v, got %#v", want, got) } } diff --git a/diagnose.go b/diagnose.go index ef406e0..a3a8cbd 100644 --- a/diagnose.go +++ b/diagnose.go @@ -159,7 +159,7 @@ func (dm *diagMode) Diagnose(data []byte) (string, error) { } // DiagnoseFirst returns extended diagnostic notation (EDN) of the first CBOR data item using the DiagMode. Any remaining bytes are returned in rest. -func (dm *diagMode) DiagnoseFirst(data []byte) (string, []byte, error) { +func (dm *diagMode) DiagnoseFirst(data []byte) (diagNotation string, rest []byte, err error) { return newDiagnose(data, dm.decMode, dm).diagFirst() } @@ -174,7 +174,7 @@ func Diagnose(data []byte) (string, error) { } // Diagnose returns extended diagnostic notation (EDN) of the first CBOR data item using the DiagMode. Any remaining bytes are returned in rest. -func DiagnoseFirst(data []byte) (string, []byte, error) { +func DiagnoseFirst(data []byte) (diagNotation string, rest []byte, err error) { return defaultDiagMode.DiagnoseFirst(data) } @@ -202,8 +202,8 @@ func (di *diagnose) diag(cborSequence bool) (string, error) { di.w.WriteString(", ") } firstItem = false - if err = di.item(); err != nil { - return di.w.String(), err + if itemErr := di.item(); itemErr != nil { + return di.w.String(), itemErr } case io.EOF: @@ -218,8 +218,8 @@ func (di *diagnose) diag(cborSequence bool) (string, error) { } } -func (di *diagnose) diagFirst() (string, []byte, error) { - err := di.wellformed(true) +func (di *diagnose) diagFirst() (diagNotation string, rest []byte, err error) { + err = di.wellformed(true) if err == nil { err = di.item() } diff --git a/encode.go b/encode.go index cd9430c..5440300 100644 --- a/encode.go +++ b/encode.go @@ -609,12 +609,12 @@ func PreferredUnsortedEncOptions() EncOptions { } // EncMode returns EncMode with immutable options and no tags (safe for concurrency). -func (opts EncOptions) EncMode() (EncMode, error) { +func (opts EncOptions) EncMode() (EncMode, error) { //nolint:gocritic // ignore hugeParam return opts.encMode() } // EncModeWithTags returns EncMode with options and tags that are both immutable (safe for concurrency). -func (opts EncOptions) EncModeWithTags(tags TagSet) (EncMode, error) { +func (opts EncOptions) EncModeWithTags(tags TagSet) (EncMode, error) { //nolint:gocritic // ignore hugeParam if opts.TagsMd == TagsForbidden { return nil, errors.New("cbor: cannot create EncMode with TagSet when TagsMd is TagsForbidden") } @@ -642,7 +642,7 @@ func (opts EncOptions) EncModeWithTags(tags TagSet) (EncMode, error) { } // EncModeWithSharedTags returns EncMode with immutable options and mutable shared tags (safe for concurrency). -func (opts EncOptions) EncModeWithSharedTags(tags TagSet) (EncMode, error) { +func (opts EncOptions) EncModeWithSharedTags(tags TagSet) (EncMode, error) { //nolint:gocritic // ignore hugeParam if opts.TagsMd == TagsForbidden { return nil, errors.New("cbor: cannot create EncMode with TagSet when TagsMd is TagsForbidden") } @@ -657,7 +657,7 @@ func (opts EncOptions) EncModeWithSharedTags(tags TagSet) (EncMode, error) { return em, nil } -func (opts EncOptions) encMode() (*encMode, error) { +func (opts EncOptions) encMode() (*encMode, error) { //nolint:gocritic // ignore hugeParam if !opts.Sort.valid() { return nil, errors.New("cbor: invalid SortMode " + strconv.Itoa(int(opts.Sort))) } diff --git a/encode_test.go b/encode_test.go index 9ca2a43..463bef8 100644 --- a/encode_test.go +++ b/encode_test.go @@ -4467,7 +4467,7 @@ func TestSortModeFastShuffle(t *testing.T) { if err != nil { t.Fatal(err) } - if string(first) != string(next) { + if !bytes.Equal(first, next) { return } } @@ -4570,7 +4570,7 @@ func TestMarshalByteArrayMode(t *testing.T) { t.Fatal(err) } - if string(out) != string(tc.expected) { + if !bytes.Equal(out, tc.expected) { t.Errorf("unexpected output, got 0x%x want 0x%x", out, tc.expected) } }) @@ -4669,7 +4669,7 @@ func TestMarshalByteSliceMode(t *testing.T) { t.Fatal(err) } - if string(out) != string(tc.expected) { + if !bytes.Equal(out, tc.expected) { t.Errorf("unexpected output, got 0x%x want 0x%x", out, tc.expected) } }) diff --git a/structfields.go b/structfields.go index 23a12be..d2a2c1d 100644 --- a/structfields.go +++ b/structfields.go @@ -144,7 +144,15 @@ func getFields(t reflect.Type) (flds fields, structOptions string) { } // appendFields appends type t's exportable fields to flds and anonymous struct fields to nTypes . -func appendFields(t reflect.Type, idx []int, flds fields, nTypes map[reflect.Type][][]int) (fields, map[reflect.Type][][]int) { +func appendFields( + t reflect.Type, + idx []int, + flds fields, + nTypes map[reflect.Type][][]int, +) ( + _flds fields, + _nTypes map[reflect.Type][][]int, +) { for i := 0; i < t.NumField(); i++ { f := t.Field(i) @@ -221,7 +229,7 @@ func appendFields(t reflect.Type, idx []int, flds fields, nTypes map[reflect.Typ // isFieldExportable returns true if f is an exportable (regular or anonymous) field or // a nonexportable anonymous field of struct type. // Nonexportable anonymous field of struct type can contain exportable fields. -func isFieldExportable(f reflect.StructField, fk reflect.Kind) bool { +func isFieldExportable(f reflect.StructField, fk reflect.Kind) bool { //nolint:gocritic // ignore hugeParam exportable := f.PkgPath == "" return exportable || (f.Anonymous && fk == reflect.Struct) }