Compare commits

..

7 Commits

Author SHA1 Message Date
tx7do
5717d4aefe feat: entgo mixin. 2023-10-31 04:46:44 +08:00
tx7do
62142a6836 feat: entgo mixin. 2023-10-31 04:46:17 +08:00
tx7do
9990fa43e0 fix: pagination bug. 2023-10-28 20:14:31 +08:00
tx7do
82fbdc15d9 feat: byte utils 2023-10-28 20:06:50 +08:00
tx7do
b4188ca4d8 feat: io utils 2023-10-27 17:01:35 +08:00
tx7do
e9ea8fa536 feat: io utils 2023-10-27 16:58:06 +08:00
tx7do
6ed25e948c feat: entgo. 2023-10-27 08:15:00 +08:00
16 changed files with 221 additions and 26 deletions

View File

@@ -1 +1 @@
# kratos-utils
# go-utils

44
byteutil/util.go Normal file
View File

@@ -0,0 +1,44 @@
package byteutil
import (
"bytes"
"encoding/binary"
)
// IntToBytes 将int转换为[]byte
func IntToBytes(n int) []byte {
data := int64(n)
byteBuf := bytes.NewBuffer([]byte{})
_ = binary.Write(byteBuf, binary.BigEndian, data)
return byteBuf.Bytes()
}
// BytesToInt 将[]byte转换为int
func BytesToInt(bys []byte) int {
byteBuf := bytes.NewBuffer(bys)
var data int64
_ = binary.Read(byteBuf, binary.BigEndian, &data)
return int(data)
}
// ByteToLower lowers a byte
func ByteToLower(b byte) byte {
if b <= '\u007F' {
if 'A' <= b && b <= 'Z' {
b += 'a' - 'A'
}
return b
}
return b
}
// ByteToUpper upper a byte
func ByteToUpper(b byte) byte {
if b <= '\u007F' {
if 'a' <= b && b <= 'z' {
b -= 'a' - 'A'
}
return b
}
return b
}

11
byteutil/util_test.go Normal file
View File

@@ -0,0 +1,11 @@
package byteutil
import (
"fmt"
"testing"
)
func TestIntToBytes(t *testing.T) {
fmt.Println(IntToBytes(1))
fmt.Println(BytesToInt(IntToBytes(1)))
}

View File

