Skip to content

Commit

Permalink
codec: fix suggestions raised by golint -min_confidence 0.9, misspell…
Browse files Browse the repository at this point in the history
…ings and ineffassign
  • Loading branch information
ugorji committed Nov 12, 2017
1 parent 3b86b15 commit 5a66da2
Show file tree
Hide file tree
Showing 25 changed files with 1,160 additions and 1,146 deletions.
8 changes: 4 additions & 4 deletions codec/0doc.go
Expand Up @@ -2,8 +2,9 @@
// Use of this source code is governed by a MIT license found in the LICENSE file.

/*
High Performance, Feature-Rich Idiomatic Go 1.4+ codec/encoding library for
binc, msgpack, cbor, json
Package codec provides a
High Performance, Feature-Rich Idiomatic Go 1.4+ codec/encoding library
for binc, msgpack, cbor, json.
Supported Serialization formats are:
Expand Down Expand Up @@ -109,7 +110,7 @@ This symmetry is important to reduce chances of issues happening because the
encoding and decoding sides are out of sync e.g. decoded via very specific
encoding.TextUnmarshaler but encoded via kind-specific generalized mode.
Consequently, if a type only defines one-half of the symetry
Consequently, if a type only defines one-half of the symmetry
(e.g. it implements UnmarshalJSON() but not MarshalJSON() ),
then that type doesn't satisfy the check and we will continue walking down the
decision tree.
Expand Down Expand Up @@ -203,4 +204,3 @@ Please see http://github.com/ugorji/go-codec-bench .
*/
package codec

2 changes: 1 addition & 1 deletion codec/README.md
Expand Up @@ -107,7 +107,7 @@ This symmetry is important to reduce chances of issues happening because the
encoding and decoding sides are out of sync e.g. decoded via very specific
encoding.TextUnmarshaler but encoded via kind-specific generalized mode.

