From 06905db6df47fd825423e9f912d42af0cdc1e4cd Mon Sep 17 00:00:00 2001 From: Callan Bryant Date: Mon, 26 Oct 2020 22:47:02 +0000 Subject: [PATCH] allow empty IPNets --- exttypes.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/exttypes.go b/exttypes.go index 86165f8..3d13d1b 100644 --- a/exttypes.go +++ b/exttypes.go @@ -17,6 +17,14 @@ func (n JSONIPNet) MarshalJSON() ([]byte, error) { func (n *JSONIPNet) UnmarshalJSON(b []byte) error { cidr := strings.Trim(string(b), "\"") + + if cidr == "" { + // Leave as empty/uninitialised IPNet. A bit like omitempty behaviour, + // but we can leave the field there and blank which is useful if the + // user wishes to add the cidr manually. + return nil + } + IP, IPNet, err := net.ParseCIDR(cidr) if err == nil {