mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-11 19:08:20 +00:00
32 lines
528 B
Go
32 lines
528 B
Go
package definitions
|
|
|
|
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
|
|
}
|