Generate UnmarshalJSON to handle emptyStringInt types
This commit changes the code generator to generate a `UnmarshalJSON` for each struct, so that if unmarshalled it properly handles UniFis varying integer values via the `emptyStringInt` type. Structs not including a field of `int` type will still have the function generated, but it will effectively do nothing. Fixes #18
This commit is contained in:
committed by
Paul Tyng
parent
c5ff8c8593
commit
4aed7d703d
@@ -5,13 +5,15 @@ package unifi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// just to fix compile issues with the import
|
||||
var (
|
||||
_ fmt.Formatter
|
||||
_ context.Context
|
||||
_ fmt.Formatter
|
||||
_ json.Marshaler
|
||||
)
|
||||
|
||||
type SettingIps struct {
|
||||
@@ -38,6 +40,22 @@ type SettingIps struct {
|
||||
Suppression SettingIpsSuppression `json:"suppression,omitempty"`
|
||||
}
|
||||
|
||||
func (dst *SettingIps) UnmarshalJSON(b []byte) error {
|
||||
type Alias SettingIps
|
||||
aux := &struct {
|
||||
*Alias
|
||||
}{
|
||||
Alias: (*Alias)(dst),
|
||||
}
|
||||
|
||||
err := json.Unmarshal(b, &aux)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to unmarshal alias: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type SettingIpsAlerts struct {
|
||||
Category string `json:"category,omitempty"`
|
||||
Gid int `json:"gid,omitempty"`
|
||||
@@ -47,6 +65,27 @@ type SettingIpsAlerts struct {
|
||||
Type string `json:"type,omitempty"` // all|track
|
||||
}
|
||||
|
||||
func (dst *SettingIpsAlerts) UnmarshalJSON(b []byte) error {
|
||||
type Alias SettingIpsAlerts
|
||||
aux := &struct {
|
||||
Gid emptyStringInt `json:"gid"`
|
||||
ID emptyStringInt `json:"id"`
|
||||
|
||||
*Alias
|
||||
}{
|
||||
Alias: (*Alias)(dst),
|
||||
}
|
||||
|
||||
err := json.Unmarshal(b, &aux)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to unmarshal alias: %w", err)
|
||||
}
|
||||
dst.Gid = int(aux.Gid)
|
||||
dst.ID = int(aux.ID)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type SettingIpsDNSFilters struct {
|
||||
AllowedSites []string `json:"allowed_sites,omitempty"` // ^[a-zA-Z0-9.-]+$|^$
|
||||
BlockedSites []string `json:"blocked_sites,omitempty"` // ^[a-zA-Z0-9.-]+$|^$
|
||||
@@ -58,29 +97,109 @@ type SettingIpsDNSFilters struct {
|
||||
Version string `json:"version,omitempty"` // v4|v6
|
||||
}
|
||||
|
||||
func (dst *SettingIpsDNSFilters) UnmarshalJSON(b []byte) error {
|
||||
type Alias SettingIpsDNSFilters
|
||||
aux := &struct {
|
||||
*Alias
|
||||
}{
|
||||
Alias: (*Alias)(dst),
|
||||
}
|
||||
|
||||
err := json.Unmarshal(b, &aux)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to unmarshal alias: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type SettingIpsHoneypot struct {
|
||||
IPAddress string `json:"ip_address,omitempty"`
|
||||
NetworkID string `json:"network_id"`
|
||||
Version string `json:"version,omitempty"` // v4|v6
|
||||
}
|
||||
|
||||
func (dst *SettingIpsHoneypot) UnmarshalJSON(b []byte) error {
|
||||
type Alias SettingIpsHoneypot
|
||||
aux := &struct {
|
||||
*Alias
|
||||
}{
|
||||
Alias: (*Alias)(dst),
|
||||
}
|
||||
|
||||
err := json.Unmarshal(b, &aux)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to unmarshal alias: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type SettingIpsSuppression struct {
|
||||
Alerts []SettingIpsAlerts `json:"alerts,omitempty"`
|
||||
Whitelist []SettingIpsWhitelist `json:"whitelist,omitempty"`
|
||||
}
|
||||
|
||||
func (dst *SettingIpsSuppression) UnmarshalJSON(b []byte) error {
|
||||
type Alias SettingIpsSuppression
|
||||
aux := &struct {
|
||||
*Alias
|
||||
}{
|
||||
Alias: (*Alias)(dst),
|
||||
}
|
||||
|
||||
err := json.Unmarshal(b, &aux)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to unmarshal alias: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type SettingIpsTracking struct {
|
||||
Direction string `json:"direction,omitempty"` // both|src|dest
|
||||
Mode string `json:"mode,omitempty"` // ip|subnet|network
|
||||
Value string `json:"value,omitempty"`
|
||||
}
|
||||
|
||||
func (dst *SettingIpsTracking) UnmarshalJSON(b []byte) error {
|
||||
type Alias SettingIpsTracking
|
||||
aux := &struct {
|
||||
*Alias
|
||||
}{
|
||||
Alias: (*Alias)(dst),
|
||||
}
|
||||
|
||||
err := json.Unmarshal(b, &aux)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to unmarshal alias: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type SettingIpsWhitelist struct {
|
||||
Direction string `json:"direction,omitempty"` // both|src|dest
|
||||
Mode string `json:"mode,omitempty"` // ip|subnet|network
|
||||
Value string `json:"value,omitempty"`
|
||||
}
|
||||
|
||||
func (dst *SettingIpsWhitelist) UnmarshalJSON(b []byte) error {
|
||||
type Alias SettingIpsWhitelist
|
||||
aux := &struct {
|
||||
*Alias
|
||||
}{
|
||||
Alias: (*Alias)(dst),
|
||||
}
|
||||
|
||||
err := json.Unmarshal(b, &aux)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to unmarshal alias: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) getSettingIps(ctx context.Context, site string) (*SettingIps, error) {
|
||||
var respBody struct {
|
||||
Meta meta `json:"meta"`
|
||||
|
||||
Reference in New Issue
Block a user