Skip to content

Commit

Permalink
fix(Golang): 修复time.Duration为空值时的异常情况
Browse files Browse the repository at this point in the history
  • Loading branch information
storezhang committed Jun 3, 2024
1 parent 14b0fcd commit ff72ec6
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion internal/param/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,24 @@ func NewDuration(params *Copy) *Duration {
func (d *Duration) Protobuf(ft reflect.Type, tt reflect.Type, from any) (to any, err error) {
var seconds int64 = 0
var nanos int32 = 0
var set = false

name := ft.String()
if name == constant.DurationPtr && d.isProtobufType(tt) {
duration := from.(*time.Duration)
nano := duration.Nanoseconds()
seconds = nano / 1000000000
nanos = int32(nano % 1000000000)
set = true
} else if name == constant.Duration && d.isProtobufType(tt) {
duration := from.(time.Duration)
nano := duration.Nanoseconds()
seconds = nano / 1000000000
nanos = int32(nano % 1000000000)
set = true
}

if 0 != nanos || 0 != seconds {
if set {
secondsKey := gox.Ift(constant.Json == d.params.Tag, constant.KeySeconds, constant.KeySecondsUpper)
nanosKey := gox.Ift(constant.Json == d.params.Tag, constant.KeyNanos, constant.KeyNanosUpper)
to = &map[string]any{
Expand Down

0 comments on commit ff72ec6

Please sign in to comment.