diff --git a/internal/command/command.go b/internal/command/command.go index 824f83ca3..87fce610c 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -20,7 +20,7 @@ var ( argumentRegex = regexp.MustCompile(`^\-{1,2}([\w\?\.\-]+)(=){0,1}(.*)$`) ) -// Custom initialization. +// Init does custom initialization. func Init(args ...string) { if len(args) == 0 { if len(defaultParsedArgs) == 0 && len(defaultParsedOptions) == 0 { diff --git a/internal/empty/empty.go b/internal/empty/empty.go index 023f759a9..af1b7fe75 100644 --- a/internal/empty/empty.go +++ b/internal/empty/empty.go @@ -286,10 +286,9 @@ func IsEmpty(value interface{}) bool { //} // IsNil checks whether given `value` is nil. -// Parameter `traceSource` is used for tracing to the source variable if given `value` is type -// of a pinter that also points to a pointer. It returns nil if the source is nil when `traceSource` -// is true. -// Note that it might use reflect feature which affects performance a little bit. +// Parameter `traceSource` is used for tracing to the source variable if given `value` is type of pinter +// that also points to a pointer. It returns nil if the source is nil when `traceSource` is true. +// Note that it might use reflect feature which affects performance a little. func IsNil(value interface{}, traceSource ...bool) bool { if value == nil { return true diff --git a/internal/structs/structs.go b/internal/structs/structs.go index d6204497c..d9dbfa83c 100644 --- a/internal/structs/structs.go +++ b/internal/structs/structs.go @@ -4,7 +4,7 @@ // If a copy of the MIT was not distributed with this file, // You can obtain one at https://github.com/gogf/gf. -// Package structs provides functions for struct conversion. +// Package structs provides functions for struct information retrieving and struct conversion. // // Inspired and improved from: https://github.com/fatih/structs package structs diff --git a/internal/structs/structs_field.go b/internal/structs/structs_field.go index 1af783adf..f72bc83e6 100644 --- a/internal/structs/structs_field.go +++ b/internal/structs/structs_field.go @@ -16,7 +16,7 @@ func (f *Field) Tag(key string) string { // TagLookup returns the value associated with key in the tag string. // If the key is present in the tag the value (which may be empty) -// is returned. Otherwise the returned value will be the empty string. +// is returned. Otherwise, the returned value will be the empty string. // The ok return value reports whether the value was explicitly set in // the tag string. If the tag does not have the conventional format, // the value returned by Lookup is unspecified. diff --git a/internal/structs/structs_tag.go b/internal/structs/structs_tag.go index a167e07e4..f7d8c85c7 100644 --- a/internal/structs/structs_tag.go +++ b/internal/structs/structs_tag.go @@ -53,7 +53,7 @@ func ParseTag(tag string) map[string]string { if i >= len(tag) { break } - quotedValue := string(tag[:i+1]) + quotedValue := tag[:i+1] tag = tag[i+1:] value, err := strconv.Unquote(quotedValue) if err != nil { @@ -137,6 +137,7 @@ func getFieldValues(value interface{}) ([]*Field, error) { goto exitLoop } } + exitLoop: for reflectKind == reflect.Ptr { reflectValue = reflectValue.Elem() diff --git a/internal/structs/structs_type.go b/internal/structs/structs_type.go index ed30ecab2..40cb02050 100644 --- a/internal/structs/structs_type.go +++ b/internal/structs/structs_type.go @@ -37,13 +37,16 @@ func StructType(object interface{}) (*Type, error) { reflectValue = reflectValue.Elem() reflectKind = reflectValue.Kind() } + case reflect.Array, reflect.Slice: reflectValue = reflect.New(reflectValue.Type().Elem()).Elem() reflectKind = reflectValue.Kind() + default: goto exitLoop } } + exitLoop: for reflectKind == reflect.Ptr { reflectValue = reflectValue.Elem() @@ -63,7 +66,7 @@ exitLoop: }, nil } -// Signature returns an unique string as this type. +// Signature returns a unique string as this type. func (t Type) Signature() string { return t.PkgPath() + "/" + t.String() } diff --git a/internal/utils/utils_io.go b/internal/utils/utils_io.go index 1d227bd79..9da382d4e 100644 --- a/internal/utils/utils_io.go +++ b/internal/utils/utils_io.go @@ -21,7 +21,7 @@ type ReadCloser struct { repeatable bool } -// NewRepeatReadCloser creates and returns a RepeatReadCloser object. +// NewReadCloser creates and returns a RepeatReadCloser object. func NewReadCloser(content []byte, repeatable bool) io.ReadCloser { return &ReadCloser{ content: content, @@ -29,7 +29,7 @@ func NewReadCloser(content []byte, repeatable bool) io.ReadCloser { } } -// NewRepeatReadCloserWithReadCloser creates and returns a RepeatReadCloser object +// NewReadCloserWithReadCloser creates and returns a RepeatReadCloser object // with given io.ReadCloser. func NewReadCloserWithReadCloser(r io.ReadCloser, repeatable bool) (io.ReadCloser, error) { content, err := ioutil.ReadAll(r) diff --git a/net/gtcp/gtcp_conn.go b/net/gtcp/gtcp_conn.go index 2a2c8e61b..ff0154c13 100644 --- a/net/gtcp/gtcp_conn.go +++ b/net/gtcp/gtcp_conn.go @@ -15,18 +15,18 @@ import ( "time" ) -// TCP connection object. +// Conn is the TCP connection object. type Conn struct { - net.Conn // Underlying TCP connection object. - reader *bufio.Reader // Buffer reader for connection. - recvDeadline time.Time // Timeout point for reading. - sendDeadline time.Time // Timeout point for writing. - recvBufferWait time.Duration // Interval duration for reading buffer. + net.Conn // Underlying TCP connection object. + reader *bufio.Reader // Buffer reader for connection. + receiveDeadline time.Time // Timeout point for reading. + sendDeadline time.Time // Timeout point for writing. + receiveBufferWait time.Duration // Interval duration for reading buffer. } const ( // Default interval for reading buffer. - gRECV_ALL_WAIT_TIMEOUT = time.Millisecond + receiveAllWaitTimeout = time.Millisecond ) // NewConn creates and returns a new connection with given address. @@ -61,11 +61,11 @@ func NewConnKeyCrt(addr, crtFile, keyFile string) (*Conn, error) { // NewConnByNetConn creates and returns a TCP connection object with given net.Conn object. func NewConnByNetConn(conn net.Conn) *Conn { return &Conn{ - Conn: conn, - reader: bufio.NewReader(conn), - recvDeadline: time.Time{}, - sendDeadline: time.Time{}, - recvBufferWait: gRECV_ALL_WAIT_TIMEOUT, + Conn: conn, + reader: bufio.NewReader(conn), + receiveDeadline: time.Time{}, + sendDeadline: time.Time{}, + receiveBufferWait: receiveAllWaitTimeout, } } @@ -84,7 +84,7 @@ func (c *Conn) Send(data []byte, retry ...Retry) error { if len(retry) > 0 { retry[0].Count-- if retry[0].Interval == 0 { - retry[0].Interval = gDEFAULT_RETRY_INTERVAL + retry[0].Interval = defaultRetryInternal } time.Sleep(retry[0].Interval) } @@ -113,13 +113,13 @@ func (c *Conn) Recv(length int, retry ...Retry) ([]byte, error) { if length > 0 { buffer = make([]byte, length) } else { - buffer = make([]byte, gDEFAULT_READ_BUFFER_SIZE) + buffer = make([]byte, defaultReadBufferSize) } for { if length < 0 && index > 0 { bufferWait = true - if err = c.SetReadDeadline(time.Now().Add(c.recvBufferWait)); err != nil { + if err = c.SetReadDeadline(time.Now().Add(c.receiveBufferWait)); err != nil { return nil, err } } @@ -132,9 +132,9 @@ func (c *Conn) Recv(length int, retry ...Retry) ([]byte, error) { break } } else { - if index >= gDEFAULT_READ_BUFFER_SIZE { + if index >= defaultReadBufferSize { // If it exceeds the buffer size, it then automatically increases its buffer size. - buffer = append(buffer, make([]byte, gDEFAULT_READ_BUFFER_SIZE)...) + buffer = append(buffer, make([]byte, defaultReadBufferSize)...) } else { // It returns immediately if received size is lesser than buffer size. if !bufferWait { @@ -150,7 +150,7 @@ func (c *Conn) Recv(length int, retry ...Retry) ([]byte, error) { } // Re-set the timeout when reading data. if bufferWait && isTimeout(err) { - if err = c.SetReadDeadline(c.recvDeadline); err != nil { + if err = c.SetReadDeadline(c.receiveDeadline); err != nil { return nil, err } err = nil @@ -163,7 +163,7 @@ func (c *Conn) Recv(length int, retry ...Retry) ([]byte, error) { } retry[0].Count-- if retry[0].Interval == 0 { - retry[0].Interval = gDEFAULT_RETRY_INTERVAL + retry[0].Interval = defaultRetryInternal } time.Sleep(retry[0].Interval) continue @@ -230,10 +230,10 @@ func (c *Conn) RecvTil(til []byte, retry ...Retry) ([]byte, error) { // RecvWithTimeout reads data from the connection with timeout. func (c *Conn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) (data []byte, err error) { - if err := c.SetRecvDeadline(time.Now().Add(timeout)); err != nil { + if err := c.SetreceiveDeadline(time.Now().Add(timeout)); err != nil { return nil, err } - defer c.SetRecvDeadline(time.Time{}) + defer c.SetreceiveDeadline(time.Time{}) data, err = c.Recv(length, retry...) return } @@ -269,16 +269,16 @@ func (c *Conn) SendRecvWithTimeout(data []byte, length int, timeout time.Duratio func (c *Conn) SetDeadline(t time.Time) error { err := c.Conn.SetDeadline(t) if err == nil { - c.recvDeadline = t + c.receiveDeadline = t c.sendDeadline = t } return err } -func (c *Conn) SetRecvDeadline(t time.Time) error { +func (c *Conn) SetreceiveDeadline(t time.Time) error { err := c.SetReadDeadline(t) if err == nil { - c.recvDeadline = t + c.receiveDeadline = t } return err } @@ -291,8 +291,8 @@ func (c *Conn) SetSendDeadline(t time.Time) error { return err } -// SetRecvBufferWait sets the buffer waiting timeout when reading all data from connection. +// SetreceiveBufferWait sets the buffer waiting timeout when reading all data from connection. // The waiting duration cannot be too long which might delay receiving data from remote address. -func (c *Conn) SetRecvBufferWait(bufferWaitDuration time.Duration) { - c.recvBufferWait = bufferWaitDuration +func (c *Conn) SetreceiveBufferWait(bufferWaitDuration time.Duration) { + c.receiveBufferWait = bufferWaitDuration } diff --git a/net/gtcp/gtcp_conn_pkg.go b/net/gtcp/gtcp_conn_pkg.go index a13a036f6..ef090adcf 100644 --- a/net/gtcp/gtcp_conn_pkg.go +++ b/net/gtcp/gtcp_conn_pkg.go @@ -13,11 +13,11 @@ import ( ) const ( - gPKG_HEADER_SIZE_DEFAULT = 2 // Header size for simple package protocol. - gPKG_HEADER_SIZE_MAX = 4 // Max header size for simple package protocol. + pkgHeaderSizeDefault = 2 // Header size for simple package protocol. + pkgHeaderSizeMax = 4 // Max header size for simple package protocol. ) -// Package option for simple protocol. +// PkgOption is package option for simple protocol. type PkgOption struct { // HeaderSize is used to mark the data length for next data receiving. // It's 2 bytes in default, 4 bytes max, which stands for the max data length @@ -51,10 +51,10 @@ func (c *Conn) SendPkg(data []byte, option ...PkgOption) error { length, pkgOption.MaxDataSize, ) } - offset := gPKG_HEADER_SIZE_MAX - pkgOption.HeaderSize - buffer := make([]byte, gPKG_HEADER_SIZE_MAX+len(data)) + offset := pkgHeaderSizeMax - pkgOption.HeaderSize + buffer := make([]byte, pkgHeaderSizeMax+len(data)) binary.BigEndian.PutUint32(buffer[0:], uint32(length)) - copy(buffer[gPKG_HEADER_SIZE_MAX:], data) + copy(buffer[pkgHeaderSizeMax:], data) if pkgOption.Retry.Count > 0 { return c.Send(buffer[offset:], pkgOption.Retry) } @@ -128,10 +128,10 @@ func (c *Conn) RecvPkg(option ...PkgOption) (result []byte, err error) { // RecvPkgWithTimeout reads data from connection with timeout using simple package protocol. func (c *Conn) RecvPkgWithTimeout(timeout time.Duration, option ...PkgOption) (data []byte, err error) { - if err := c.SetRecvDeadline(time.Now().Add(timeout)); err != nil { + if err := c.SetreceiveDeadline(time.Now().Add(timeout)); err != nil { return nil, err } - defer c.SetRecvDeadline(time.Time{}) + defer c.SetreceiveDeadline(time.Time{}) data, err = c.RecvPkg(option...) return } @@ -144,12 +144,12 @@ func getPkgOption(option ...PkgOption) (*PkgOption, error) { pkgOption = option[0] } if pkgOption.HeaderSize == 0 { - pkgOption.HeaderSize = gPKG_HEADER_SIZE_DEFAULT + pkgOption.HeaderSize = pkgHeaderSizeDefault } - if pkgOption.HeaderSize > gPKG_HEADER_SIZE_MAX { + if pkgOption.HeaderSize > pkgHeaderSizeMax { return nil, fmt.Errorf( `package header size %d definition exceeds max header size %d`, - pkgOption.HeaderSize, gPKG_HEADER_SIZE_MAX, + pkgOption.HeaderSize, pkgHeaderSizeMax, ) } if pkgOption.MaxDataSize == 0 { diff --git a/net/gtcp/gtcp_func.go b/net/gtcp/gtcp_func.go index 7643f918b..c2d520f49 100644 --- a/net/gtcp/gtcp_func.go +++ b/net/gtcp/gtcp_func.go @@ -16,9 +16,9 @@ import ( ) const ( - gDEFAULT_CONN_TIMEOUT = 30 * time.Second // Default connection timeout. - gDEFAULT_RETRY_INTERVAL = 100 * time.Millisecond // Default retry interval. - gDEFAULT_READ_BUFFER_SIZE = 128 // (Byte) Buffer size for reading. + defaultConnTimeout = 30 * time.Second // Default connection timeout. + defaultRetryInternal = 100 * time.Millisecond // Default retry interval. + defaultReadBufferSize = 128 // (Byte) Buffer size for reading. ) type Retry struct { @@ -29,7 +29,7 @@ type Retry struct { // NewNetConn creates and returns a net.Conn with given address like "127.0.0.1:80". // The optional parameter specifies the timeout for dialing connection. func NewNetConn(addr string, timeout ...time.Duration) (net.Conn, error) { - d := gDEFAULT_CONN_TIMEOUT + d := defaultConnTimeout if len(timeout) > 0 { d = timeout[0] } @@ -40,7 +40,7 @@ func NewNetConn(addr string, timeout ...time.Duration) (net.Conn, error) { // The optional parameter specifies the timeout for dialing connection. func NewNetConnTLS(addr string, tlsConfig *tls.Config, timeout ...time.Duration) (net.Conn, error) { dialer := &net.Dialer{ - Timeout: gDEFAULT_CONN_TIMEOUT, + Timeout: defaultConnTimeout, } if len(timeout) > 0 { dialer.Timeout = timeout[0] diff --git a/net/gtcp/gtcp_pool.go b/net/gtcp/gtcp_pool.go index eda264b38..49a5cf33e 100644 --- a/net/gtcp/gtcp_pool.go +++ b/net/gtcp/gtcp_pool.go @@ -14,19 +14,18 @@ import ( ) // PoolConn is a connection with pool feature for TCP. -// Note that it is NOT a pool or connection manager, -// it is just a TCP connection object. +// Note that it is NOT a pool or connection manager, it is just a TCP connection object. type PoolConn struct { *Conn // Underlying connection object. - pool *gpool.Pool // Connection pool, which is not a really connection pool, but a connection reusable pool. + pool *gpool.Pool // Connection pool, which is not a real connection pool, but a connection reusable pool. status int // Status of current connection, which is used to mark this connection usable or not. } const ( - gDEFAULT_POOL_EXPIRE = 10 * time.Second // Default TTL for connection in the pool. - gCONN_STATUS_UNKNOWN = 0 // Means it is unknown it's connective or not. - gCONN_STATUS_ACTIVE = 1 // Means it is now connective. - gCONN_STATUS_ERROR = 2 // Means it should be closed and removed from pool. + defaultPoolExpire = 10 * time.Second // Default TTL for connection in the pool. + connStatusUnknown = 0 // Means it is unknown it's connective or not. + connStatusActive = 1 // Means it is now connective. + connStatusError = 2 // Means it should be closed and removed from pool. ) var ( @@ -38,9 +37,9 @@ var ( func NewPoolConn(addr string, timeout ...time.Duration) (*PoolConn, error) { v := addressPoolMap.GetOrSetFuncLock(addr, func() interface{} { var pool *gpool.Pool - pool = gpool.New(gDEFAULT_POOL_EXPIRE, func() (interface{}, error) { + pool = gpool.New(defaultPoolExpire, func() (interface{}, error) { if conn, err := NewConn(addr, timeout...); err == nil { - return &PoolConn{conn, pool, gCONN_STATUS_ACTIVE}, nil + return &PoolConn{conn, pool, connStatusActive}, nil } else { return nil, err } @@ -60,8 +59,8 @@ func NewPoolConn(addr string, timeout ...time.Duration) (*PoolConn, error) { // Note that, if calls Close function closing itself, can not // be used again. func (c *PoolConn) Close() error { - if c.pool != nil && c.status == gCONN_STATUS_ACTIVE { - c.status = gCONN_STATUS_UNKNOWN + if c.pool != nil && c.status == connStatusActive { + c.status = connStatusUnknown c.pool.Put(c) } else { return c.Conn.Close() @@ -73,7 +72,7 @@ func (c *PoolConn) Close() error { // writing data. func (c *PoolConn) Send(data []byte, retry ...Retry) error { err := c.Conn.Send(data, retry...) - if err != nil && c.status == gCONN_STATUS_UNKNOWN { + if err != nil && c.status == connStatusUnknown { if v, e := c.pool.Get(); e == nil { c.Conn = v.(*PoolConn).Conn err = c.Send(data, retry...) @@ -82,9 +81,9 @@ func (c *PoolConn) Send(data []byte, retry ...Retry) error { } } if err != nil { - c.status = gCONN_STATUS_ERROR + c.status = connStatusError } else { - c.status = gCONN_STATUS_ACTIVE + c.status = connStatusActive } return err } @@ -93,9 +92,9 @@ func (c *PoolConn) Send(data []byte, retry ...Retry) error { func (c *PoolConn) Recv(length int, retry ...Retry) ([]byte, error) { data, err := c.Conn.Recv(length, retry...) if err != nil { - c.status = gCONN_STATUS_ERROR + c.status = connStatusError } else { - c.status = gCONN_STATUS_ACTIVE + c.status = connStatusActive } return data, err } @@ -105,9 +104,9 @@ func (c *PoolConn) Recv(length int, retry ...Retry) ([]byte, error) { func (c *PoolConn) RecvLine(retry ...Retry) ([]byte, error) { data, err := c.Conn.RecvLine(retry...) if err != nil { - c.status = gCONN_STATUS_ERROR + c.status = connStatusError } else { - c.status = gCONN_STATUS_ACTIVE + c.status = connStatusActive } return data, err } @@ -117,19 +116,19 @@ func (c *PoolConn) RecvLine(retry ...Retry) ([]byte, error) { func (c *PoolConn) RecvTil(til []byte, retry ...Retry) ([]byte, error) { data, err := c.Conn.RecvTil(til, retry...) if err != nil { - c.status = gCONN_STATUS_ERROR + c.status = connStatusError } else { - c.status = gCONN_STATUS_ACTIVE + c.status = connStatusActive } return data, err } // RecvWithTimeout reads data from the connection with timeout. func (c *PoolConn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) (data []byte, err error) { - if err := c.SetRecvDeadline(time.Now().Add(timeout)); err != nil { + if err := c.SetreceiveDeadline(time.Now().Add(timeout)); err != nil { return nil, err } - defer c.SetRecvDeadline(time.Time{}) + defer c.SetreceiveDeadline(time.Time{}) data, err = c.Recv(length, retry...) return } diff --git a/net/gtcp/gtcp_pool_pkg.go b/net/gtcp/gtcp_pool_pkg.go index 7412e829c..ba64af00e 100644 --- a/net/gtcp/gtcp_pool_pkg.go +++ b/net/gtcp/gtcp_pool_pkg.go @@ -13,7 +13,7 @@ import ( // SendPkg sends a package containing to the connection. // The optional parameter