Consequently, if a type only defines one-half of the symetry
Consequently, if a type only defines one-half of the symmetry
(e.g. it implements UnmarshalJSON() but not MarshalJSON() ),
then that type doesn't satisfy the check and we will continue walking down the
decision tree.
Expand Down
22 changes: 13 additions & 9 deletions codec/binc.go
Expand Up @@ -208,7 +208,7 @@ func (e *bincEncDriver) EncodeString(c charEncoding, v string) {

func (e *bincEncDriver) EncodeSymbol(v string) {
// if WriteSymbolsNoRefs {
// e.encodeString(c_UTF8, v)
// e.encodeString(cUTF8, v)
// return
// }

Expand All @@ -218,10 +218,10 @@ func (e *bincEncDriver) EncodeSymbol(v string) {

l := len(v)
if l == 0 {
e.encBytesLen(c_UTF8, 0)
e.encBytesLen(cUTF8, 0)
return
} else if l == 1 {
e.encBytesLen(c_UTF8, 1)
e.encBytesLen(cUTF8, 1)
e.w.writen1(v[0])
return
}
Expand Down Expand Up @@ -280,7 +280,7 @@ func (e *bincEncDriver) EncodeStringBytes(c charEncoding, v []byte) {

func (e *bincEncDriver) encBytesLen(c charEncoding, length uint64) {
//TODO: support bincUnicodeOther (for now, just use string or bytearray)
if c == c_RAW {
if c == cRAW {
e.encLen(bincVdByteArray<<4, length)
} else {
e.encLen(bincVdString<<4, length)
Expand Down Expand Up @@ -365,9 +365,10 @@ func (d *bincDecDriver) ContainerType() (vt valueType) {
return valueTypeArray
} else if d.vd == bincVdMap {
return valueTypeMap
} else {
// d.d.errorf("isContainerType: unsupported parameter: %v", vt)
}
// else {
// d.d.errorf("isContainerType: unsupported parameter: %v", vt)
// }
return valueTypeUnset
}

Expand Down Expand Up @@ -399,7 +400,7 @@ func (d *bincDecDriver) DecodeBuiltin(rt uintptr, v interface{}) {
if err != nil {
panic(err)
}
var vt *time.Time = v.(*time.Time)
var vt = v.(*time.Time)
*vt = tt
d.bdRead = false
return
Expand Down Expand Up @@ -637,7 +638,7 @@ func (d *bincDecDriver) decStringAndBytes(bs []byte, withString, zerocopy bool)
d.bdRead = false
return
}
var slen int = -1
var slen = -1
// var ok bool
switch d.vd {
case bincVdString, bincVdByteArray:
Expand Down Expand Up @@ -910,6 +911,7 @@ type BincHandle struct {
noElemSeparators
}

// SetBytesExt sets an extension
func (h *BincHandle) SetBytesExt(rt reflect.Type, tag uint64, ext BytesExt) (err error) {
return h.SetExt(rt, tag, &setExtWrapper{b: ext})
}
Expand All @@ -922,7 +924,9 @@ func (h *BincHandle) newDecDriver(d *Decoder) decDriver {
return &bincDecDriver{d: d, h: h, r: d.r, br: d.bytes}
}

func (_ *BincHandle) IsBuiltinType(rt uintptr) bool {
// IsBuiltinType returns true for time.Time, else false.
// only time.Time is builtin.
func (h *BincHandle) IsBuiltinType(rt uintptr) bool {
return rt == timeTypId
}

Expand Down
10 changes: 7 additions & 3 deletions codec/cbor.go
Expand Up @@ -38,6 +38,8 @@ const (
cborBdBreak = 0xff
)

// These define some in-stream descriptors for
// manual encoding e.g. when doing explicit indefinite-length
const (
CborStreamBytes byte = 0x5f
CborStreamString = 0x7f
Expand Down Expand Up @@ -181,7 +183,7 @@ func (e *cborEncDriver) EncodeString(c charEncoding, v string) {
}

func (e *cborEncDriver) EncodeStringBytes(c charEncoding, v []byte) {
if c == c_RAW {
if c == cRAW {
e.encStringBytesS(cborBaseBytes, stringView(v))
} else {
e.encStringBytesS(cborBaseString, stringView(v))
Expand Down Expand Up @@ -261,9 +263,10 @@ func (d *cborDecDriver) ContainerType() (vt valueType) {
return valueTypeArray
} else if d.bd == cborBdIndefiniteMap || (d.bd >= cborBaseMap && d.bd < cborBaseTag) {
return valueTypeMap
} else {
// d.d.errorf("isContainerType: unsupported parameter: %v", vt)
}
// else {
// d.d.errorf("isContainerType: unsupported parameter: %v", vt)
// }
return valueTypeUnset
}

Expand Down Expand Up @@ -622,6 +625,7 @@ type CborHandle struct {
IndefiniteLength bool
}

// SetInterfaceExt sets an extension
func (h *CborHandle) SetInterfaceExt(rt reflect.Type, tag uint64, ext InterfaceExt) (err error) {
return h.SetExt(rt, tag, &setExtWrapper{i: ext})
}
Expand Down
8 changes: 4 additions & 4 deletions codec/codec_test.go
Expand Up @@ -40,11 +40,11 @@ type testCustomStringT string
// make this a mapbyslice
type testMbsT []interface{}

func (_ testMbsT) MapBySlice() {}
func (testMbsT) MapBySlice() {}

type testMbsCustStrT []testCustomStringT

func (_ testMbsCustStrT) MapBySlice() {}
func (testMbsCustStrT) MapBySlice() {}

type testVerifyFlag uint8

Expand Down Expand Up @@ -716,7 +716,7 @@ func testCodecMiscOne(t *testing.T, h Handle) {
// logT(t, "------- Expecting error because we cannot unmarshal to int32 nil ptr")
// failT(t)
// }
var i2 int32 = 0
var i2 int32
testUnmarshalErr(&i2, b, h, t, "int32-ptr")
if i2 != int32(32) {
logT(t, "------- didn't unmarshal to 32: Received: %d", i2)
Expand Down Expand Up @@ -1004,7 +1004,7 @@ func testCodecRpcOne(t *testing.T, rr Rpc, h Handle, doRequest bool, exitSleepMs
// opts.MapType = mapStrIntfTyp
// opts.RawToString = false
serverExitChan := make(chan bool, 1)
var serverExitFlag uint64 = 0
var serverExitFlag uint64
serverFn := func() {
for {
conn1, err1 := ln.Accept()
Expand Down
56 changes: 29 additions & 27 deletions codec/decode.go
Expand Up @@ -20,12 +20,12 @@ const (
)

var (
onlyMapOrArrayCanDecodeIntoStructErr = errors.New("only encoded map or array can be decoded into a struct")
cannotDecodeIntoNilErr = errors.New("cannot decode into nil")
errOnlyMapOrArrayCanDecodeIntoStruct = errors.New("only encoded map or array can be decoded into a struct")
errCannotDecodeIntoNil = errors.New("cannot decode into nil")

decUnreadByteNothingToReadErr = errors.New("cannot unread - nothing has been read")
decUnreadByteLastByteNotReadErr = errors.New("cannot unread - last byte has not been read")
decUnreadByteUnknownErr = errors.New("cannot unread - reason unknown")
errDecUnreadByteNothingToRead = errors.New("cannot unread - nothing has been read")
errDecUnreadByteLastByteNotRead = errors.New("cannot unread - last byte has not been read")
errDecUnreadByteUnknown = errors.New("cannot unread - reason unknown")
)

// decReader abstracts the reading source, allowing implementations that can
Expand Down Expand Up @@ -108,22 +108,20 @@ type decDriver interface {
uncacheRead()
}

// type decNoSeparator struct {}
// func (_ decNoSeparator) ReadEnd() {}

type decDriverNoopContainerReader struct{}

func (_ decDriverNoopContainerReader) ReadArrayStart() (v int) { return }
func (_ decDriverNoopContainerReader) ReadArrayElem() {}
func (_ decDriverNoopContainerReader) ReadArrayEnd() {}
func (_ decDriverNoopContainerReader) ReadMapStart() (v int) { return }
func (_ decDriverNoopContainerReader) ReadMapElemKey() {}
func (_ decDriverNoopContainerReader) ReadMapElemValue() {}
func (_ decDriverNoopContainerReader) ReadMapEnd() {}
func (_ decDriverNoopContainerReader) CheckBreak() (v bool) { return }
func (x decDriverNoopContainerReader) ReadArrayStart() (v int) { return }
func (x decDriverNoopContainerReader) ReadArrayElem() {}
func (x decDriverNoopContainerReader) ReadArrayEnd() {}
func (x decDriverNoopContainerReader) ReadMapStart() (v int) { return }
func (x decDriverNoopContainerReader) ReadMapElemKey() {}
func (x decDriverNoopContainerReader) ReadMapElemValue() {}
func (x decDriverNoopContainerReader) ReadMapEnd() {}
func (x decDriverNoopContainerReader) CheckBreak() (v bool) { return }

// func (_ decNoSeparator) uncacheRead() {}
// func (x decNoSeparator) uncacheRead() {}

// DecodeOptions captures configuration options during decode.
type DecodeOptions struct {
// MapType specifies type to use during schema-less decoding of a map in the stream.
// If nil, we use map[interface{}]interface{}
Expand Down Expand Up @@ -334,7 +332,7 @@ func (z *bufioDecReader) UnreadByte() (err error) {
}
return
}
return decUnreadByteNothingToReadErr
return errDecUnreadByteNothingToRead
}

func (z *bufioDecReader) numread() int {
Expand Down Expand Up @@ -602,11 +600,11 @@ func (z *ioDecReader) UnreadByte() (err error) {
case 2:
z.ls = 1
case 0:
err = decUnreadByteNothingToReadErr
err = errDecUnreadByteNothingToRead
case 1:
err = decUnreadByteLastByteNotReadErr
err = errDecUnreadByteLastByteNotRead
default:
err = decUnreadByteUnknownErr
err = errDecUnreadByteUnknown
}
return
}
Expand Down Expand Up @@ -745,7 +743,7 @@ func (z *ioDecReader) stopTrack() (bs []byte) {

// ------------------------------------

var bytesDecReaderCannotUnreadErr = errors.New("cannot unread last byte read")
var errBytesDecReaderCannotUnread = errors.New("cannot unread last byte read")

// bytesDecReader is a decReader that reads off a byte slice with zero copying
type bytesDecReader struct {
Expand All @@ -768,7 +766,7 @@ func (z *bytesDecReader) numread() int {

func (z *bytesDecReader) unreadn1() {
if z.c == 0 || len(z.b) == 0 {
panic(bytesDecReaderCannotUnreadErr)
panic(errBytesDecReaderCannotUnread)
}
z.c--
z.a++
Expand Down Expand Up @@ -1186,7 +1184,7 @@ func (d *Decoder) kStruct(f *codecFnInfo, rv reflect.Value) {
}
dd.ReadArrayEnd()
} else {
d.error(onlyMapOrArrayCanDecodeIntoStructErr)
d.error(errOnlyMapOrArrayCanDecodeIntoStruct)
return
}
}
Expand Down Expand Up @@ -1781,6 +1779,8 @@ func (d *Decoder) resetCommon() {
}
}

// Reset the Decoder with a new Reader to decode from,
// clearing all state from last run(s).
func (d *Decoder) Reset(r io.Reader) {
if d.h.ReaderBufferSize > 0 {
d.bi.buf = make([]byte, 0, d.h.ReaderBufferSize)
Expand All @@ -1795,6 +1795,8 @@ func (d *Decoder) Reset(r io.Reader) {
d.resetCommon()
}

// ResetBytes resets the Decoder with a new []byte to decode from,
// clearing all state from last run(s).
func (d *Decoder) ResetBytes(in []byte) {
d.bytes = true
d.rb.reset(in)
Expand Down Expand Up @@ -2002,7 +2004,7 @@ func (d *Decoder) decode(iv interface{}) {
// check nil and interfaces explicitly,
// so that type switches just have a run of constant non-interface types.
if iv == nil {
d.error(cannotDecodeIntoNilErr)
d.error(errCannotDecodeIntoNil)
return
}
if v, ok := iv.(Selfer); ok {
Expand Down Expand Up @@ -2153,7 +2155,7 @@ func (d *Decoder) ensureDecodeable(rv reflect.Value) (rv2 reflect.Value) {
return
}
if !rv.IsValid() {
d.error(cannotDecodeIntoNilErr)
d.error(errCannotDecodeIntoNil)
return
}
if !rv.CanInterface() {
Expand All @@ -2175,7 +2177,7 @@ func (d *Decoder) ensureDecodeable(rv reflect.Value) (rv2 reflect.Value) {

// func (d *Decoder) errNotValidPtrValue(rv reflect.Value) {
// if !rv.IsValid() {
// d.error(cannotDecodeIntoNilErr)
// d.error(errCannotDecodeIntoNil)
// return
// }
// if !rv.CanInterface() {
Expand Down

0 comments on commit 5a66da2

Please sign in to comment.