Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions config/parseCmd/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ func Parseparams(f ...func()) {
flag.BoolVar(&config.Dbg, "debug", false, "Debug 模式")
flag.StringVar(&logLevel, "log-level", "info", "日志级别:\ndebug info warn error fatal")
flag.BoolVar(&vers, "version", false, "查看版本")
if len(f) > 0 {
for _, v := range f {
if v != nil {
v()
}
for _, v := range f {
if v != nil {
v()
}
}
flag.Parse()
Expand All @@ -43,6 +41,8 @@ func Parseparams(f ...func()) {
}
// 控制日志等级
switch logLevel {
case "trace":
ulogs.Level = ulogs.LogLevelTrace
case "debug":
ulogs.Level = ulogs.LogLevelDebug
case "info":
Expand All @@ -55,6 +55,7 @@ func Parseparams(f ...func()) {
ulogs.Level = ulogs.LogLevelFatal
}

// noinspection all
if config.EnableParseParamsLog {
fmt.Println("日志级别", logLevel, ulogs.Level)
ulogs.Log("运行参数解析完成...")
Expand Down
80 changes: 67 additions & 13 deletions dataType/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dataType

import (
"database/sql/driver"
"fmt"

"github.com/helays/utils/v2/config"
"github.com/helays/utils/v2/tools"
Expand All @@ -13,6 +14,7 @@ type Byte byte

// noinspection all
func (b Byte) Value() (driver.Value, error) {
// 注意,这里只能接受int64类型
return int64(b), nil
}

Expand All @@ -22,20 +24,11 @@ func (b *Byte) Scan(value any) error {
*b = 0
return nil
}
switch t := value.(type) {
case byte:
*b = Byte(t)
case int8:
*b = Byte(t)
case int:
*b = Byte(t)
default:
v, err := tools.Any2Int[Byte](value)
if err != nil {
return err
}
*b = Byte(v)
v, err := tools.Any2Int[byte](value)
if err != nil {
return fmt.Errorf("Byte.Scan: unknown type %T", value)
}
*b = Byte(v)
return nil
}

Expand All @@ -53,3 +46,64 @@ func (Byte) GormDBDataType(db *gorm.DB, field *schema.Field) string {
}
return "int"
}

type Uint64 struct {
uint64
}

func NewUint64(v uint64) Uint64 {
return Uint64{uint64: v}
}

func (u *Uint64) GetValue() uint64 {
return u.uint64
}

func (u *Uint64) SetValue(v uint64) {
u.uint64 = v
}

func (u *Uint64) Equals(other Uint64) bool {
return u.uint64 == other.uint64
}

func (u *Uint64) EqualsInt(other int) bool {
return u.uint64 == uint64(other)
}

func (u *Uint64) EqualsUint64(other uint64) bool {
return u.uint64 == other
}

// noinspection all
func (u Uint64) Value() (driver.Value, error) {
return u.uint64, nil
}

// noinspection all
func (u *Uint64) Scan(value any) error {
if value == nil {
return nil
}
v, err := tools.Any2Int[uint64](value)
if err != nil {
return err
}
u.uint64 = v
return nil
}

// noinspection all
func (u Uint64) GormDBDataType(db *gorm.DB, field *schema.Field) string {
switch db.Dialector.Name() {
case config.DbTypeSqlite:
return "integer"
case config.DbTypeMysql:
return "BIGINT UNSIGNED"
case config.DbTypePostgres:
return "BIGINT"
case config.DbTypeSqlserver:
return "BIGINT"
}
return "bigint"
}
58 changes: 58 additions & 0 deletions dataType/sessionval.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package dataType

import (
"bytes"
"database/sql/driver"
"encoding/gob"

"github.com/helays/utils/v2/tools"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)

func NewSessionValue(val any) SessionValue {
return SessionValue{Val: val}
}

// noinspection all
type SessionValue struct {
Val any
}

// Value return blob value, implement driver.Valuer interface
// noinspection all
func (s SessionValue) Value() (driver.Value, error) {
var buf bytes.Buffer
err := gob.NewEncoder(&buf).Encode(s)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

// noinspection all
func (s *SessionValue) Scan(val any) error {
if val == nil {
*s = SessionValue{}
return nil
}

b, err := tools.Any2bytes(val)
if err != nil {
return err
}

err = gob.NewDecoder(bytes.NewReader(b)).Decode(s)

return err
}

// GormDBDataType gorm db data type
// noinspection all
func (SessionValue) GormDBDataType(db *gorm.DB, field *schema.Field) string {
return BlobDbDataType(db, field)
}

func (s SessionValue) GormDataType() string {
return "blob"
}
6 changes: 4 additions & 2 deletions db/fileSaver/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package local

import (
"fmt"
"github.com/helays/utils/v2/close/vclose"
"github.com/helays/utils/v2/tools"
"io"
"os"
"path"

"github.com/helays/utils/v2/close/vclose"
"github.com/helays/utils/v2/tools"
)

type Local struct{}
Expand All @@ -29,6 +30,7 @@ func (this Local) Write(p string, src io.Reader, existIgnores ...bool) (int64, e
return 0, fmt.Errorf("创建目录%s失败: %s", dir, err.Error())
}
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
defer vclose.Close(file)
if err != nil {
return 0, fmt.Errorf("打开文件%s失败: %s", filePath, err.Error())
}
Expand Down
Loading