From 50cb2699546045b67366e5ee68821bc0aa9e8484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AE=B7=E5=8B=87?= Date: Wed, 25 Oct 2023 15:46:41 +0800 Subject: [PATCH] save --- convert.go | 11 ++++++++--- httpcli.go | 8 ++++---- mysql.go | 26 +++++++++++++------------- queue.go | 2 +- redis.go | 12 ++++++------ strutils.go | 16 ++++++++-------- types.go | 2 +- uuid.go | 6 +++--- 8 files changed, 44 insertions(+), 39 deletions(-) diff --git a/convert.go b/convert.go index 2ba4136..97e1c2d 100644 --- a/convert.go +++ b/convert.go @@ -1,9 +1,9 @@ package q5 import ( + "errors" "strconv" "strings" - "errors" ) // ToInt converts a value to an integer. @@ -11,6 +11,11 @@ func ToInt32[T string | int | int32 | int64 | float32 | float64](value T) int32 return int32(ToInt64(value)) } +// ToInt converts a value to an integer. +func ToInt[T string | int32 | int64 | float32 | float64](value T) int { + return int(ToInt64(value)) +} + // ToInt64 converts a value to an int64. func ToInt64[T string | int | int32 | int64 | float32 | float64](value T) int64 { var x interface{} = value @@ -80,7 +85,7 @@ func ToString[T string | int | int32 | int64 | float32 | float64](value T) strin return "" } -func ToInt64Ex(x interface{}) (int64, error) { +func ToInt64Ex(x interface{}) (int64, error) { switch i := x.(type) { case int: return int64(i), nil @@ -140,7 +145,7 @@ func ToStringEx(x interface{}) (string, error) { } func DuckToSimple[T string | int | int32 | int64 | float32 | float64]( - d interface{}, s *T) bool { + d interface{}, s *T) bool { var sx interface{} = s switch sv := sx.(type) { case *int: diff --git a/httpcli.go b/httpcli.go index c8cb485..c13ce79 100644 --- a/httpcli.go +++ b/httpcli.go @@ -1,16 +1,16 @@ package q5 import ( + "errors" + "io/ioutil" "net/http" net_url "net/url" - "io/ioutil" - "errors" //"strings" ) const ( HTTP_OPT_ADD_HEADER = iota - HTTP_OPT_SET_PROXY = iota + HTTP_OPT_SET_PROXY = iota ) func internalSetOpt(client *http.Client, request *http.Request, opt int32, @@ -45,7 +45,7 @@ func HttpGetEx(url string, params map[string]string, panic("http.NewRequest error") } if initFunc != nil { - initFunc(func (opt int32, key string, val string) { + initFunc(func(opt int32, key string, val string) { internalSetOpt(client, request, opt, key, val) }) } diff --git a/mysql.go b/mysql.go index 0146b73..2a868cb 100644 --- a/mysql.go +++ b/mysql.go @@ -1,21 +1,21 @@ package q5 import ( - "fmt" "database/sql" + "fmt" _ "github.com/go-sql-driver/mysql" ) type Mysql struct { - host string - port int32 - user string - passwd string + host string + port int32 + user string + passwd string database string - db *sql.DB + db *sql.DB } -func (this* Mysql) init(host string, port int32, user string, passwd string, database string) { +func (this *Mysql) init(host string, port int32, user string, passwd string, database string) { this.host = host this.port = port this.user = user @@ -23,7 +23,7 @@ func (this* Mysql) init(host string, port int32, user string, passwd string, dat this.database = database } -func (this* Mysql) Open() error { +func (this *Mysql) Open() error { connStr := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8", this.user, this.passwd, @@ -35,26 +35,26 @@ func (this* Mysql) Open() error { return err } -func (this* Mysql) Close() { +func (this *Mysql) Close() { if this.db != nil { this.db.Close() } } -func (this* Mysql) Ping() error { +func (this *Mysql) Ping() error { return this.db.Ping() } -func (this* Mysql) Query(query string, args ...interface{}) (*sql.Rows, error) { +func (this *Mysql) Query(query string, args ...interface{}) (*sql.Rows, error) { rows, err := this.db.Query(query, args...) return rows, err } -func (this* Mysql) QueryRow(query string, args ...interface{}) *sql.Row { +func (this *Mysql) QueryRow(query string, args ...interface{}) *sql.Row { return this.db.QueryRow(query, args...) } -func (this* Mysql) Exec(query string, args ...interface{}) (sql.Result, error) { +func (this *Mysql) Exec(query string, args ...interface{}) (sql.Result, error) { result, err := this.db.Exec(query, args...) return result, err } diff --git a/queue.go b/queue.go index 02249da..80b9e63 100644 --- a/queue.go +++ b/queue.go @@ -6,7 +6,7 @@ import ( type Queue struct { msgMutex sync.Mutex - msgList ListHead + msgList ListHead WorkList ListHead } diff --git a/redis.go b/redis.go index 6d8f983..68020bf 100644 --- a/redis.go +++ b/redis.go @@ -5,20 +5,20 @@ import ( ) type Redis struct { - host string - port int32 + host string + port int32 passwd string - conn *redis.Conn + conn *redis.Conn } -func (this* Redis) Init(host string, port int32, passwd string) { +func (this *Redis) Init(host string, port int32, passwd string) { this.host = host this.port = port this.passwd = passwd } -func (this* Redis) Open() { +func (this *Redis) Open() { } -func (this* Redis) Close() { +func (this *Redis) Close() { } diff --git a/strutils.go b/strutils.go index d22c7e3..26525f0 100644 --- a/strutils.go +++ b/strutils.go @@ -1,18 +1,18 @@ package q5 import ( - "io" - "strings" "crypto/md5" + "encoding/base64" "encoding/hex" "encoding/json" - "encoding/base64" "hash/crc32" + "io" + "strings" ) const ( - JSON_NONE = 0 - JSON_ARRAY = iota + JSON_NONE = 0 + JSON_ARRAY = iota JSON_OBJECT = iota ) @@ -82,16 +82,16 @@ func ConvertUpperCamelCase(name string) string { preIs_ := false for i := 0; i < len(name); i++ { if i == 0 { - newName += strings.ToUpper(name[i:i +1]) + newName += strings.ToUpper(name[i : i+1]) preIs_ = name[i] == '_' } else { if preIs_ { if name[i] != '_' { - newName += strings.ToUpper(name[i:i +1]) + newName += strings.ToUpper(name[i : i+1]) } } else { if name[i] != '_' { - newName += name[i:i +1] + newName += name[i : i+1] } } preIs_ = name[i] == '_' diff --git a/types.go b/types.go index 797134a..5c6b118 100644 --- a/types.go +++ b/types.go @@ -1,6 +1,6 @@ package q5 -type TimerCb func (int32, *Args) +type TimerCb func(int32, *Args) type Args []interface{} type Module interface { diff --git a/uuid.go b/uuid.go index d1ecc76..76bdfcb 100644 --- a/uuid.go +++ b/uuid.go @@ -5,7 +5,7 @@ import ( "time" ) -const Q5_EPOCH = 1419120000000; +const Q5_EPOCH = 1419120000000 const MACHINE_ID_BIT_NUM = 12 const SEQUNCE_ID_BIT_NUM = 10 @@ -13,8 +13,8 @@ const MAX_MACHINE_ID = (1 << MACHINE_ID_BIT_NUM) - 1 const MAX_SEQUNCE_ID = (1 << SEQUNCE_ID_BIT_NUM) - 1 type Uuid struct { - machineId int32 - sequenceId int32 + machineId int32 + sequenceId int32 lastGenerateTick int64 }