mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-10 18:38:22 +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")
|
||||
}
|
||||
}
|
|
@ -1,14 +1,12 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
import "nfsense.net/nfsense/internal/definitions/common"
|
||||
|
||||
type DHCPv4Server struct {
|
||||
Interface string `json:"interface"`
|
||||
Pool []string `json:"pool"`
|
||||
DefaultLeaseTime time.Duration `json:"default_lease_time"`
|
||||
MaxLeaseTime time.Duration `json:"max_lease_time"`
|
||||
Interface string `json:"interface"`
|
||||
Pool []string `json:"pool"`
|
||||
DefaultLeaseTime common.Duration `json:"default_lease_time"`
|
||||
MaxLeaseTime common.Duration `json:"max_lease_time"`
|
||||
|
||||
GatewayMode Mode `json:"gateway_mode"`
|
||||
Gateway *string `json:"gateway,omitempty"`
|
||||
|
|
|
@ -8,8 +8,8 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions/common"
|
||||
"nfsense.net/nfsense/internal/definitions/config"
|
||||
"nfsense.net/nfsense/internal/util"
|
||||
)
|
||||
|
@ -66,7 +66,7 @@ func getAddressObjectAsPoolRange(conf config.Config, name string) string {
|
|||
return strings.ReplaceAll(conf.Object.Addresses[name].Range.String(), "-", " ")
|
||||
}
|
||||
|
||||
func getTimeInSecond(dur time.Duration) string {
|
||||
func getTimeInSecond(dur common.Duration) string {
|
||||
return fmt.Sprintf("%d", int(dur.Seconds()))
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue