Compare commits
1 Commits
copierutil
...
v1.1.23
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
50161f8c8a |
2
tag.bat
2
tag.bat
@@ -1,4 +1,4 @@
|
||||
git tag v1.1.22
|
||||
git tag v1.1.23
|
||||
|
||||
git tag bank_card/v1.1.5
|
||||
git tag geoip/v1.1.5
|
||||
|
||||
@@ -236,7 +236,7 @@ func DurationpbToDuration(duration *durationpb.Duration) *time.Duration {
|
||||
return &d
|
||||
}
|
||||
|
||||
func DurationpbSecond(duration *durationpb.Duration) *float64 {
|
||||
func DurationpbToSecond(duration *durationpb.Duration) *float64 {
|
||||
if duration == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -244,3 +244,20 @@ func DurationpbSecond(duration *durationpb.Duration) *float64 {
|
||||
secondsInt64 := seconds
|
||||
return &secondsInt64
|
||||
}
|
||||
|
||||
func StringToDurationpb(in *string) *durationpb.Duration {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
f, _ := time.ParseDuration(*in)
|
||||
return durationpb.New(f)
|
||||
}
|
||||
|
||||
func DurationpbToString(in *durationpb.Duration) *string {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return trans.Ptr(in.AsDuration().String())
|
||||
}
|
||||
|
||||
@@ -288,21 +288,21 @@ func TestSecondToDurationpb(t *testing.T) {
|
||||
assert.Nil(t, result)
|
||||
}
|
||||
|
||||
func TestDurationpbSecond(t *testing.T) {
|
||||
func TestDurationpbToSecond(t *testing.T) {
|
||||
// 测试非空输入
|
||||
duration := durationpb.New(5 * time.Second)
|
||||
result := DurationpbSecond(duration)
|
||||
result := DurationpbToSecond(duration)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, 5.0, *result, "应返回正确的秒数")
|
||||
|
||||
// 测试零输入
|
||||
duration = durationpb.New(0)
|
||||
result = DurationpbSecond(duration)
|
||||
result = DurationpbToSecond(duration)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, 0.0, *result, "应返回零秒")
|
||||
|
||||
// 测试空输入
|
||||
result = DurationpbSecond(nil)
|
||||
result = DurationpbToSecond(nil)
|
||||
assert.Nil(t, result, "空输入应返回nil")
|
||||
}
|
||||
|
||||
@@ -353,3 +353,48 @@ func TestTimeToUnixSecondInt64Ptr(t *testing.T) {
|
||||
result = TimeToUnixSecondInt64Ptr(nil)
|
||||
assert.Nil(t, result)
|
||||
}
|
||||
|
||||
func TestStringToDurationpb(t *testing.T) {
|
||||
// 测试有效输入
|
||||
validInput := "1h30m"
|
||||
expected := durationpb.New(90 * time.Minute)
|
||||
result := StringToDurationpb(&validInput)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, expected, result)
|
||||
|
||||
// 测试无效输入
|
||||
invalidInput := "invalid-duration"
|
||||
result = StringToDurationpb(&invalidInput)
|
||||
assert.NotNil(t, result) // 即使输入无效,time.ParseDuration 返回零值
|
||||
assert.Equal(t, durationpb.New(0), result)
|
||||
|
||||
// 测试空字符串输入
|
||||
emptyInput := ""
|
||||
result = StringToDurationpb(&emptyInput)
|
||||
assert.NotNil(t, result) // 空字符串解析为零值
|
||||
assert.Equal(t, durationpb.New(0), result)
|
||||
|
||||
// 测试空指针输入
|
||||
result = StringToDurationpb(nil)
|
||||
assert.Nil(t, result)
|
||||
}
|
||||
|
||||
func TestDurationpbToString(t *testing.T) {
|
||||
// 测试有效输入
|
||||
duration := durationpb.New(90 * time.Second) // 90秒
|
||||
expected := "1m30s"
|
||||
result := DurationpbToString(duration)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, expected, *result)
|
||||
|
||||
// 测试零值输入
|
||||
duration = durationpb.New(0)
|
||||
expected = "0s"
|
||||
result = DurationpbToString(duration)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, expected, *result)
|
||||
|
||||
// 测试空输入
|
||||
result = DurationpbToString(nil)
|
||||
assert.Nil(t, result)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user