mirror of
https://github.com/speatzle/nfsense.git
synced 2025-09-13 15:19:08 +00:00
add common.Duration
This commit is contained in:
parent
cc7f8b85b5
commit
a9a0bebc2f
3 changed files with 41 additions and 9 deletions
34
internal/definitions/common/time.go
Normal file
34
internal/definitions/common/time.go
Normal file
|
@ -0,0 +1,34 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Duration struct {
|
||||
time.Duration
|
||||
}
|
||||
|
||||
// MarshalJSON for IPNet
|
||||
func (i Duration) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(int(i.Seconds()))
|
||||
}
|
||||
|
||||
// UnmarshalJSON for IPNet
|
||||
func (i *Duration) UnmarshalJSON(b []byte) error {
|
||||
var v interface{}
|
||||
if err := json.Unmarshal(b, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
switch value := v.(type) {
|
||||
case float64:
|
||||
i.Duration = time.Second * time.Duration(value)
|
||||
return nil
|
||||
case int:
|
||||
i.Duration = time.Second * time.Duration(value)
|
||||
return nil
|
||||
default:
|
||||
return errors.New("invalid duration")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue