Refactor code and lint

This commit is contained in:
Faye Amacker 2024-05-27 15:33:34 -05:00
parent 842ac3ea85
commit 03575b4950
7 changed files with 43 additions and 27 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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)
}
}

View File

@ -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()
}

View File

@ -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)))
}

View File

@ -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)
}
})

View File

@ -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)
}