Skip to content

Commit

Permalink
Fix for linters.
Browse files Browse the repository at this point in the history
Signed-off-by: Yan Qing <[email protected]>
  • Loading branch information
zensh committed Jan 26, 2023
1 parent 78118bc commit a455df5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 42 deletions.
46 changes: 15 additions & 31 deletions diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -229,7 +228,6 @@ func (di *diagnose) value() error {
if err != nil {
return err
}

return di.encodeTextString(string(b), '"')

case cborTypeArray:
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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:
Expand Down Expand Up @@ -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(')')
}
}

Expand Down Expand Up @@ -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), '\'')
Expand All @@ -391,7 +374,6 @@ func (di *diagnose) encodeByteString(val []byte) error {
if err := di.writeString(string(data)); err != nil {
return err
}

return di.writeString(">>")
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions diagnose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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"),
Expand Down Expand Up @@ -209,7 +209,7 @@ func TestDiagnoseExamples(t *testing.T) {
},
{
hexDecode("c1fb41d452d9ec200000"),
`1(1363896240.5)`, // SKIP 48
`1(1363896240.5)`,
},
{
hexDecode("d74401020304"),
Expand Down

0 comments on commit a455df5

Please sign in to comment.