1 Star 0 Fork 0

PeterTu_2019/influxdb

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
duration.go 767 Bytes
一键复制 编辑 原始数据 按行查看 历史
package influxdb
import (
"encoding/json"
"errors"
"time"
)
// Duration is based on time.Duration to embed in any struct.
type Duration struct {
time.Duration
}
// MarshalJSON implements json.Marshaler interface.
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
// UnmarshalJSON implements json.Unmarshaler interface.
func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case float64:
d.Duration = time.Duration(value)
return nil
case string:
var err error
d.Duration, err = time.ParseDuration(value)
if err != nil {
return err
}
return nil
default:
return errors.New("invalid duration")
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/petertu_2019/influxdb.git
git@gitee.com:petertu_2019/influxdb.git
petertu_2019
influxdb
influxdb
master

搜索帮助