From f5b2023207c949b6c74163a3f8453638e738289a Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Sun, 21 Jun 2020 23:21:10 -0700 Subject: [PATCH] deal with empty array --- core/unifi/events.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/core/unifi/events.go b/core/unifi/events.go index d960136b..af2a15fd 100644 --- a/core/unifi/events.go +++ b/core/unifi/events.go @@ -164,6 +164,11 @@ type Event struct { // IPGeo is part of the UniFi Event data. Each event may have up to three of these. // One for source, one for dest and one for the USG location. type IPGeo struct { + GeoIP +} + +// GeoIP is a struct in a struct to deal with weird UniFi output. +type GeoIP struct { Asn int64 `json:"asn"` Latitude float64 `json:"latitude"` Longitude float64 `json:"longitude"` @@ -173,3 +178,13 @@ type IPGeo struct { CountryName string `json:"country_name"` Organization string `json:"organization"` } + +// UnmarshalJSON is required because sometimes the unifi api returns +// an empty array instead of a struct filled with data. +func (v *IPGeo) UnmarshalJSON(data []byte) error { + if string(data) == "[]" { + return nil // it's empty + } + + return json.Unmarshal(data, &v.GeoIP) +}