Compare commits
10 Commits
geoip/v1.1
...
v1.1.4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b27c96f932 | ||
|
|
836f4e2461 | ||
|
|
5717d4aefe | ||
|
|
62142a6836 | ||
|
|
9990fa43e0 | ||
|
|
82fbdc15d9 | ||
|
|
b4188ca4d8 | ||
|
|
e9ea8fa536 | ||
|
|
6ed25e948c | ||
|
|
bfea578907 |
44
byteutil/util.go
Normal file
44
byteutil/util.go
Normal 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
11
byteutil/util_test.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package byteutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIntToBytes(t *testing.T) {
|
||||
fmt.Println(IntToBytes(1))
|
||||
fmt.Println(BytesToInt(IntToBytes(1)))
|
||||
}
|
||||
@@ -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 (
|
||||
|
||||
@@ -7,6 +7,8 @@ entgo.io/ent v0.12.4/go.mod h1:Y3JVAjtlIk8xVZYSn3t3mf8xlZIn5SAOXZQxD6kKI+Q=
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
|
||||
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=
|
||||
github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
|
||||
github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM=
|
||||
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
|
||||
github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY=
|
||||
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
|
||||
github.com/bufbuild/protocompile v0.6.0 h1:Uu7WiSQ6Yj9DbkdnOe7U4mNKp58y9WDMKDn28/ZlunY=
|
||||
@@ -18,6 +20,7 @@ github.com/go-kratos/kratos/v2 v2.7.1/go.mod h1:CPn82O93OLHjtnbuyOKhAG5TkSvw+mFn
|
||||
github.com/go-openapi/inflect v0.19.0 h1:9jCH9scKIbHeV9m12SmPilScz6krDxKRasNNSNPXu/4=
|
||||
github.com/go-openapi/inflect v0.19.0/go.mod h1:lHpZVlpIQqLyKwJ4N+YSc9hchQy/i12fJykb83CRBH4=
|
||||
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
|
||||
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
|
||||
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
||||
@@ -26,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=
|
||||
@@ -33,6 +37,7 @@ github.com/jhump/protoreflect v1.15.3/go.mod h1:4ORHmSBmlCW8fh3xHmJMGyul1zNqZK4E
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 h1:MtvEpTB6LX3vkb4ax0b5D2DHbNAUsen0Gx5wZoq3lV4=
|
||||
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
|
||||
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
|
||||
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
|
||||
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
|
||||
@@ -42,10 +47,13 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
|
||||
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
|
||||
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/zclconf/go-cty v1.14.1 h1:t9fyA35fwjjUMcmL5hLER+e/rEPqrbCK1/OSE4SI9KA=
|
||||
github.com/zclconf/go-cty v1.14.1/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE=
|
||||
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRKQfBXbGkpdV6QMzT3rU1kSTAnfu1dO8dPKjYprgj8=
|
||||
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||
golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
|
||||
|
||||
21
entgo/mixin/creator_id.go
Normal file
21
entgo/mixin/creator_id.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package mixin
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/field"
|
||||
_mixin "entgo.io/ent/schema/mixin"
|
||||
)
|
||||
|
||||
type CreatorId struct {
|
||||
_mixin.Schema
|
||||
}
|
||||
|
||||
func (CreatorId) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Uint64("creator_id").
|
||||
Comment("创建者用户ID").
|
||||
Immutable().
|
||||
Optional().
|
||||
Nillable(),
|
||||
}
|
||||
}
|
||||
37
entgo/mixin/snowflake_id.go
Normal file
37
entgo/mixin/snowflake_id.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package mixin
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
|
||||
"github.com/tx7do/go-utils/sonyflake"
|
||||
)
|
||||
|
||||
type SnowflackId struct {
|
||||
mixin.Schema
|
||||
}
|
||||
|
||||
func (SnowflackId) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Uint64("id").
|
||||
Comment("id").
|
||||
DefaultFunc(sonyflake.GenerateSonyflake).
|
||||
Positive().
|
||||
Immutable().
|
||||
StructTag(`json:"id,omitempty"`).
|
||||
SchemaType(map[string]string{
|
||||
dialect.MySQL: "bigint",
|
||||
dialect.Postgres: "bigint",
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes of the SnowflackId.
|
||||
func (SnowflackId) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("id"),
|
||||
}
|
||||
}
|
||||
33
entgo/mixin/string_id.go
Normal file
33
entgo/mixin/string_id.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package mixin
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type StringId struct {
|
||||
mixin.Schema
|
||||
}
|
||||
|
||||
func (StringId) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("id").
|
||||
Comment("id").
|
||||
MaxLen(25).
|
||||
NotEmpty().
|
||||
Unique().
|
||||
Immutable().
|
||||
Match(regexp.MustCompile("^[0-9a-zA-Z_\\-]+$")).
|
||||
StructTag(`json:"id,omitempty"`),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes of the StringId.
|
||||
func (StringId) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("id"),
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@ func (SwitchStatus) Fields() []ent.Field {
|
||||
// dialect.MySQL: "switch_status",
|
||||
// dialect.Postgres: "switch_status",
|
||||
//}).
|
||||
Default("ON").
|
||||
Values(
|
||||
"OFF",
|
||||
"ON",
|
||||
|
||||
@@ -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)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -5,11 +5,6 @@ import (
|
||||
_ "github.com/go-kratos/kratos/v2/encoding/json"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultPage = 1
|
||||
DefaultPageSize = 10
|
||||
)
|
||||
|
||||
// BuildQuerySelector 构建分页查询选择器
|
||||
func BuildQuerySelector(
|
||||
dbDriverName string,
|
||||
|
||||
3
go.mod
3
go.mod
@@ -3,6 +3,8 @@ module github.com/tx7do/go-utils
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/google/uuid v1.4.0
|
||||
github.com/gosimple/slug v1.13.1
|
||||
github.com/sony/sonyflake v1.2.0
|
||||
github.com/stretchr/testify v1.8.4
|
||||
golang.org/x/crypto v0.14.0
|
||||
@@ -10,6 +12,7 @@ require (
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/gosimple/unidecode v1.0.1 // indirect
|
||||
github.com/kr/pretty v0.3.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/rogpeppe/go-internal v1.10.0 // indirect
|
||||
|
||||
6
go.sum
6
go.sum
@@ -1,6 +1,12 @@
|
||||
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/gosimple/slug v1.13.1 h1:bQ+kpX9Qa6tHRaK+fZR0A0M2Kd7Pa5eHPPsb1JpHD+Q=
|
||||
github.com/gosimple/slug v1.13.1/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ=
|
||||
github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o=
|
||||
github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc=
|
||||
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
85
ioutil/path.go
Normal 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
|
||||
}
|
||||
@@ -1,5 +1,11 @@
|
||||
package pagination
|
||||
|
||||
const (
|
||||
DefaultPage = 1 // 默认页数
|
||||
DefaultPageSize = 10 // 默认每页行数
|
||||
)
|
||||
|
||||
// GetPageOffset 计算偏移量
|
||||
func GetPageOffset(pageNum, pageSize int32) int {
|
||||
return int((pageNum - 1) * pageSize)
|
||||
}
|
||||
|
||||
29
slug/slug.go
Normal file
29
slug/slug.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package slug
|
||||
|
||||
import (
|
||||
"github.com/gosimple/slug"
|
||||
)
|
||||
|
||||
// Generate 生成短链接
|
||||
func Generate(input string) string {
|
||||
slug.Lowercase = true
|
||||
return slug.MakeLang(input, "en")
|
||||
}
|
||||
|
||||
// GenerateCaseSensitive 生成大小写敏感的短链接
|
||||
func GenerateCaseSensitive(input string) string {
|
||||
slug.Lowercase = false
|
||||
return slug.MakeLang(input, "en")
|
||||
}
|
||||
|
||||
// GenerateEnglish 生成英文短链接
|
||||
func GenerateEnglish(input string) string {
|
||||
slug.Lowercase = true
|
||||
return slug.MakeLang(input, "en")
|
||||
}
|
||||
|
||||
// GenerateGerman 生成德文短链接
|
||||
func GenerateGerman(input string) string {
|
||||
slug.Lowercase = true
|
||||
return slug.MakeLang(input, "de")
|
||||
}
|
||||
88
slug/slug_test.go
Normal file
88
slug/slug_test.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package slug
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/gosimple/slug"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGoSimple(t *testing.T) {
|
||||
// 俄文
|
||||
text := slug.Make("Hellö Wörld хелло ворлд")
|
||||
assert.Equal(t, text, "hello-world-khello-vorld")
|
||||
fmt.Println(text)
|
||||
|
||||
// 繁体中文
|
||||
someText := slug.Make("影師")
|
||||
assert.Equal(t, someText, "ying-shi")
|
||||
fmt.Println(someText)
|
||||
|
||||
// 简体中文
|
||||
cnText := slug.Make("天下太平")
|
||||
assert.Equal(t, cnText, "tian-xia-tai-ping")
|
||||
fmt.Println(cnText)
|
||||
|
||||
// 英文
|
||||
enText := slug.MakeLang("This & that", "en")
|
||||
assert.Equal(t, enText, "this-and-that")
|
||||
fmt.Println(enText)
|
||||
|
||||
// 德文
|
||||
deText := slug.MakeLang("Diese & Dass", "de")
|
||||
assert.Equal(t, deText, "diese-und-dass")
|
||||
fmt.Println(deText)
|
||||
|
||||
// 保持大小写
|
||||
slug.Lowercase = false
|
||||
deUppercaseText := slug.MakeLang("Diese & Dass", "de")
|
||||
assert.Equal(t, deUppercaseText, "Diese-und-Dass")
|
||||
fmt.Println(deUppercaseText)
|
||||
|
||||
// 字符替换
|
||||
slug.CustomSub = map[string]string{
|
||||
"water": "sand",
|
||||
}
|
||||
textSub := slug.Make("water is hot")
|
||||
assert.Equal(t, textSub, "sand-is-hot")
|
||||
fmt.Println(textSub)
|
||||
}
|
||||
|
||||
func TestGenerate(t *testing.T) {
|
||||
// 俄文
|
||||
text := Generate("Hellö Wörld хелло ворлд")
|
||||
assert.Equal(t, text, "hello-world-khello-vorld")
|
||||
fmt.Println(text)
|
||||
|
||||
// 繁体中文
|
||||
someText := Generate("影師")
|
||||
assert.Equal(t, someText, "ying-shi")
|
||||
fmt.Println(someText)
|
||||
|
||||
// 简体中文
|
||||
cnText := Generate("天下太平")
|
||||
assert.Equal(t, cnText, "tian-xia-tai-ping")
|
||||
fmt.Println(cnText)
|
||||
|
||||
// 英文
|
||||
enText := GenerateEnglish("This & that")
|
||||
assert.Equal(t, enText, "this-and-that")
|
||||
fmt.Println(enText)
|
||||
|
||||
enText = GenerateCaseSensitive("This & That")
|
||||
assert.Equal(t, enText, "This-and-That")
|
||||
fmt.Println(enText)
|
||||
|
||||
// 德文
|
||||
deText := GenerateGerman("Diese & Dass")
|
||||
assert.Equal(t, deText, "diese-und-dass")
|
||||
fmt.Println(deText)
|
||||
|
||||
slug.CustomSub = map[string]string{
|
||||
"water": "sand",
|
||||
}
|
||||
textSub := Generate("water is hot")
|
||||
assert.Equal(t, textSub, "sand-is-hot")
|
||||
fmt.Println(textSub)
|
||||
}
|
||||
4
tag.bat
4
tag.bat
@@ -1,6 +1,6 @@
|
||||
git tag v1.1.0
|
||||
git tag v1.1.4
|
||||
git tag bank_card/v1.1.0
|
||||
git tag entgo/v1.1.0
|
||||
git tag entgo/v1.1.5
|
||||
git tag geoip/v1.1.0
|
||||
|
||||
git push origin --tags
|
||||
|
||||
7
uuid/test_trans.go
Normal file
7
uuid/test_trans.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package uuid
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestUUID(t *testing.T) {
|
||||
|
||||
}
|
||||
31
uuid/trans.go
Normal file
31
uuid/trans.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user