deal with empty array
This commit is contained in:
parent
8e20a5038d
commit
f5b2023207
|
|
@ -164,6 +164,11 @@ type Event struct {
|
||||||
// IPGeo is part of the UniFi Event data. Each event may have up to three of these.
|
// 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.
|
// One for source, one for dest and one for the USG location.
|
||||||
type IPGeo struct {
|
type IPGeo struct {
|
||||||
|
GeoIP
|
||||||
|
}
|
||||||
|
|
||||||
|
// GeoIP is a struct in a struct to deal with weird UniFi output.
|
||||||
|
type GeoIP struct {
|
||||||
Asn int64 `json:"asn"`
|
Asn int64 `json:"asn"`
|
||||||
Latitude float64 `json:"latitude"`
|
Latitude float64 `json:"latitude"`
|
||||||
Longitude float64 `json:"longitude"`
|
Longitude float64 `json:"longitude"`
|
||||||
|
|
@ -173,3 +178,13 @@ type IPGeo struct {
|
||||||
CountryName string `json:"country_name"`
|
CountryName string `json:"country_name"`
|
||||||
Organization string `json:"organization"`
|
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)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue