Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
lichunzhu committed Aug 10, 2020
1 parent 7fa5245 commit 7c92fa1
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pkg/util/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@
package util

import (
"encoding/json"
"fmt"
"strconv"
"time"

"github.com/BurntSushi/toml"
"github.com/pingcap/errors"
)

var empty = ""
var _ toml.TextMarshaler = Duration(empty)
var _ toml.TextUnmarshaler = (*Duration)(&empty)
var _ json.Marshaler = Duration(empty)
var _ json.Unmarshaler = (*Duration)(&empty)

// Duration is a wrapper of time.Duration for TOML and JSON.
type Duration string

Expand All @@ -30,8 +38,8 @@ func NewDuration(duration time.Duration) Duration {
}

// MarshalJSON returns the duration as a JSON string.
func (d *Duration) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, *d)), nil
func (d Duration) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, d)), nil
}

// UnmarshalJSON parses a JSON string into the duration.
Expand All @@ -40,6 +48,11 @@ func (d *Duration) UnmarshalJSON(text []byte) error {
if err != nil {
return errors.WithStack(err)
}
td := Duration(s)
_, err = td.ParseDuration()
if err != nil {
return errors.WithStack(err)
}
*d = Duration(s)
return nil
}
Expand Down

0 comments on commit 7c92fa1

Please sign in to comment.