many lint fixes

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2022-01-10 16:47:37 +03:00
parent 94929878fe
commit 74765b4c5f
23 changed files with 190 additions and 85 deletions

View File

@@ -2,8 +2,12 @@ package buf // import "go.unistack.org/micro/v3/util/buf"
import (
"bytes"
"io"
)
var _ io.Closer = &Buffer{}
// Buffer bytes.Buffer wrapper to satisfie io.Closer interface
type Buffer struct {
*bytes.Buffer
}

View File

@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore
package http

View File

@@ -45,8 +45,7 @@ var methodMap = map[string]methodTyp{
http.MethodTrace: mTRACE,
}
// RegisterMethod adds support for custom HTTP method handlers, available
// via Router#Method and Router#MethodFunc
// RegisterMethod adds support for custom HTTP method handlers
func RegisterMethod(method string) error {
if method == "" {
return nil
@@ -75,13 +74,13 @@ const (
ntCatchAll // /api/v1/*
)
func NewTrie() *Node {
return &Node{}
// NewTrie create new tree
func NewTrie() *Trie {
return &Trie{}
}
type Trie = Node
type Node struct {
// Trie holds nodes for path based tree search
type Trie struct {
// regexp matcher for regexp nodes
rex *regexp.Regexp
@@ -128,7 +127,8 @@ func (s endpoints) Value(method methodTyp) *endpoint {
return mh
}
func (n *Node) Insert(methods []string, pattern string, handler interface{}) error {
// Insert add elemenent to tree
func (n *Trie) Insert(methods []string, pattern string, handler interface{}) error {
var err error
for _, method := range methods {
if err = n.insert(methodMap[method], pattern, handler); err != nil {
@@ -138,8 +138,8 @@ func (n *Node) Insert(methods []string, pattern string, handler interface{}) err
return nil
}
func (n *Node) insert(method methodTyp, pattern string, handler interface{}) error {
var parent *Node
func (n *Trie) insert(method methodTyp, pattern string, handler interface{}) error {
var parent *Trie
search := pattern
for {
@@ -175,8 +175,8 @@ func (n *Node) insert(method methodTyp, pattern string, handler interface{}) err
// No edge, create one
if n == nil {
child := &Node{typ: ntStatic, label: label, tail: segTail, prefix: search}
var hn *Node
child := &Trie{typ: ntStatic, label: label, tail: segTail, prefix: search}
var hn *Trie
hn, err = parent.addChild(child, search)
if err != nil {
return err
@@ -205,7 +205,7 @@ func (n *Node) insert(method methodTyp, pattern string, handler interface{}) err
}
// Split the node
child := &Node{
child := &Trie{
typ: ntStatic,
prefix: search[:commonPrefix],
}
@@ -227,12 +227,12 @@ func (n *Node) insert(method methodTyp, pattern string, handler interface{}) err
}
// Create a new edge for the node
subchild := &Node{
subchild := &Trie{
typ: ntStatic,
label: search[0],
prefix: search,
}
var hn *Node
var hn *Trie
hn, err = child.addChild(subchild, search)
if err != nil {
return err
@@ -245,7 +245,7 @@ func (n *Node) insert(method methodTyp, pattern string, handler interface{}) err
// For a URL router like chi's, we split the static, param, regexp and wildcard segments
// into different nodes. In addition, addChild will recursively call itself until every
// pattern segment is added to the url pattern tree as individual nodes, depending on type.
func (n *Node) addChild(child *Node, prefix string) (*Node, error) {
func (n *Trie) addChild(child *Trie, prefix string) (*Trie, error) {
search := prefix
// handler leaf node added to the tree is the child.
@@ -298,7 +298,7 @@ func (n *Node) addChild(child *Node, prefix string) (*Node, error) {
search = search[segStartIdx:] // advance search position
nn := &Node{
nn := &Trie{
typ: ntStatic,
label: search[0],
prefix: search,
@@ -321,7 +321,7 @@ func (n *Node) addChild(child *Node, prefix string) (*Node, error) {
// add the param edge node
search = search[segStartIdx:]
nn := &Node{
nn := &Trie{
typ: segTyp,
label: search[0],
tail: segTail,
@@ -339,7 +339,7 @@ func (n *Node) addChild(child *Node, prefix string) (*Node, error) {
return hn, nil
}
func (n *Node) replaceChild(label, tail byte, child *Node) error {
func (n *Trie) replaceChild(label, tail byte, child *Trie) error {
for i := 0; i < len(n.children[child.typ]); i++ {
if n.children[child.typ][i].label == label && n.children[child.typ][i].tail == tail {
n.children[child.typ][i] = child
@@ -351,7 +351,7 @@ func (n *Node) replaceChild(label, tail byte, child *Node) error {
return fmt.Errorf("replacing missing child")
}
func (n *Node) getEdge(ntyp nodeTyp, label, tail byte, prefix string) *Node {
func (n *Trie) getEdge(ntyp nodeTyp, label, tail byte, prefix string) *Trie {
nds := n.children[ntyp]
for i := 0; i < len(nds); i++ {
if nds[i].label == label && nds[i].tail == tail {
@@ -364,7 +364,7 @@ func (n *Node) getEdge(ntyp nodeTyp, label, tail byte, prefix string) *Node {
return nil
}
func (n *Node) setEndpoint(method methodTyp, handler interface{}, pattern string) error {
func (n *Trie) setEndpoint(method methodTyp, handler interface{}, pattern string) error {
// Set the handler for the method type on the node
if n.endpoints == nil {
n.endpoints = make(endpoints)
@@ -398,7 +398,8 @@ func (n *Node) setEndpoint(method methodTyp, handler interface{}, pattern string
return nil
}
func (n *Node) Search(method string, path string) (interface{}, map[string]string, bool) {
// Search try to find element in tree with path and method
func (n *Trie) Search(method string, path string) (interface{}, map[string]string, bool) {
params := &routeParams{}
// Find the routing handlers for the path
rn := n.findRoute(params, methodMap[method], path)
@@ -425,7 +426,7 @@ type routeParams struct {
// Recursive edge traversal by checking all nodeTyp groups along the way.
// It's like searching through a multi-dimensional radix trie.
func (n *Node) findRoute(params *routeParams, method methodTyp, path string) *Node {
func (n *Trie) findRoute(params *routeParams, method methodTyp, path string) *Trie {
nn := n
search := path
@@ -435,7 +436,7 @@ func (n *Node) findRoute(params *routeParams, method methodTyp, path string) *No
continue
}
var xn *Node
var xn *Trie
xsearch := search
var label byte
@@ -550,7 +551,7 @@ func (n *Node) findRoute(params *routeParams, method methodTyp, path string) *No
return nil
}
func (n *Node) isLeaf() bool {
func (n *Trie) isLeaf() bool {
return n.endpoints != nil
}
@@ -665,7 +666,7 @@ func longestPrefix(k1, k2 string) int {
return i
}
type nodes []*Node
type nodes []*Trie
// Sort the list of nodes by label
func (ns nodes) Sort() { sort.Sort(ns); ns.tailSort() }
@@ -684,7 +685,7 @@ func (ns nodes) tailSort() {
}
}
func (ns nodes) findEdge(label byte) *Node {
func (ns nodes) findEdge(label byte) *Trie {
num := len(ns)
idx := 0
i, j := 0, num-1

View File

@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore
package pool

View File

@@ -10,16 +10,19 @@ type Rand struct {
buf [8]byte
}
// Int31 function implementation
func (r *Rand) Int31() int32 {
_, _ = crand.Read(r.buf[:4])
return int32(binary.BigEndian.Uint32(r.buf[:4]) & ^uint32(1<<31))
}
// Int function implementation
func (r *Rand) Int() int {
u := uint(r.Int63())
return int(u << 1 >> 1) // clear sign bit if int == int32
}
// Float64 function implementation
func (r *Rand) Float64() float64 {
again:
f := float64(r.Int63()) / (1 << 63)
@@ -29,6 +32,7 @@ again:
return f
}
// Float32 function implementation
func (r *Rand) Float32() float32 {
again:
f := float32(r.Float64())
@@ -38,14 +42,17 @@ again:
return f
}
// Uint32 function implementation
func (r *Rand) Uint32() uint32 {
return uint32(r.Int63() >> 31)
}
// Uint64 function implementation
func (r *Rand) Uint64() uint64 {
return uint64(r.Int63())>>31 | uint64(r.Int63())<<32
}
// Intn function implementation
func (r *Rand) Intn(n int) int {
if n <= 1<<31-1 {
return int(r.Int31n(int32(n)))
@@ -53,12 +60,13 @@ func (r *Rand) Intn(n int) int {
return int(r.Int63n(int64(n)))
}
// Int63 function implementation
func (r *Rand) Int63() int64 {
_, _ = crand.Read(r.buf[:])
return int64(binary.BigEndian.Uint64(r.buf[:]) & ^uint64(1<<63))
}
// Int31n copied from the standard library math/rand implementation of Int31n
// Int31n function implementation copied from the standard library math/rand implementation of Int31n
func (r *Rand) Int31n(n int32) int32 {
if n&(n-1) == 0 { // n is power of two, can mask
return r.Int31() & (n - 1)
@@ -71,7 +79,7 @@ func (r *Rand) Int31n(n int32) int32 {
return v % n
}
// Int63n copied from the standard library math/rand implementation of Int63n
// Int63n function implementation copied from the standard library math/rand implementation of Int63n
func (r *Rand) Int63n(n int64) int64 {
if n&(n-1) == 0 { // n is power of two, can mask
return r.Int63() & (n - 1)
@@ -84,7 +92,7 @@ func (r *Rand) Int63n(n int64) int64 {
return v % n
}
// Shuffle copied from the standard library math/rand implementation of Shuffle
// Shuffle function implementation copied from the standard library math/rand implementation of Shuffle
func (r *Rand) Shuffle(n int, swap func(i, j int)) {
if n < 0 {
panic("invalid argument to Shuffle")

View File

@@ -10,30 +10,40 @@ import (
)
var (
// ErrInvalidStruct happens when passed not struct and not struct pointer
ErrInvalidStruct = errors.New("invalid struct specified")
ErrInvalidValue = errors.New("invalid value specified")
ErrNotFound = errors.New("struct field not found")
// ErrInvalidValue happens when passed invalid value for field
ErrInvalidValue = errors.New("invalid value specified")
// ErrNotFound happens when struct field not found
ErrNotFound = errors.New("struct field not found")
)
// Option func signature
type Option func(*Options)
// Options for merge
type Options struct {
Tags []string
// Tags specifies tags to lookup
Tags []string
// SliceAppend controls slice appending
SliceAppend bool
}
// Tags sets the merge tags for lookup
func Tags(t []string) Option {
return func(o *Options) {
o.Tags = t
}
}
// SliceAppend sets the option
func SliceAppend(b bool) Option {
return func(o *Options) {
o.SliceAppend = b
}
}
// Merge merges map[string]interface{} to destination struct
func Merge(dst interface{}, mp map[string]interface{}, opts ...Option) error {
var err error
var sval reflect.Value
@@ -481,6 +491,7 @@ func IsEmpty(v reflect.Value) bool {
return true
}
// FieldName returns map field name that can be looked up in struct field
func FieldName(name string) string {
newstr := make([]rune, 0, len(name))
for idx, chr := range name {

View File

@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore
package basic