Skip to content

Commit

Permalink
golint: comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zerosnake0 committed Oct 8, 2020
1 parent 6d08ca0 commit 4c4977c
Show file tree
Hide file tree
Showing 39 changed files with 141 additions and 43 deletions.
2 changes: 1 addition & 1 deletion decoder_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (decCfg *DecoderConfig) UnmarshalFromString(s string, obj interface{}) erro
return decCfg.Unmarshal(localStringToBytes(s), obj)
}

// UnmarshalFromReader behave like json.Unmarshal but with a io.Reader
// UnmarshalFromReader behave like json.Unmarshal but with an io.Reader
func (decCfg *DecoderConfig) UnmarshalFromReader(r io.Reader, obj interface{}) error {
it := decCfg.NewIterator()
err := it.UnmarshalFromReader(r, obj)
Expand Down
4 changes: 2 additions & 2 deletions decoder_config_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package jzon

// NewIterator returns a new iterator.
func (decCfg *DecoderConfig) NewIterator() *Iterator {
it := defaultIteratorPool.BorrowIterator()
it := defaultIteratorPool.borrowIterator()
it.cfg = decCfg
it.useNumber = decCfg.useNumber
it.disallowUnknownFields = decCfg.disallowUnknownFields
Expand All @@ -11,5 +11,5 @@ func (decCfg *DecoderConfig) NewIterator() *Iterator {

func (decCfg *DecoderConfig) returnIterator(it *Iterator) {
it.cfg = nil
defaultIteratorPool.ReturnIterator(it)
defaultIteratorPool.returnIterator(it)
}
4 changes: 2 additions & 2 deletions encoder_config_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package jzon

// NewStreamer returns a new streamer.
func (encCfg *EncoderConfig) NewStreamer() *Streamer {
s := defaultStreamerPool.BorrowStreamer()
s := defaultStreamerPool.borrowStreamer()
s.cfg = encCfg
s.EscapeHTML(s.cfg.escapeHTML)
return s
}

func (encCfg *EncoderConfig) returnStreamer(s *Streamer) {
s.cfg = nil
defaultStreamerPool.ReturnStreamer(s)
defaultStreamerPool.returnStreamer(s)
}
3 changes: 3 additions & 0 deletions errors_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"errors"
)

// ErrNoAttachedWriter there is no writer attaching to the streamer
var ErrNoAttachedWriter = errors.New("no attached writer")

// ErrFloatIsInfinity the float to write is infinity
var ErrFloatIsInfinity = errors.New("float is infinity")

// ErrFloatIsNan the float to write is NaN
var ErrFloatIsNan = errors.New("float is NaN")
23 changes: 16 additions & 7 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ type Iterator struct {
disallowUnknownFields bool
}

// NewIterator returns a new iterator.
func NewIterator() *Iterator {
return DefaultDecoderConfig.NewIterator()
}

// Release the iterator, the iterator should not be reused after call.
func (it *Iterator) Release() {
it.cfg.returnIterator(it)
}
Expand All @@ -67,12 +69,13 @@ func (it *Iterator) reset() {
it.iteratorEmbedded = iteratorEmbedded{}
}

/*
* In reset methods, explicit assignment is faster than then following
* *it = Iterator{ ... }
* When the above code is used, runtime.duffcopy and runtime.duffzero will be used
* which will slow down our code (correct me if I am wrong)
*/
// Reset the iterator with an io.Reader
// if the reader is nil, reset the iterator to its initial state
//
// In reset methods, explicit assignment is faster than then following
// *it = Iterator{ ... }
// When the above code is used, runtime.duffcopy and runtime.duffzero will be used
// which will slow down our code (correct me if I am wrong)
func (it *Iterator) Reset(r io.Reader) {
switch v := r.(type) {
case nil:
Expand All @@ -90,6 +93,7 @@ func (it *Iterator) Reset(r io.Reader) {
it.iteratorEmbedded = iteratorEmbedded{}
}

// ResetBytes resets iterator with a byte slice
func (it *Iterator) ResetBytes(data []byte) {
it.reader = nil
it.buffer = data
Expand All @@ -99,6 +103,7 @@ func (it *Iterator) ResetBytes(data []byte) {
it.iteratorEmbedded = iteratorEmbedded{}
}

// Buffer returns the current slice buffer of the iterator.
func (it *Iterator) Buffer() []byte {
return it.buffer[it.head:it.tail]
}
Expand All @@ -121,6 +126,7 @@ func (it *Iterator) errorLocation() []byte {
return it.buffer[head:tail]
}

// WrapError wraps the error with the current iterator location
func (it *Iterator) WrapError(err error) *DecodeError {
if e, ok := err.(*DecodeError); ok {
return e
Expand Down Expand Up @@ -233,7 +239,7 @@ func (it *Iterator) nextToken() (ret byte, err error) {
}
}

// Read until the first valid token is found, only the whitespaces are consumed
// NextValueType read until the first valid token is found, only the whitespaces are consumed
func (it *Iterator) NextValueType() (ValueType, error) {
v, err := it.nextToken()
return valueTypeMap[v], err
Expand All @@ -254,11 +260,13 @@ func (it *Iterator) unmarshal(obj interface{}) error {
return nil
}

// Unmarshal behave like standard json.Unmarshal
func (it *Iterator) Unmarshal(data []byte, obj interface{}) error {
it.ResetBytes(data)
return it.unmarshal(obj)
}

// Valid behave like standard json.Valid
func (it *Iterator) Valid(data []byte) bool {
it.ResetBytes(data)
err := it.Skip()
Expand All @@ -269,6 +277,7 @@ func (it *Iterator) Valid(data []byte) bool {
return err == io.EOF
}

// UnmarshalFromReader behave like standard json.Unmarshal but with an io.Reader
func (it *Iterator) UnmarshalFromReader(r io.Reader, obj interface{}) error {
it.Reset(r)
return it.unmarshal(obj)
Expand Down
29 changes: 16 additions & 13 deletions iterator_array.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package jzon

/*
* var (
* more bool
* err error
* )
* for more, err = it.ReadArray();
* more;
* more, err = it.ReadArrayMore() {
* }
* if err != nil {
* // error handling
* }
*/
// ReadArrayBegin starts to read an array
//
// var (
// more bool
// err error
// )
// for more, err = it.ReadArray();
// more;
// more, err = it.ReadArrayMore() {
// }
// if err != nil {
// // error handling
// }
func (it *Iterator) ReadArrayBegin() (ret bool, err error) {
c, err := it.nextToken()
if err != nil {
Expand All @@ -33,6 +33,7 @@ func (it *Iterator) ReadArrayBegin() (ret bool, err error) {
return true, nil
}

// ReadArrayMore tells if there is more item to read in the array
func (it *Iterator) ReadArrayMore() (ret bool, err error) {
c, err := it.nextToken()
if err != nil {
Expand All @@ -50,6 +51,8 @@ func (it *Iterator) ReadArrayMore() (ret bool, err error) {
}
}

// ReadArrayCB reads the array with a callback
// The caller should make sure that the callback is correct
func (it *Iterator) ReadArrayCB(cb func(*Iterator) error) error {
c, err := it.nextToken()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions iterator_bool.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jzon

// ReadBool reads a boolean value
func (it *Iterator) ReadBool() (bool, error) {
c, err := it.nextToken()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions iterator_float32.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"
)

// ReadFloat32 reads a float32 value
func (it *Iterator) ReadFloat32() (float32, error) {
c, err := it.nextToken()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions iterator_float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func (it *Iterator) readFloat64(c byte) (float64, error) {
return f, err
}

// ReadFloat64 reads a float64 value
func (it *Iterator) ReadFloat64() (float64, error) {
c, err := it.nextToken()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions iterator_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func init() {
}
}

// ReadInt reads an int value
func (it *Iterator) ReadInt() (int, error) {
if strconv.IntSize == 32 {
i, err := it.ReadInt32()
Expand All @@ -30,6 +31,7 @@ func (it *Iterator) ReadInt() (int, error) {
return int(i), err
}

// ReadUint reads an uint value
func (it *Iterator) ReadUint() (uint, error) {
if strconv.IntSize == 32 {
u, err := it.ReadUint32()
Expand Down
2 changes: 2 additions & 0 deletions iterator_int16.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
maxUint16Mod10 = int8(math.MaxUint16 - maxUint16Div10*10)
)

// ReadUint16 reads an uint16 value
func (it *Iterator) ReadUint16() (uint16, error) {
c, err := it.nextToken()
if err != nil {
Expand Down Expand Up @@ -92,6 +93,7 @@ func (it *Iterator) readInt16(c byte) (int16, error) {
return int16(v), nil
}

// ReadInt16 reads an int16 value
func (it *Iterator) ReadInt16() (int16, error) {
c, err := it.nextToken()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions iterator_int32.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
maxUint32Mod10 = int8(math.MaxUint32 - maxUint32Div10*10)
)

// ReadUint32 reads an uint32 value
func (it *Iterator) ReadUint32() (uint32, error) {
c, err := it.nextToken()
if err != nil {
Expand Down Expand Up @@ -92,6 +93,7 @@ func (it *Iterator) readInt32(c byte) (int32, error) {
return int32(v), nil
}

// ReadInt32 reads an int32 value
func (it *Iterator) ReadInt32() (int32, error) {
c, err := it.nextToken()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions iterator_int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
maxUint64Mod10 = int8(math.MaxUint64 - maxUint64Div10*10)
)

// ReadUint64 reads an uint64 value
func (it *Iterator) ReadUint64() (uint64, error) {
c, err := it.nextToken()
if err != nil {
Expand Down Expand Up @@ -93,6 +94,7 @@ func (it *Iterator) readInt64(c byte) (int64, error) {
return int64(v), nil
}

// ReadInt64 reads an int64 value
func (it *Iterator) ReadInt64() (int64, error) {
c, err := it.nextToken()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions iterator_int8.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
maxUint8Mod10 = int8(math.MaxUint8 - maxUint8Div10*10)
)

// ReadUint8 reads an Uint8 value
func (it *Iterator) ReadUint8() (uint8, error) {
c, err := it.nextToken()
if err != nil {
Expand Down Expand Up @@ -97,6 +98,7 @@ func (it *Iterator) readInt8(c byte) (int8, error) {
return int8(v), nil
}

// ReadInt8 reads an int8 value
func (it *Iterator) ReadInt8() (int8, error) {
c, err := it.nextToken()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions iterator_null.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jzon

// ReadNull reads a nil
func (it *Iterator) ReadNull() error {
c, err := it.nextToken()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions iterator_number.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func (it *Iterator) readNumberAsString(c byte) (n string, err error) {
return
}

// ReadNumber reads a Number(json.Number)
func (it *Iterator) ReadNumber() (n Number, err error) {
c, err := it.nextToken()
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions iterator_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (it *Iterator) skipObjectField() error {
return nil
}

// ReadObjectBegin starts to read an object
func (it *Iterator) ReadObjectBegin() (_ bool, _ string, err error) {
c, err := it.nextToken()
if err != nil {
Expand Down Expand Up @@ -77,6 +78,7 @@ func (it *Iterator) ReadObjectBegin() (_ bool, _ string, err error) {
}
}

// ReadObjectMore tells if there is more field to read in the object
func (it *Iterator) ReadObjectMore() (_ bool, _ string, err error) {
c, err := it.nextToken()
if err != nil {
Expand Down Expand Up @@ -109,6 +111,8 @@ func (it *Iterator) ReadObjectMore() (_ bool, _ string, err error) {
}
}

// ReadObjectCB reads the object with a callback
// The caller should make sure that the callback is correct
func (it *Iterator) ReadObjectCB(cb func(it *Iterator, field string) error) error {
c, err := it.nextToken()
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions iterator_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
)

var (
defaultIteratorPool = NewIteratorPool()
defaultIteratorPool = newIteratorPool()
)

type IteratorPool struct {
type iteratorPool struct {
pool sync.Pool
}

func NewIteratorPool() *IteratorPool {
return &IteratorPool{
func newIteratorPool() *iteratorPool {
return &iteratorPool{
pool: sync.Pool{
New: func() interface{} {
return &Iterator{
Expand All @@ -25,12 +25,12 @@ func NewIteratorPool() *IteratorPool {
}
}

func (p *IteratorPool) BorrowIterator() *Iterator {
func (p *iteratorPool) borrowIterator() *Iterator {
it := p.pool.Get().(*Iterator)
return it
}

func (p *IteratorPool) ReturnIterator(it *Iterator) {
func (p *iteratorPool) returnIterator(it *Iterator) {
it.reset()
p.pool.Put(it)
}
6 changes: 3 additions & 3 deletions iterator_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import (
func TestIteratorPool(t *testing.T) {
must := require.New(t)

pool := NewIteratorPool()
pool := newIteratorPool()

f := func(cb func(it *Iterator)) {
it := pool.BorrowIterator()
it := pool.borrowIterator()
must.Nil(it.reader)
must.Nil(it.buffer)
must.Equal(0, it.offset)
must.Equal(0, it.head)
must.Equal(0, it.tail)
cb(it)
pool.ReturnIterator(it)
pool.returnIterator(it)
must.Nil(it.reader)
must.Nil(it.buffer)
}
Expand Down
Loading

0 comments on commit 4c4977c

Please sign in to comment.