From a455df5ecfff886c2a4869e5238dd8262dbf2393 Mon Sep 17 00:00:00 2001 From: zensh Date: Fri, 27 Jan 2023 00:04:51 +0800 Subject: [PATCH] Fix for linters. Signed-off-by: Yan Qing --- diagnose.go | 46 +++++++++++++++------------------------------- diagnose_test.go | 22 +++++++++++----------- 2 files changed, 26 insertions(+), 42 deletions(-) diff --git a/diagnose.go b/diagnose.go index 0fbc3001..aaea4848 100644 --- a/diagnose.go +++ b/diagnose.go @@ -96,10 +96,10 @@ func (di *diagnose) diag() ([]byte, error) { for { switch err := di.valid(); err { case nil: - if err := di.writeString(", "); err != nil { + if err = di.writeString(", "); err != nil { return di.w.Bytes(), err } - if err := di.value(); err != nil { + if err = di.value(); err != nil { return di.w.Bytes(), err } @@ -119,10 +119,10 @@ func (di *diagnose) valid() error { return err } -func (di *diagnose) value() error { +func (di *diagnose) value() error { //nolint:gocyclo initialByte := di.d.data[di.d.off] switch initialByte { - case 0x5f, 0x7f: // indefinite byte string or UTF-8 string + case 0x5f, 0x7f: // indefinite byte string or UTF-8 text di.d.off++ if err := di.writeString("(_ "); err != nil { return err @@ -213,13 +213,12 @@ func (di *diagnose) value() error { bi.SetUint64(val) bi.Add(bi, big.NewInt(1)) bi.Neg(bi) - return di.writeString(bi.String()) - } else { - nValue := int64(-1) ^ int64(val) - return di.writeString(strconv.FormatInt(nValue, 10)) } + nValue := int64(-1) ^ int64(val) + return di.writeString(strconv.FormatInt(nValue, 10)) + case cborTypeByteString: b := di.d.parseByteString() return di.encodeByteString(b) @@ -229,7 +228,6 @@ func (di *diagnose) value() error { if err != nil { return err } - return di.encodeTextString(string(b), '"') case cborTypeArray: @@ -245,15 +243,11 @@ func (di *diagnose) value() error { return err } } - if err := di.value(); err != nil { return err } } - - if err := di.writeByte(']'); err != nil { - return err - } + return di.writeByte(']') case cborTypeMap: _, _, val := di.d.getHead() @@ -268,25 +262,19 @@ func (di *diagnose) value() error { return err } } - // key if err := di.value(); err != nil { return err } - if err := di.writeString(": "); err != nil { return err } - // value if err := di.value(); err != nil { return err } } - - if err := di.writeByte('}'); err != nil { - return err - } + return di.writeByte('}') case cborTypeTag: _, _, tagNum := di.d.getHead() @@ -313,9 +301,7 @@ func (di *diagnose) value() error { if err := di.value(); err != nil { return err } - if err := di.writeByte(')'); err != nil { - return err - } + return di.writeByte(')') } case cborTypePrimitives: @@ -343,9 +329,7 @@ func (di *diagnose) value() error { if err := di.writeString(strconv.FormatUint(val, 10)); err != nil { return err } - if err := di.writeByte(')'); err != nil { - return err - } + return di.writeByte(')') } } @@ -376,7 +360,6 @@ var rawBase32Encoding = base32.StdEncoding.WithPadding(base32.NoPadding) var rawBase32HexEncoding = base32.HexEncoding.WithPadding(base32.NoPadding) func (di *diagnose) encodeByteString(val []byte) error { - if len(val) > 0 { if di.opts.ByteStringText && utf8.Valid(val) { return di.encodeTextString(string(val), '\'') @@ -391,7 +374,6 @@ func (di *diagnose) encodeByteString(val []byte) error { if err := di.writeString(string(data)); err != nil { return err } - return di.writeString(">>") } } @@ -421,6 +403,7 @@ func (di *diagnose) encodeByteString(val []byte) error { return err } } + return di.writeByte('\'') case "base32": if err := di.writeString("b32'"); err != nil { @@ -431,6 +414,7 @@ func (di *diagnose) encodeByteString(val []byte) error { return err } encoder.Close() + return di.writeByte('\'') case "base32hex": if err := di.writeString("h32'"); err != nil { @@ -441,6 +425,7 @@ func (di *diagnose) encodeByteString(val []byte) error { return err } encoder.Close() + return di.writeByte('\'') case "base64": if err := di.writeString("b64'"); err != nil { @@ -451,12 +436,11 @@ func (di *diagnose) encodeByteString(val []byte) error { return err } encoder.Close() + return di.writeByte('\'') default: return errors.New("cbor: invalid ByteStringEncoding option " + strconv.Quote(di.opts.ByteStringEncoding)) } - - return di.writeByte('\'') } var utf16SurrSelf = rune(0x10000) diff --git a/diagnose_test.go b/diagnose_test.go index f6bad211..d27ec731 100644 --- a/diagnose_test.go +++ b/diagnose_test.go @@ -89,15 +89,15 @@ func TestDiagnoseExamples(t *testing.T) { }, { hexDecode("f90000"), - `0.0`, // SKIP 18 + `0.0`, }, { hexDecode("f98000"), - `-0.0`, // SKIP 19 + `-0.0`, }, { hexDecode("f93c00"), - `1.0`, // SKIP 20 + `1.0`, }, { hexDecode("fb3ff199999999999a"), @@ -109,31 +109,31 @@ func TestDiagnoseExamples(t *testing.T) { }, { hexDecode("f97bff"), - `65504.0`, // SKIP 23 + `65504.0`, }, { hexDecode("fa47c35000"), - `100000.0`, // SKIP 24 + `100000.0`, }, { hexDecode("fa7f7fffff"), - `3.4028234663852886e+38`, // SKIP 25 + `3.4028234663852886e+38`, }, { hexDecode("fb7e37e43c8800759c"), - `1.0e+300`, // SKIP 26 + `1.0e+300`, }, { hexDecode("f90001"), - `5.960464477539063e-8`, // SKIP 27 + `5.960464477539063e-8`, }, { hexDecode("f90400"), - `0.00006103515625`, // SKIP 28 + `0.00006103515625`, }, { hexDecode("f9c400"), - `-4.0`, // SKIP 29 + `-4.0`, }, { hexDecode("fbc010666666666666"), @@ -209,7 +209,7 @@ func TestDiagnoseExamples(t *testing.T) { }, { hexDecode("c1fb41d452d9ec200000"), - `1(1363896240.5)`, // SKIP 48 + `1(1363896240.5)`, }, { hexDecode("d74401020304"),