Skip to content

Commit

Permalink
fix: unmarshal duration
Browse files Browse the repository at this point in the history
Signed-off-by: Liang Zheng <zhengliang0901@gmail.com>
  • Loading branch information
microyahoo authored and mulbc committed Dec 19, 2023
1 parent 40f8f78 commit 3ea7679
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions common/configFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,33 +223,46 @@ func (d *Duration) UnmarshalJSON(b []byte) error {
return err
}
switch value := v.(type) {
case int:
*d = Duration(time.Duration(value))
case float64:
*d = Duration(time.Duration(value))
return nil
case string:
tmp, err := time.ParseDuration(value)
if err != nil {
return err
}
*d = Duration(tmp)
return nil
default:
return errors.New("invalid duration")
}
return nil
}

func (d Duration) MarshalYAML() ([]byte, error) {
return yaml.Marshal(time.Duration(d).String())
}

func (d *Duration) UnmarshalYAML(unmarshal func(interface{}) error) error {
var yamlDuration time.Duration
err := unmarshal(yamlDuration)
var v interface{}
err := unmarshal(&v)
if err != nil {
return err
}

*d = Duration(yamlDuration)
switch value := v.(type) {
case int:
*d = Duration(time.Duration(value))
case float64:
*d = Duration(time.Duration(value))
case string:
tmp, err := time.ParseDuration(value)
if err != nil {
return err
}
*d = Duration(tmp)
default:
return errors.New("invalid duration")
}
return nil
}

Expand Down

0 comments on commit 3ea7679

Please sign in to comment.