add common.Duration

This commit is contained in:
Samuel Lorch 2023-04-24 18:09:22 +02:00
parent cc7f8b85b5
commit a9a0bebc2f
3 changed files with 41 additions and 9 deletions

View 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")
}
}

View file

@ -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"`

View file

@ -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()))
}