mirror of
https://github.com/duanhf2012/origin.git
synced 2026-04-13 20:47:28 +08:00
优化代码规范
This commit is contained in:
@@ -58,7 +58,7 @@ func (ft *FrameTimer) addTimerData(timerID FrameTimerID, frameNum FrameNumber, t
|
||||
td.cb = cb
|
||||
td.idx = -1
|
||||
td.tickerFrameNum = tickerFrameNum
|
||||
|
||||
td.ctx = ctx
|
||||
ft.mapTimer[timerID] = td
|
||||
return td
|
||||
}
|
||||
|
||||
@@ -6,11 +6,11 @@ import (
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
"io"
|
||||
)
|
||||
|
||||
type IGinProcessor interface {
|
||||
@@ -23,12 +23,12 @@ type GinModule struct {
|
||||
*gin.Engine
|
||||
srv *http.Server
|
||||
|
||||
listenAddr string
|
||||
listenAddr string
|
||||
handleTimeout time.Duration
|
||||
processor []IGinProcessor
|
||||
processor []IGinProcessor
|
||||
}
|
||||
|
||||
func (gm *GinModule) Init(addr string, handleTimeout time.Duration,engine *gin.Engine) {
|
||||
func (gm *GinModule) Init(addr string, handleTimeout time.Duration, engine *gin.Engine) {
|
||||
gm.listenAddr = addr
|
||||
gm.handleTimeout = handleTimeout
|
||||
gm.Engine = engine
|
||||
@@ -96,91 +96,91 @@ func (gm *GinModule) Stop(ctx context.Context) {
|
||||
|
||||
type SafeContext struct {
|
||||
*gin.Context
|
||||
chanWait chan struct{}
|
||||
chanWait chan struct{}
|
||||
}
|
||||
|
||||
func (c *SafeContext) JSONAndDone(code int, obj any) {
|
||||
c.Context.JSON(code,obj)
|
||||
c.Context.JSON(code, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) AsciiJSONAndDone(code int, obj any){
|
||||
c.Context.AsciiJSON(code,obj)
|
||||
func (c *SafeContext) AsciiJSONAndDone(code int, obj any) {
|
||||
c.Context.AsciiJSON(code, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) PureJSONAndDone(code int, obj any){
|
||||
c.Context.PureJSON(code,obj)
|
||||
func (c *SafeContext) PureJSONAndDone(code int, obj any) {
|
||||
c.Context.PureJSON(code, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) XMLAndDone(code int, obj any){
|
||||
c.Context.XML(code,obj)
|
||||
func (c *SafeContext) XMLAndDone(code int, obj any) {
|
||||
c.Context.XML(code, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) YAMLAndDone(code int, obj any){
|
||||
c.Context.YAML(code,obj)
|
||||
func (c *SafeContext) YAMLAndDone(code int, obj any) {
|
||||
c.Context.YAML(code, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) TOMLAndDone(code int, obj any){
|
||||
c.Context.TOML(code,obj)
|
||||
func (c *SafeContext) TOMLAndDone(code int, obj any) {
|
||||
c.Context.TOML(code, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) ProtoBufAndDone(code int, obj any){
|
||||
c.Context.ProtoBuf(code,obj)
|
||||
func (c *SafeContext) ProtoBufAndDone(code int, obj any) {
|
||||
c.Context.ProtoBuf(code, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) StringAndDone(code int, format string, values ...any){
|
||||
c.Context.String(code,format,values...)
|
||||
func (c *SafeContext) StringAndDone(code int, format string, values ...any) {
|
||||
c.Context.String(code, format, values...)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) RedirectAndDone(code int, location string){
|
||||
c.Context.Redirect(code,location)
|
||||
func (c *SafeContext) RedirectAndDone(code int, location string) {
|
||||
c.Context.Redirect(code, location)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) DataAndDone(code int, contentType string, data []byte){
|
||||
c.Context.Data(code,contentType,data)
|
||||
func (c *SafeContext) DataAndDone(code int, contentType string, data []byte) {
|
||||
c.Context.Data(code, contentType, data)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) DataFromReaderAndDone(code int, contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string){
|
||||
c.DataFromReader(code,contentLength,contentType,reader,extraHeaders)
|
||||
func (c *SafeContext) DataFromReaderAndDone(code int, contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string) {
|
||||
c.DataFromReader(code, contentLength, contentType, reader, extraHeaders)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) HTMLAndDone(code int, name string, obj any){
|
||||
c.Context.HTML(code,name,obj)
|
||||
func (c *SafeContext) HTMLAndDone(code int, name string, obj any) {
|
||||
c.Context.HTML(code, name, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) IndentedJSONAndDone(code int, obj any){
|
||||
c.Context.IndentedJSON(code,obj)
|
||||
func (c *SafeContext) IndentedJSONAndDone(code int, obj any) {
|
||||
c.Context.IndentedJSON(code, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) SecureJSONAndDone(code int, obj any){
|
||||
c.Context.SecureJSON(code,obj)
|
||||
func (c *SafeContext) SecureJSONAndDone(code int, obj any) {
|
||||
c.Context.SecureJSON(code, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) JSONPAndDone(code int, obj any){
|
||||
c.Context.JSONP(code,obj)
|
||||
func (c *SafeContext) JSONPAndDone(code int, obj any) {
|
||||
c.Context.JSONP(code, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) Done(){
|
||||
func (c *SafeContext) Done() {
|
||||
c.chanWait <- struct{}{}
|
||||
}
|
||||
|
||||
type GinEvent struct {
|
||||
handlersChain []SafeHandlerFunc
|
||||
c SafeContext
|
||||
c SafeContext
|
||||
}
|
||||
|
||||
type SafeHandlerFunc func(*SafeContext)
|
||||
@@ -199,16 +199,16 @@ func (gm *GinModule) handleMethod(httpMethod, relativePath string, handlers ...S
|
||||
}
|
||||
|
||||
var ev GinEvent
|
||||
chanWait := make(chan struct{},2)
|
||||
chanWait := make(chan struct{}, 2)
|
||||
ev.c.chanWait = chanWait
|
||||
ev.handlersChain = handlers
|
||||
ev.c.Context = c
|
||||
gm.NotifyEvent(&ev)
|
||||
|
||||
ctx,cancel := context.WithTimeout(context.Background(), gm.handleTimeout)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), gm.handleTimeout)
|
||||
defer cancel()
|
||||
|
||||
select{
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Error("GinModule process timeout", slog.Any("path", c.Request.URL.Path))
|
||||
c.AbortWithStatus(http.StatusRequestTimeout)
|
||||
|
||||
@@ -39,19 +39,19 @@ func (slf *SyncHttpResponse) Get(timeoutMs int) HttpResponse {
|
||||
return rsp
|
||||
}
|
||||
return HttpResponse{
|
||||
Err: fmt.Errorf("Getting the return result timeout [%d]ms", timeoutMs),
|
||||
Err: fmt.Errorf("getting the return result timeout [%d]ms", timeoutMs),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *HttpClientModule) InitHttpClient(transport http.RoundTripper,timeout time.Duration,checkRedirect func(req *http.Request, via []*http.Request) error){
|
||||
func (m *HttpClientModule) InitHttpClient(transport http.RoundTripper, timeout time.Duration, checkRedirect func(req *http.Request, via []*http.Request) error) {
|
||||
m.client = &http.Client{
|
||||
Transport: transport,
|
||||
Timeout: timeout,
|
||||
Transport: transport,
|
||||
Timeout: timeout,
|
||||
CheckRedirect: checkRedirect,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *HttpClientModule) Init(proxyUrl string, maxpool int, dialTimeout time.Duration,dialKeepAlive time.Duration,idleConnTimeout time.Duration,timeout time.Duration) {
|
||||
func (m *HttpClientModule) Init(proxyUrl string, maxPool int, dialTimeout time.Duration, dialKeepAlive time.Duration, idleConnTimeout time.Duration, timeout time.Duration) {
|
||||
type ProxyFun func(_ *http.Request) (*url.URL, error)
|
||||
var proxyFun ProxyFun
|
||||
if proxyUrl != "" {
|
||||
@@ -66,8 +66,8 @@ func (m *HttpClientModule) Init(proxyUrl string, maxpool int, dialTimeout time.D
|
||||
Timeout: dialTimeout,
|
||||
KeepAlive: dialKeepAlive,
|
||||
}).DialContext,
|
||||
MaxIdleConns: maxpool,
|
||||
MaxIdleConnsPerHost: maxpool,
|
||||
MaxIdleConns: maxPool,
|
||||
MaxIdleConnsPerHost: maxPool,
|
||||
IdleConnTimeout: idleConnTimeout,
|
||||
Proxy: proxyFun,
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
|
||||
@@ -75,12 +75,12 @@ func (s *Session) NextSeq(db string, collection string, id interface{}) (int, er
|
||||
return res.Seq, err
|
||||
}
|
||||
|
||||
// indexKeys[索引][每个索引key字段]
|
||||
// EnsureIndex indexKeys[索引][每个索引key字段]
|
||||
func (s *Session) EnsureIndex(db string, collection string, indexKeys [][]string, bBackground bool, sparse bool, asc bool) error {
|
||||
return s.ensureIndex(db, collection, indexKeys, bBackground, false, sparse, asc)
|
||||
}
|
||||
|
||||
// indexKeys[索引][每个索引key字段]
|
||||
// EnsureUniqueIndex indexKeys[索引][每个索引key字段]
|
||||
func (s *Session) EnsureUniqueIndex(db string, collection string, indexKeys [][]string, bBackground bool, sparse bool, asc bool) error {
|
||||
return s.ensureIndex(db, collection, indexKeys, bBackground, true, sparse, asc)
|
||||
}
|
||||
@@ -92,9 +92,9 @@ func (s *Session) ensureIndex(db string, collection string, indexKeys [][]string
|
||||
keysDoc := bson.D{}
|
||||
for _, key := range keys {
|
||||
if asc {
|
||||
keysDoc = append(keysDoc, bson.E{Key:key,Value:1})
|
||||
keysDoc = append(keysDoc, bson.E{Key: key, Value: 1})
|
||||
} else {
|
||||
keysDoc = append(keysDoc, bson.E{Key:key,Value:-1})
|
||||
keysDoc = append(keysDoc, bson.E{Key: key, Value: -1})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,8 +19,8 @@ import (
|
||||
type SyncFun func()
|
||||
|
||||
type DBExecute struct {
|
||||
syncExecuteFun chan SyncFun
|
||||
syncExecuteExit chan bool
|
||||
syncExecuteFun chan SyncFun
|
||||
syncExecuteExit chan bool
|
||||
}
|
||||
|
||||
type PingExecute struct {
|
||||
@@ -28,22 +28,21 @@ type PingExecute struct {
|
||||
pintExit chan bool
|
||||
}
|
||||
|
||||
// DBModule ...
|
||||
type MySQLModule struct {
|
||||
service.Module
|
||||
db *sql.DB
|
||||
url string
|
||||
username string
|
||||
password string
|
||||
dbname string
|
||||
slowDuration time.Duration
|
||||
pingCoroutine PingExecute
|
||||
waitGroup sync.WaitGroup
|
||||
db *sql.DB
|
||||
url string
|
||||
username string
|
||||
password string
|
||||
dbname string
|
||||
slowDuration time.Duration
|
||||
pingCoroutine PingExecute
|
||||
waitGroup sync.WaitGroup
|
||||
}
|
||||
|
||||
// Tx ...
|
||||
type Tx struct {
|
||||
tx *sql.Tx
|
||||
tx *sql.Tx
|
||||
slowDuration time.Duration
|
||||
}
|
||||
|
||||
@@ -53,7 +52,7 @@ type DBResult struct {
|
||||
RowsAffected int64
|
||||
|
||||
rowNum int
|
||||
RowInfo map[string][]interface{} //map[fieldname][row]sql.NullString
|
||||
RowInfo map[string][]interface{} //map[fieldName][row]sql.NullString
|
||||
}
|
||||
|
||||
type DataSetList struct {
|
||||
@@ -63,19 +62,17 @@ type DataSetList struct {
|
||||
blur bool
|
||||
}
|
||||
|
||||
|
||||
type dbControl interface {
|
||||
Exec(query string, args ...interface{}) (sql.Result, error)
|
||||
Query(query string, args ...interface{}) (*sql.Rows, error)
|
||||
}
|
||||
|
||||
|
||||
func (m *MySQLModule) Init( url string, userName string, password string, dbname string,maxConn int) error {
|
||||
func (m *MySQLModule) Init(url string, userName string, password string, dbname string, maxConn int) error {
|
||||
m.url = url
|
||||
m.username = userName
|
||||
m.password = password
|
||||
m.dbname = dbname
|
||||
m.pingCoroutine = PingExecute{tickerPing : time.NewTicker(5*time.Second), pintExit : make(chan bool, 1)}
|
||||
m.pingCoroutine = PingExecute{tickerPing: time.NewTicker(5 * time.Second), pintExit: make(chan bool, 1)}
|
||||
|
||||
return m.connect(maxConn)
|
||||
}
|
||||
@@ -85,12 +82,12 @@ func (m *MySQLModule) SetQuerySlowTime(slowDuration time.Duration) {
|
||||
}
|
||||
|
||||
func (m *MySQLModule) Query(strQuery string, args ...interface{}) (*DataSetList, error) {
|
||||
return query(m.slowDuration, m.db,strQuery,args...)
|
||||
return query(m.slowDuration, m.db, strQuery, args...)
|
||||
}
|
||||
|
||||
// Exec ...
|
||||
func (m *MySQLModule) Exec(strSql string, args ...interface{}) (*DBResult, error) {
|
||||
return exec(m.slowDuration, m.db,strSql,args...)
|
||||
return exec(m.slowDuration, m.db, strSql, args...)
|
||||
}
|
||||
|
||||
// Begin starts a transaction.
|
||||
@@ -116,14 +113,14 @@ func (slf *Tx) Commit() error {
|
||||
return slf.tx.Commit()
|
||||
}
|
||||
|
||||
// QueryEx executes a query that return rows.
|
||||
// Query executes a query that return rows.
|
||||
func (slf *Tx) Query(strQuery string, args ...interface{}) (*DataSetList, error) {
|
||||
return query(slf.slowDuration,slf.tx,strQuery,args...)
|
||||
return query(slf.slowDuration, slf.tx, strQuery, args...)
|
||||
}
|
||||
|
||||
// Exec executes a query that doesn't return rows.
|
||||
func (slf *Tx) Exec(strSql string, args ...interface{}) (*DBResult, error) {
|
||||
return exec(slf.slowDuration,slf.tx,strSql,args...)
|
||||
return exec(slf.slowDuration, slf.tx, strSql, args...)
|
||||
}
|
||||
|
||||
// Connect ...
|
||||
@@ -168,7 +165,7 @@ func (m *MySQLModule) runPing() {
|
||||
}
|
||||
}
|
||||
|
||||
func checkArgs(args ...interface{}) error {
|
||||
func checkArgs(args ...interface{}) error {
|
||||
for _, val := range args {
|
||||
if reflect.TypeOf(val).Kind() == reflect.String {
|
||||
retVal := val.(string)
|
||||
@@ -211,33 +208,33 @@ func checkArgs(args ...interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkSlow(slowDuration time.Duration,Time time.Duration) bool {
|
||||
if slowDuration != 0 && Time >=slowDuration {
|
||||
func checkSlow(slowDuration time.Duration, Time time.Duration) bool {
|
||||
if slowDuration != 0 && Time >= slowDuration {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func query(slowDuration time.Duration,db dbControl,strQuery string, args ...interface{}) (*DataSetList, error) {
|
||||
func query(slowDuration time.Duration, db dbControl, strQuery string, args ...interface{}) (*DataSetList, error) {
|
||||
datasetList := DataSetList{}
|
||||
datasetList.tag = "json"
|
||||
datasetList.blur = true
|
||||
|
||||
if checkArgs(args) != nil {
|
||||
log.Error("CheckArgs is error :%s", strQuery)
|
||||
return &datasetList, fmt.Errorf("CheckArgs is error!")
|
||||
return &datasetList, fmt.Errorf("checkArgs is error")
|
||||
}
|
||||
|
||||
if db == nil {
|
||||
log.Error("cannot connect database:%s", strQuery)
|
||||
return &datasetList, fmt.Errorf("cannot connect database!")
|
||||
return &datasetList, fmt.Errorf("cannot connect database")
|
||||
}
|
||||
|
||||
TimeFuncStart := time.Now()
|
||||
rows, err := db.Query(strQuery, args...)
|
||||
timeFuncPass := time.Since(TimeFuncStart)
|
||||
|
||||
if checkSlow(slowDuration,timeFuncPass) {
|
||||
if checkSlow(slowDuration, timeFuncPass) {
|
||||
log.Error("DBModule QueryEx Time %s , Query :%s , args :%+v", timeFuncPass, strQuery, args)
|
||||
}
|
||||
if err != nil {
|
||||
@@ -256,7 +253,7 @@ func query(slowDuration time.Duration,db dbControl,strQuery string, args ...inte
|
||||
if dbResult.RowInfo == nil {
|
||||
dbResult.RowInfo = make(map[string][]interface{})
|
||||
}
|
||||
//RowInfo map[string][][]sql.NullString //map[fieldname][row][column]sql.NullString
|
||||
|
||||
colField, err := rows.Columns()
|
||||
if err != nil {
|
||||
return &datasetList, err
|
||||
@@ -268,10 +265,10 @@ func query(slowDuration time.Duration,db dbControl,strQuery string, args ...inte
|
||||
}
|
||||
rows.Scan(valuePtrs...)
|
||||
|
||||
for idx, fieldname := range colField {
|
||||
fieldRowData := dbResult.RowInfo[strings.ToLower(fieldname)]
|
||||
for idx, fieldName := range colField {
|
||||
fieldRowData := dbResult.RowInfo[strings.ToLower(fieldName)]
|
||||
fieldRowData = append(fieldRowData, valuePtrs[idx])
|
||||
dbResult.RowInfo[strings.ToLower(fieldname)] = fieldRowData
|
||||
dbResult.RowInfo[strings.ToLower(fieldName)] = fieldRowData
|
||||
}
|
||||
dbResult.rowNum += 1
|
||||
}
|
||||
@@ -282,7 +279,7 @@ func query(slowDuration time.Duration,db dbControl,strQuery string, args ...inte
|
||||
|
||||
if hasRet == false {
|
||||
if rows.Err() != nil {
|
||||
log.Error( "Query:%s(%+v)", strQuery, rows)
|
||||
log.Error("Query:%s(%+v)", strQuery, rows)
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -291,22 +288,22 @@ func query(slowDuration time.Duration,db dbControl,strQuery string, args ...inte
|
||||
return &datasetList, nil
|
||||
}
|
||||
|
||||
func exec(slowDuration time.Duration,db dbControl,strSql string, args ...interface{}) (*DBResult, error) {
|
||||
func exec(slowDuration time.Duration, db dbControl, strSql string, args ...interface{}) (*DBResult, error) {
|
||||
ret := &DBResult{}
|
||||
if db == nil {
|
||||
log.Error("cannot connect database:%s", strSql)
|
||||
return ret, fmt.Errorf("cannot connect database!")
|
||||
return ret, fmt.Errorf("cannot connect database")
|
||||
}
|
||||
|
||||
if checkArgs(args) != nil {
|
||||
log.Error("CheckArgs is error :%s", strSql)
|
||||
return ret, fmt.Errorf("CheckArgs is error!")
|
||||
return ret, fmt.Errorf("checkArgs is error")
|
||||
}
|
||||
|
||||
TimeFuncStart := time.Now()
|
||||
res, err := db.Exec(strSql, args...)
|
||||
timeFuncPass := time.Since(TimeFuncStart)
|
||||
if checkSlow(slowDuration,timeFuncPass) {
|
||||
if checkSlow(slowDuration, timeFuncPass) {
|
||||
log.Error("DBModule QueryEx Time %s , Query :%s , args :%+v", timeFuncPass, strSql, args)
|
||||
}
|
||||
if err != nil {
|
||||
@@ -361,21 +358,21 @@ func (ds *DataSetList) slice2interface(in interface{}) error {
|
||||
}
|
||||
|
||||
v := reflect.ValueOf(in).Elem()
|
||||
newv := reflect.MakeSlice(v.Type(), 0, length)
|
||||
v.Set(newv)
|
||||
newV := reflect.MakeSlice(v.Type(), 0, length)
|
||||
v.Set(newV)
|
||||
v.SetLen(length)
|
||||
|
||||
for i := 0; i < length; i++ {
|
||||
idxv := v.Index(i)
|
||||
if idxv.Kind() == reflect.Ptr {
|
||||
newObj := reflect.New(idxv.Type().Elem())
|
||||
idxV := v.Index(i)
|
||||
if idxV.Kind() == reflect.Ptr {
|
||||
newObj := reflect.New(idxV.Type().Elem())
|
||||
v.Index(i).Set(newObj)
|
||||
idxv = newObj
|
||||
idxV = newObj
|
||||
} else {
|
||||
idxv = idxv.Addr()
|
||||
idxV = idxV.Addr()
|
||||
}
|
||||
|
||||
err := ds.rowData2interface(i, ds.dataSetList[ds.currentDataSetIdx].RowInfo, idxv)
|
||||
err := ds.rowData2interface(i, ds.dataSetList[ds.currentDataSetIdx].RowInfo, idxV)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -390,7 +387,7 @@ func (ds *DataSetList) rowData2interface(rowIdx int, m map[string][]interface{},
|
||||
typ := t.Elem()
|
||||
|
||||
if !val.IsValid() {
|
||||
return errors.New("Incorrect data type!")
|
||||
return errors.New("incorrect data type")
|
||||
}
|
||||
|
||||
for i := 0; i < val.NumField(); i++ {
|
||||
@@ -402,11 +399,11 @@ func (ds *DataSetList) rowData2interface(rowIdx int, m map[string][]interface{},
|
||||
}
|
||||
|
||||
if tag != "" && tag != "-" {
|
||||
vtag := strings.ToLower(tag)
|
||||
columnData, ok := m[vtag]
|
||||
vTag := strings.ToLower(tag)
|
||||
columnData, ok := m[vTag]
|
||||
if ok == false {
|
||||
if !ds.blur {
|
||||
return fmt.Errorf("Cannot find filed name %s!", vtag)
|
||||
return fmt.Errorf("cannot find filed name %s", vTag)
|
||||
}
|
||||
continue
|
||||
}
|
||||
@@ -416,12 +413,12 @@ func (ds *DataSetList) rowData2interface(rowIdx int, m map[string][]interface{},
|
||||
meta := columnData[rowIdx].(*sql.NullString)
|
||||
if !ok {
|
||||
if !ds.blur {
|
||||
return fmt.Errorf("No corresponding field was found in the result set %s!", tag)
|
||||
return fmt.Errorf("no corresponding field was found in the result set %s", tag)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if !value.CanSet() {
|
||||
return errors.New("Struct fields do not have read or write permissions!")
|
||||
return errors.New("struct fields do not have read or write permissions")
|
||||
}
|
||||
|
||||
if meta.Valid == false {
|
||||
@@ -460,7 +457,7 @@ func (ds *DataSetList) rowData2interface(rowIdx int, m map[string][]interface{},
|
||||
}
|
||||
value.SetBool(b)
|
||||
default:
|
||||
return errors.New("The database map has unrecognized data types!")
|
||||
return errors.New("the database map has unrecognized data types")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,18 +90,18 @@ func (m *RedisModule) Init(redisCfg *ConfigRedis) {
|
||||
func (m *RedisModule) getConn() (redis.Conn, error) {
|
||||
if m.redisPool == nil {
|
||||
log.Error("Not Init RedisModule")
|
||||
return nil, fmt.Errorf("Not Init RedisModule")
|
||||
return nil, fmt.Errorf("not Init RedisModule")
|
||||
}
|
||||
conn := m.redisPool.Get()
|
||||
if conn == nil {
|
||||
log.Error("Cannot get connection")
|
||||
return nil, fmt.Errorf("Cannot get connection")
|
||||
return nil, fmt.Errorf("cannot get connection")
|
||||
}
|
||||
|
||||
if conn.Err() != nil {
|
||||
err := conn.Err()
|
||||
if err != nil {
|
||||
log.Error("Get Conn have error,reason:%v", err)
|
||||
log.Error("get Conn have error,reason:%v", err)
|
||||
}
|
||||
conn.Close()
|
||||
return nil, err
|
||||
@@ -153,7 +153,7 @@ func (m *RedisModule) SetStringJSONExpire(key interface{}, val interface{}, expi
|
||||
|
||||
func (m *RedisModule) setStringByExpire(key, value, expire interface{}) error {
|
||||
if key == "" {
|
||||
return errors.New("Key Is Empty")
|
||||
return errors.New("key is empty")
|
||||
}
|
||||
|
||||
conn, err := m.getConn()
|
||||
@@ -474,7 +474,7 @@ func (m *RedisModule) DelStringKeyList(keys []interface{}) (map[interface{}]bool
|
||||
|
||||
func (m *RedisModule) SetHash(redisKey, hashKey, value interface{}) error {
|
||||
if redisKey == "" || hashKey == "" {
|
||||
return errors.New("Key Is Empty")
|
||||
return errors.New("key is empty")
|
||||
}
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -490,10 +490,9 @@ func (m *RedisModule) SetHash(redisKey, hashKey, value interface{}) error {
|
||||
return retErr
|
||||
}
|
||||
|
||||
// GetRedisAllHashJSON ...
|
||||
func (m *RedisModule) GetAllHashJSON(redisKey string) (map[string]string, error) {
|
||||
if redisKey == "" {
|
||||
return nil, errors.New("Key Is Empty")
|
||||
return nil, errors.New("key is empty")
|
||||
}
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -513,7 +512,7 @@ func (m *RedisModule) GetAllHashJSON(redisKey string) (map[string]string, error)
|
||||
func (m *RedisModule) GetHash(redisKey interface{}, fieldKey interface{}) (string, error) {
|
||||
if redisKey == "" || fieldKey == "" {
|
||||
log.Error("GetHashValueByKey key is empty!")
|
||||
return "", errors.New("Key Is Empty")
|
||||
return "", errors.New("key is empty")
|
||||
}
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -527,7 +526,7 @@ func (m *RedisModule) GetHash(redisKey interface{}, fieldKey interface{}) (strin
|
||||
return "", err
|
||||
}
|
||||
if value == nil {
|
||||
return "", errors.New("Reids Get Hash nil")
|
||||
return "", errors.New("redis get hash nil")
|
||||
}
|
||||
|
||||
return redis.String(value, nil)
|
||||
@@ -536,7 +535,7 @@ func (m *RedisModule) GetHash(redisKey interface{}, fieldKey interface{}) (strin
|
||||
func (m *RedisModule) GetMuchHash(args ...interface{}) ([]string, error) {
|
||||
if len(args) < 2 {
|
||||
log.Error("GetHashValueByHashKeyList key len less than two!")
|
||||
return nil, errors.New("Key Is Empty")
|
||||
return nil, errors.New("key is empty")
|
||||
}
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -550,11 +549,11 @@ func (m *RedisModule) GetMuchHash(args ...interface{}) ([]string, error) {
|
||||
return nil, err
|
||||
}
|
||||
if value == nil {
|
||||
return nil, errors.New("Reids Get Hash nil")
|
||||
return nil, errors.New("redis get hash nil")
|
||||
}
|
||||
|
||||
valueList := value.([]interface{})
|
||||
retList := []string{}
|
||||
var retList []string
|
||||
for _, valueItem := range valueList {
|
||||
valueByte, ok := valueItem.([]byte)
|
||||
if !ok {
|
||||
@@ -572,7 +571,7 @@ func (m *RedisModule) ScanMatchKeys(cursorValue int, redisKey string, count int)
|
||||
nextCursorValue := 0
|
||||
if redisKey == "" {
|
||||
log.Error("ScanMatchKeys key is empty!")
|
||||
return nextCursorValue, nil, errors.New("Key Is Empty")
|
||||
return nextCursorValue, nil, errors.New("key is empty")
|
||||
}
|
||||
|
||||
conn, err := m.getConn()
|
||||
@@ -587,7 +586,7 @@ func (m *RedisModule) ScanMatchKeys(cursorValue int, redisKey string, count int)
|
||||
return nextCursorValue, nil, err
|
||||
}
|
||||
if value == nil {
|
||||
return nextCursorValue, nil, errors.New("Reids Get Hash nil")
|
||||
return nextCursorValue, nil, errors.New("redis get hash nil")
|
||||
}
|
||||
|
||||
valueList := value.([]interface{})
|
||||
@@ -669,7 +668,7 @@ func (m *RedisModule) RPushListJSON(key interface{}, value ...interface{}) error
|
||||
// LPUSH和RPUSH
|
||||
func (m *RedisModule) setListPush(setType string, args ...interface{}) error {
|
||||
if setType != "LPUSH" && setType != "RPUSH" {
|
||||
return errors.New("Redis List Push Type Error,Must Be LPUSH or RPUSH")
|
||||
return errors.New("redis list push type error,must be LPUSH or RPUSH")
|
||||
}
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -697,7 +696,6 @@ func (m *RedisModule) setListJSONPush(setType string, key interface{}, value ...
|
||||
return m.setListPush(setType, args...)
|
||||
}
|
||||
|
||||
// Lrange ...
|
||||
func (m *RedisModule) LRangeList(key string, start, end int) ([]string, error) {
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -714,7 +712,7 @@ func (m *RedisModule) LRangeList(key string, start, end int) ([]string, error) {
|
||||
return redis.Strings(reply, err)
|
||||
}
|
||||
|
||||
// 获取List的长度
|
||||
// GetListLen 获取List的长度
|
||||
func (m *RedisModule) GetListLen(key string) (int, error) {
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -730,7 +728,7 @@ func (m *RedisModule) GetListLen(key string) (int, error) {
|
||||
return redis.Int(reply, err)
|
||||
}
|
||||
|
||||
// 弹出List最后条记录
|
||||
// RPOPListValue 弹出List最后条记录
|
||||
func (m *RedisModule) RPOPListValue(key string) (string, error) {
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -782,7 +780,7 @@ func (m *RedisModule) LRange(key string, start, stop int) ([]byte, error) {
|
||||
return makeListJson(reply.([]interface{}), false), nil
|
||||
}
|
||||
|
||||
// 弹出list(消息队列)数据,数据放入out fromLeft表示是否从左侧弹出 block表示是否阻塞 timeout表示阻塞超时
|
||||
// ListPopJson 弹出list(消息队列)数据,数据放入out fromLeft表示是否从左侧弹出 block表示是否阻塞 timeout表示阻塞超时
|
||||
func (m *RedisModule) ListPopJson(key string, fromLeft, block bool, timeout int, out interface{}) error {
|
||||
b, err := m.ListPop(key, fromLeft, block, timeout)
|
||||
if err != nil {
|
||||
@@ -795,7 +793,7 @@ func (m *RedisModule) ListPopJson(key string, fromLeft, block bool, timeout int,
|
||||
return nil
|
||||
}
|
||||
|
||||
// 弹出list(消息队列)数据 fromLeft表示是否从左侧弹出 block表示是否阻塞 timeout表示阻塞超时
|
||||
// ListPop 弹出list(消息队列)数据 fromLeft表示是否从左侧弹出 block表示是否阻塞 timeout表示阻塞超时
|
||||
func (m *RedisModule) ListPop(key string, fromLeft, block bool, timeout int) ([]byte, error) {
|
||||
cmd := ""
|
||||
if fromLeft {
|
||||
@@ -837,7 +835,7 @@ func (m *RedisModule) ListPop(key string, fromLeft, block bool, timeout int) ([]
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// 有序集合插入Json
|
||||
// ZADDInsertJson 有序集合插入Json
|
||||
func (m *RedisModule) ZADDInsertJson(key string, score float64, value interface{}) error {
|
||||
|
||||
conn, err := m.getConn()
|
||||
@@ -857,7 +855,7 @@ func (m *RedisModule) ZADDInsertJson(key string, score float64, value interface{
|
||||
return nil
|
||||
}
|
||||
|
||||
// 有序集合插入
|
||||
// ZADDInsert 有序集合插入
|
||||
func (m *RedisModule) ZADDInsert(key string, score float64, Data interface{}) error {
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -897,7 +895,7 @@ func (m *RedisModule) ZRangeJSON(key string, start, stop int, ascend bool, withS
|
||||
return nil
|
||||
}
|
||||
|
||||
// 取有序set指定排名 ascend=true表示按升序遍历 否则按降序遍历
|
||||
// ZRange 取有序set指定排名 ascend=true表示按升序遍历 否则按降序遍历
|
||||
func (m *RedisModule) ZRange(key string, start, stop int, ascend bool, withScores bool) ([]byte, error) {
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -921,7 +919,7 @@ func (m *RedisModule) ZRange(key string, start, stop int, ascend bool, withScore
|
||||
return makeListJson(reply.([]interface{}), withScores), nil
|
||||
}
|
||||
|
||||
// 获取有序集合长度
|
||||
// Zcard 获取有序集合长度
|
||||
func (m *RedisModule) Zcard(key string) (int, error) {
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -1005,7 +1003,7 @@ func (m *RedisModule) ZRangeByScore(key string, start, stop float64, ascend bool
|
||||
return makeListJson(reply.([]interface{}), withScores), nil
|
||||
}
|
||||
|
||||
// 获取指定member的排名
|
||||
// ZScore 获取指定member的排名
|
||||
func (m *RedisModule) ZScore(key string, member interface{}) (float64, error) {
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -1021,7 +1019,7 @@ func (m *RedisModule) ZScore(key string, member interface{}) (float64, error) {
|
||||
return redis.Float64(reply, err)
|
||||
}
|
||||
|
||||
// 获取指定member的排名
|
||||
// ZRank 获取指定member的排名
|
||||
func (m *RedisModule) ZRank(key string, member interface{}, ascend bool) (int, error) {
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -1080,7 +1078,7 @@ func (m *RedisModule) ZREMMulti(key string, member ...interface{}) (int, error)
|
||||
|
||||
func (m *RedisModule) HincrbyHashInt(redisKey, hashKey string, value int) error {
|
||||
if redisKey == "" || hashKey == "" {
|
||||
return errors.New("Key Is Empty")
|
||||
return errors.New("key is empty")
|
||||
}
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -1140,7 +1138,7 @@ func (m *RedisModule) Keys(key string) ([]string, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
strs := []string{}
|
||||
var strs []string
|
||||
for _, val := range retList {
|
||||
strVal, ok := val.([]byte)
|
||||
if !ok {
|
||||
|
||||
Reference in New Issue
Block a user