Compare commits
2 Commits
translator
...
copierutil
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c03ac708f0 | ||
|
|
9035e79520 |
43
copierutil/converters.go
Normal file
43
copierutil/converters.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package copierutil
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
"github.com/tx7do/go-utils/timeutil"
|
||||
"github.com/tx7do/go-utils/trans"
|
||||
)
|
||||
|
||||
var TimeToStringConverter = copier.TypeConverter{
|
||||
SrcType: &time.Time{}, // 源类型
|
||||
DstType: trans.Ptr(""), // 目标类型
|
||||
Fn: func(src interface{}) (interface{}, error) {
|
||||
return timeutil.TimeToTimeString(src.(*time.Time)), nil
|
||||
},
|
||||
}
|
||||
|
||||
var StringToTimeConverter = copier.TypeConverter{
|
||||
SrcType: trans.Ptr(""),
|
||||
DstType: &time.Time{},
|
||||
Fn: func(src interface{}) (interface{}, error) {
|
||||
return timeutil.StringTimeToTime(src.(*string)), nil
|
||||
},
|
||||
}
|
||||
|
||||
var TimeToTimestamppbConverter = copier.TypeConverter{
|
||||
SrcType: &time.Time{},
|
||||
DstType: ×tamppb.Timestamp{},
|
||||
Fn: func(src interface{}) (interface{}, error) {
|
||||
return timeutil.TimeToTimestamppb(src.(*time.Time)), nil
|
||||
},
|
||||
}
|
||||
|
||||
var TimestamppbToTimeConverter = copier.TypeConverter{
|
||||
SrcType: ×tamppb.Timestamp{},
|
||||
DstType: &time.Time{},
|
||||
Fn: func(src interface{}) (interface{}, error) {
|
||||
return timeutil.TimestamppbToTime(src.(*timestamppb.Timestamp)), nil
|
||||
},
|
||||
}
|
||||
14
copierutil/go.mod
Normal file
14
copierutil/go.mod
Normal file
@@ -0,0 +1,14 @@
|
||||
module github.com/tx7do/go-utils/copierutil
|
||||
|
||||
go 1.23.0
|
||||
|
||||
toolchain go1.24.3
|
||||
|
||||
require (
|
||||
github.com/jinzhu/copier v0.4.0
|
||||
github.com/tx7do/go-utils v1.1.22
|
||||
)
|
||||
|
||||
require google.golang.org/protobuf v1.36.6 // indirect
|
||||
|
||||
replace github.com/tx7do/go-utils => ../
|
||||
16
copierutil/go.sum
Normal file
16
copierutil/go.sum
Normal file
@@ -0,0 +1,16 @@
|
||||
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/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
|
||||
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
|
||||
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
3
tag.bat
3
tag.bat
@@ -1,8 +1,9 @@
|
||||
git tag v1.1.21
|
||||
git tag v1.1.22
|
||||
|
||||
git tag bank_card/v1.1.5
|
||||
git tag geoip/v1.1.5
|
||||
git tag translator/v1.1.1
|
||||
git tag copierutil/v0.0.1
|
||||
|
||||
git tag entgo/v1.1.27
|
||||
git tag gorm/v1.1.5
|
||||
|
||||
@@ -48,6 +48,46 @@ func StringToUnixMilliInt64Ptr(tm *string) *int64 {
|
||||
return &unixTime
|
||||
}
|
||||
|
||||
// UnixMilliToTimePtr 毫秒时间戳 -> 时间
|
||||
func UnixMilliToTimePtr(tm *int64) *time.Time {
|
||||
if tm == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
unixMilli := time.UnixMilli(*tm)
|
||||
return &unixMilli
|
||||
}
|
||||
|
||||
// TimeToUnixMilliInt64Ptr 时间 -> 毫秒时间戳
|
||||
func TimeToUnixMilliInt64Ptr(tm *time.Time) *int64 {
|
||||
if tm == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
unixTime := tm.UnixMilli()
|
||||
return &unixTime
|
||||
}
|
||||
|
||||
// UnixSecondToTimePtr 秒时间戳 -> 时间
|
||||
func UnixSecondToTimePtr(tm *int64) *time.Time {
|
||||
if tm == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
unixMilli := time.Unix(*tm, 0)
|
||||
return &unixMilli
|
||||
}
|
||||
|
||||
// TimeToUnixSecondInt64Ptr 时间 -> 秒时间戳
|
||||
func TimeToUnixSecondInt64Ptr(tm *time.Time) *int64 {
|
||||
if tm == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
unixTime := tm.Unix()
|
||||
return &unixTime
|
||||
}
|
||||
|
||||
// StringTimeToTime 时间字符串 -> 时间
|
||||
func StringTimeToTime(str *string) *time.Time {
|
||||
if str == nil {
|
||||
|
||||
@@ -305,3 +305,51 @@ func TestDurationpbSecond(t *testing.T) {
|
||||
result = DurationpbSecond(nil)
|
||||
assert.Nil(t, result, "空输入应返回nil")
|
||||
}
|
||||
|
||||
func TestUnixMilliToTimePtr(t *testing.T) {
|
||||
// 测试有效输入
|
||||
now := time.Now().UnixMilli()
|
||||
result := UnixMilliToTimePtr(&now)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, time.UnixMilli(now), *result)
|
||||
|
||||
// 测试空输入
|
||||
result = UnixMilliToTimePtr(nil)
|
||||
assert.Nil(t, result)
|
||||
}
|
||||
|
||||
func TestTimeToUnixMilliInt64Ptr(t *testing.T) {
|
||||
// 测试有效输入
|
||||
now := time.Now()
|
||||
result := TimeToUnixMilliInt64Ptr(&now)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, now.UnixMilli(), *result)
|
||||
|
||||
// 测试空输入
|
||||
result = TimeToUnixMilliInt64Ptr(nil)
|
||||
assert.Nil(t, result)
|
||||
}
|
||||
|
||||
func TestUnixSecondToTimePtr(t *testing.T) {
|
||||
// 测试有效输入
|
||||
now := time.Now().Unix()
|
||||
result := UnixSecondToTimePtr(&now)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, time.Unix(now, 0), *result)
|
||||
|
||||
// 测试空输入
|
||||
result = UnixSecondToTimePtr(nil)
|
||||
assert.Nil(t, result)
|
||||
}
|
||||
|
||||
func TestTimeToUnixSecondInt64Ptr(t *testing.T) {
|
||||
// 测试有效输入
|
||||
now := time.Now()
|
||||
result := TimeToUnixSecondInt64Ptr(&now)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, now.Unix(), *result)
|
||||
|
||||
// 测试空输入
|
||||
result = TimeToUnixSecondInt64Ptr(nil)
|
||||
assert.Nil(t, result)
|
||||
}
|
||||
|
||||
16
upgrade.bat
16
upgrade.bat
@@ -10,10 +10,22 @@ cd %DIR%/bank_card
|
||||
go get all
|
||||
go mod tidy
|
||||
|
||||
cd %DIR%/geoip
|
||||
go get all
|
||||
go mod tidy
|
||||
|
||||
cd %DIR%/copierutil
|
||||
go get all
|
||||
go mod tidy
|
||||
|
||||
cd %DIR%/translator
|
||||
go get all
|
||||
go mod tidy
|
||||
|
||||
cd %DIR%/entgo
|
||||
go get all
|
||||
go mod tidy
|
||||
|
||||
cd %DIR%/geoip
|
||||
cd %DIR%/gorm
|
||||
go get all
|
||||
go mod tidy
|
||||
go mod tidy
|
||||
Reference in New Issue
Block a user