@@ -6,9 +6,9 @@ require (
entgo.io/contrib v0.4.5
entgo.io/ent v0.12.4
github.com/go-kratos/kratos/v2 v2.7.1
github.com/google/uuid v1.3.1
github.com/google/uuid v1.4.0
github.com/stretchr/testify v1.8.4
github.com/tx7do/go-utils v1.1.0
github.com/tx7do/go-utils v1.1.3
)
require (

View File

@@ -29,6 +29,7 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/hcl/v2 v2.19.1 h1://i05Jqznmb2EXqa39Nsvyan2o5XyMowW5fnCKW5RPI=
github.com/hashicorp/hcl/v2 v2.19.1/go.mod h1:ThLC89FV4p9MPW804KVbe/cEXoQ8NZEh+JtMeeGErHE=
github.com/jhump/protoreflect v1.15.3 h1:6SFRuqU45u9hIZPJAoZ8c28T3nK64BNdp9w6jFonzls=

View File

@@ -29,6 +29,7 @@ func (SwitchStatus) Fields() []ent.Field {
// dialect.MySQL: "switch_status",
// dialect.Postgres: "switch_status",
//}).
Default("ON").
Values(
"OFF",
"ON",

View File

@@ -21,14 +21,14 @@ func QueryCommandToOrderConditions(orderBys []string) (error, func(s *sql.Select
continue
}
s.OrderBy(sql.Desc(s.C(key)))
BuildOrderSelect(s, key, true)
} else {
// 升序
if len(v) == 0 {
continue
}
s.OrderBy(sql.Asc(s.C(v)))
BuildOrderSelect(s, v, false)
}
}
}
@@ -37,9 +37,17 @@ func QueryCommandToOrderConditions(orderBys []string) (error, func(s *sql.Select
func BuildOrderSelector(orderBys []string, defaultOrderField string) (error, func(s *sql.Selector)) {
if len(orderBys) == 0 {
return nil, func(s *sql.Selector) {
s.OrderBy(sql.Desc(s.C(defaultOrderField)))
BuildOrderSelect(s, defaultOrderField, true)
}
} else {
return QueryCommandToOrderConditions(orderBys)
}
}
func BuildOrderSelect(s *sql.Selector, field string, desc bool) {
if desc {
s.OrderBy(sql.Desc(s.C(field)))
} else {
s.OrderBy(sql.Asc(s.C(field)))
}
}

View File

@@ -9,18 +9,21 @@ import (
func BuildPaginationSelector(page, pageSize int32, noPaging bool) func(*sql.Selector) {
if noPaging {
return nil
}
if page == 0 {
page = DefaultPage
}
if pageSize == 0 {
pageSize = DefaultPageSize
}
return func(s *sql.Selector) {
s.Offset(paging.GetPageOffset(page, pageSize)).
Limit(int(pageSize))
} else {
return func(s *sql.Selector) {
BuildPaginationSelect(s, page, pageSize)
}
}
}
func BuildPaginationSelect(s *sql.Selector, page, pageSize int32) {
if page < 1 {
page = paging.DefaultPage
}
if pageSize < 1 {
pageSize = paging.DefaultPageSize
}
offset := paging.GetPageOffset(page, pageSize)
s.Offset(offset).Limit(int(pageSize))
}

View File

@@ -5,11 +5,6 @@ import (
_ "github.com/go-kratos/kratos/v2/encoding/json"
)
const (
DefaultPage = 1
DefaultPageSize = 10
)
// BuildQuerySelector 构建分页查询选择器
func BuildQuerySelector(
dbDriverName string,

1
go.mod
View File

@@ -3,6 +3,7 @@ module github.com/tx7do/go-utils
go 1.20
require (
github.com/google/uuid v1.4.0
github.com/sony/sonyflake v1.2.0
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.14.0

2
go.sum
View File

@@ -1,6 +1,8 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=

85
ioutil/path.go Normal file
View File

@@ -0,0 +1,85 @@
package ioutil
import (
"os"
"path/filepath"
)
// GetWorkingDirPath 获取工作路径
func GetWorkingDirPath() string {
dir, err := os.Getwd()
if err != nil {
panic(err)
}
return dir
}
// GetExePath 获取可执行程序路径
func GetExePath() string {
ex, err := os.Executable()
if err != nil {
panic(err)
}
exePath := filepath.Dir(ex)
return exePath
}
// GetAbsPath 获取绝对路径
func GetAbsPath() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
panic(err)
}
return dir
}
// PathExist 路径是否存在
func PathExist(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
// GetFileList 获取文件夹下面的所有文件的列表
func GetFileList(root string) []string {
var files []string
if err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
files = append(files, path)
return nil
}); err != nil {
return nil
}
return files
}
// GetFolderNameList 获取当前文件夹下面的所有文件夹名的列表
func GetFolderNameList(root string) []string {
var names []string
fs, _ := os.ReadDir(root)
for _, file := range fs {
if file.IsDir() {
names = append(names, file.Name())
}
}
return names
}
// ReadFile 读取文件
func ReadFile(path string) []byte {
content, err := os.ReadFile(path)
if err != nil {
return nil
}
return content
}

View File

@@ -1,5 +1,11 @@
package pagination
const (
DefaultPage = 1 // 默认页数
DefaultPageSize = 10 // 默认每页行数
)
// GetPageOffset 计算偏移量
func GetPageOffset(pageNum, pageSize int32) int {
return int((pageNum - 1) * pageSize)
}

View File

@@ -1,6 +1,6 @@
git tag v1.1.0
git tag v1.1.3
git tag bank_card/v1.1.0
git tag entgo/v1.1.1
git tag entgo/v1.1.4
git tag geoip/v1.1.0
git push origin --tags

7
uuid/test_trans.go Normal file
View File

@@ -0,0 +1,7 @@
package uuid
import "testing"
func TestUUID(t *testing.T) {
}

31
uuid/trans.go Normal file
View File

@@ -0,0 +1,31 @@
package uuid
import (
"github.com/google/uuid"
"github.com/tx7do/go-utils/trans"
)
func ToUuidPtr(str *string) *uuid.UUID {
var id *uuid.UUID
if str != nil {
_id, err := uuid.Parse(*str)
if err != nil {
return nil
}
id = &_id
}
return id
}
func ToUuid(str string) uuid.UUID {
id, _ := uuid.Parse(str)
return id
}
func ToStringPtr(id *uuid.UUID) *string {
var strUUID *string
if id != nil {
strUUID = trans.String(id.String())
}
return strUUID
}