deal with empty array

This commit is contained in:
davidnewhall2 2020-06-21 23:21:10 -07:00
parent 8e20a5038d
commit f5b2023207
1 changed files with 15 additions and 0 deletions

View File

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