This commit is contained in:
殷勇 2023-10-25 15:46:41 +08:00
parent ef6a02719b
commit 50cb269954
8 changed files with 44 additions and 39 deletions

View File

@ -1,9 +1,9 @@
package q5 package q5
import ( import (
"errors"
"strconv" "strconv"
"strings" "strings"
"errors"
) )
// ToInt converts a value to an integer. // 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)) 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. // ToInt64 converts a value to an int64.
func ToInt64[T string | int | int32 | int64 | float32 | float64](value T) int64 { func ToInt64[T string | int | int32 | int64 | float32 | float64](value T) int64 {
var x interface{} = value var x interface{} = value
@ -80,7 +85,7 @@ func ToString[T string | int | int32 | int64 | float32 | float64](value T) strin
return "" return ""
} }
func ToInt64Ex(x interface{}) (int64, error) { func ToInt64Ex(x interface{}) (int64, error) {
switch i := x.(type) { switch i := x.(type) {
case int: case int:
return int64(i), nil return int64(i), nil
@ -140,7 +145,7 @@ func ToStringEx(x interface{}) (string, error) {
} }
func DuckToSimple[T string | int | int32 | int64 | float32 | float64]( func DuckToSimple[T string | int | int32 | int64 | float32 | float64](
d interface{}, s *T) bool { d interface{}, s *T) bool {
var sx interface{} = s var sx interface{} = s
switch sv := sx.(type) { switch sv := sx.(type) {
case *int: case *int:

View File

@ -1,16 +1,16 @@
package q5 package q5
import ( import (
"errors"
"io/ioutil"
"net/http" "net/http"
net_url "net/url" net_url "net/url"
"io/ioutil"
"errors"
//"strings" //"strings"
) )
const ( const (
HTTP_OPT_ADD_HEADER = iota 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, 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") panic("http.NewRequest error")
} }
if initFunc != nil { 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) internalSetOpt(client, request, opt, key, val)
}) })
} }

View File

@ -1,21 +1,21 @@
package q5 package q5
import ( import (
"fmt"
"database/sql" "database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
) )
type Mysql struct { type Mysql struct {
host string host string
port int32 port int32
user string user string
passwd string passwd string
database 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.host = host
this.port = port this.port = port
this.user = user this.user = user
@ -23,7 +23,7 @@ func (this* Mysql) init(host string, port int32, user string, passwd string, dat
this.database = database this.database = database
} }
func (this* Mysql) Open() error { func (this *Mysql) Open() error {
connStr := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8", connStr := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8",
this.user, this.user,
this.passwd, this.passwd,
@ -35,26 +35,26 @@ func (this* Mysql) Open() error {
return err return err
} }
func (this* Mysql) Close() { func (this *Mysql) Close() {
if this.db != nil { if this.db != nil {
this.db.Close() this.db.Close()
} }
} }
func (this* Mysql) Ping() error { func (this *Mysql) Ping() error {
return this.db.Ping() 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...) rows, err := this.db.Query(query, args...)
return rows, err 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...) 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...) result, err := this.db.Exec(query, args...)
return result, err return result, err
} }

View File

@ -6,7 +6,7 @@ import (
type Queue struct { type Queue struct {
msgMutex sync.Mutex msgMutex sync.Mutex
msgList ListHead msgList ListHead
WorkList ListHead WorkList ListHead
} }

View File

@ -5,20 +5,20 @@ import (
) )
type Redis struct { type Redis struct {
host string host string
port int32 port int32
passwd string 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.host = host
this.port = port this.port = port
this.passwd = passwd this.passwd = passwd
} }
func (this* Redis) Open() { func (this *Redis) Open() {
} }
func (this* Redis) Close() { func (this *Redis) Close() {
} }

View File

@ -1,18 +1,18 @@
package q5 package q5
import ( import (
"io"
"strings"
"crypto/md5" "crypto/md5"
"encoding/base64"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"encoding/base64"
"hash/crc32" "hash/crc32"
"io"
"strings"
) )
const ( const (
JSON_NONE = 0 JSON_NONE = 0
JSON_ARRAY = iota JSON_ARRAY = iota
JSON_OBJECT = iota JSON_OBJECT = iota
) )
@ -82,16 +82,16 @@ func ConvertUpperCamelCase(name string) string {
preIs_ := false preIs_ := false
for i := 0; i < len(name); i++ { for i := 0; i < len(name); i++ {
if i == 0 { if i == 0 {
newName += strings.ToUpper(name[i:i +1]) newName += strings.ToUpper(name[i : i+1])
preIs_ = name[i] == '_' preIs_ = name[i] == '_'
} else { } else {
if preIs_ { if preIs_ {
if name[i] != '_' { if name[i] != '_' {
newName += strings.ToUpper(name[i:i +1]) newName += strings.ToUpper(name[i : i+1])
} }
} else { } else {
if name[i] != '_' { if name[i] != '_' {
newName += name[i:i +1] newName += name[i : i+1]
} }
} }
preIs_ = name[i] == '_' preIs_ = name[i] == '_'

View File

@ -1,6 +1,6 @@
package q5 package q5
type TimerCb func (int32, *Args) type TimerCb func(int32, *Args)
type Args []interface{} type Args []interface{}
type Module interface { type Module interface {

View File

@ -5,7 +5,7 @@ import (
"time" "time"
) )
const Q5_EPOCH = 1419120000000; const Q5_EPOCH = 1419120000000
const MACHINE_ID_BIT_NUM = 12 const MACHINE_ID_BIT_NUM = 12
const SEQUNCE_ID_BIT_NUM = 10 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 const MAX_SEQUNCE_ID = (1 << SEQUNCE_ID_BIT_NUM) - 1
type Uuid struct { type Uuid struct {
machineId int32 machineId int32
sequenceId int32 sequenceId int32
lastGenerateTick int64 lastGenerateTick int64
} }