mirror of
https://github.com/speatzle/nfsense.git
synced 2025-09-13 15:19:08 +00:00
Seperate Definitions by Sub System
This commit is contained in:
parent
14c8da64cc
commit
72bf96295d
31 changed files with 120 additions and 108 deletions
30
internal/definitions/common/hardwareaddress.go
Normal file
30
internal/definitions/common/hardwareaddress.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net"
|
||||
)
|
||||
|
||||
type HardwareAddress struct {
|
||||
net.HardwareAddr
|
||||
}
|
||||
|
||||
// MarshalJSON for IPCIDR
|
||||
func (i HardwareAddress) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(i.String())
|
||||
}
|
||||
|
||||
// UnmarshalJSON for IPCIDR
|
||||
func (i *HardwareAddress) UnmarshalJSON(b []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(b, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mac, err := net.ParseMAC(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
i.HardwareAddr = mac
|
||||
return nil
|
||||
}
|
32
internal/definitions/common/ipcidr.go
Normal file
32
internal/definitions/common/ipcidr.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net"
|
||||
)
|
||||
|
||||
// IPCIDR is IP Address with the mask in CIDR format
|
||||
type IPCIDR struct {
|
||||
net.IPNet
|
||||
}
|
||||
|
||||
// MarshalJSON for IPCIDR
|
||||
func (i IPCIDR) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(i.String())
|
||||
}
|
||||
|
||||
// UnmarshalJSON for IPCIDR
|
||||
func (i *IPCIDR) UnmarshalJSON(b []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(b, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ip, ipnet, err := net.ParseCIDR(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
i.IPNet = *ipnet
|
||||
i.IPNet.IP = ip
|
||||
return nil
|
||||
}
|
30
internal/definitions/common/ipnet.go
Normal file
30
internal/definitions/common/ipnet.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net"
|
||||
)
|
||||
|
||||
type IPNet struct {
|
||||
net.IPNet
|
||||
}
|
||||
|
||||
// MarshalJSON for IPNet
|
||||
func (i IPNet) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(i.String())
|
||||
}
|
||||
|
||||
// UnmarshalJSON for IPNet
|
||||
func (i *IPNet) UnmarshalJSON(b []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(b, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, ipnet, err := net.ParseCIDR(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
i.IPNet = *ipnet
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue