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:
Hendrik "T4cC0re" Meyer
2021-01-02 09:40:52 +01:00
committed by Paul Tyng
parent c5ff8c8593
commit 4aed7d703d
67 changed files with 2298 additions and 155 deletions

View File

@@ -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 ChannelPlan struct {
@@ -36,18 +38,74 @@ type ChannelPlan struct {
SiteBlacklistedChannels []ChannelPlanSiteBlacklistedChannels `json:"site_blacklisted_channels,omitempty"`
}
func (dst *ChannelPlan) UnmarshalJSON(b []byte) error {
type Alias ChannelPlan
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 ChannelPlanApBlacklistedChannels struct {
Channel int `json:"channel,omitempty"` // 36|38|40|42|44|46|48|52|56|60|64|100|104|108|112|116|120|124|128|132|136|140|144|149|153|157|161|165|183|184|185|187|188|189|192|196
MAC string `json:"mac,omitempty"` // ^([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2})$
Timestamp int `json:"timestamp,omitempty"` // [1-9][0-9]{12}
}
func (dst *ChannelPlanApBlacklistedChannels) UnmarshalJSON(b []byte) error {
type Alias ChannelPlanApBlacklistedChannels
aux := &struct {
Channel emptyStringInt `json:"channel"`
Timestamp emptyStringInt `json:"timestamp"`
*Alias
}{
Alias: (*Alias)(dst),
}
err := json.Unmarshal(b, &aux)
if err != nil {
return fmt.Errorf("unable to unmarshal alias: %w", err)
}
dst.Channel = int(aux.Channel)
dst.Timestamp = int(aux.Timestamp)
return nil
}
type ChannelPlanCoupling struct {
Rssi int `json:"rssi,omitempty"`
Source string `json:"source,omitempty"` // ^([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2}).*$
Target string `json:"target,omitempty"` // ^([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2}).*$
}
func (dst *ChannelPlanCoupling) UnmarshalJSON(b []byte) error {
type Alias ChannelPlanCoupling
aux := &struct {
Rssi emptyStringInt `json:"rssi"`
*Alias
}{
Alias: (*Alias)(dst),
}
err := json.Unmarshal(b, &aux)
if err != nil {
return fmt.Errorf("unable to unmarshal alias: %w", err)
}
dst.Rssi = int(aux.Rssi)
return nil
}
type ChannelPlanRadioTable struct {
BackupChannel string `json:"backup_channel,omitempty"` // [0-9]|[1][0-4]|16|34|36|38|40|42|44|46|48|52|56|60|64|100|104|108|112|116|120|124|128|132|136|140|144|149|153|157|161|165|183|184|185|187|188|189|192|196|auto
Channel string `json:"channel,omitempty"` // [0-9]|[1][0-4]|16|34|36|38|40|42|44|46|48|52|56|60|64|100|104|108|112|116|120|124|128|132|136|140|144|149|153|157|161|165|183|184|185|187|188|189|192|196|auto
@@ -58,16 +116,72 @@ type ChannelPlanRadioTable struct {
Width int `json:"width,omitempty"` // 20|40|80|160
}
func (dst *ChannelPlanRadioTable) UnmarshalJSON(b []byte) error {
type Alias ChannelPlanRadioTable
aux := &struct {
Width emptyStringInt `json:"width"`
*Alias
}{
Alias: (*Alias)(dst),
}
err := json.Unmarshal(b, &aux)
if err != nil {
return fmt.Errorf("unable to unmarshal alias: %w", err)
}
dst.Width = int(aux.Width)
return nil
}
type ChannelPlanSatisfactionTable struct {
DeviceMAC string `json:"device_mac,omitempty"` // ^([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2})$
Satisfaction float64 `json:"satisfaction,omitempty"`
}
func (dst *ChannelPlanSatisfactionTable) UnmarshalJSON(b []byte) error {
type Alias ChannelPlanSatisfactionTable
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 ChannelPlanSiteBlacklistedChannels struct {
Channel int `json:"channel,omitempty"` // 36|38|40|42|44|46|48|52|56|60|64|100|104|108|112|116|120|124|128|132|136|140|144|149|153|157|161|165|183|184|185|187|188|189|192|196
Timestamp int `json:"timestamp,omitempty"` // [1-9][0-9]{12}
}
func (dst *ChannelPlanSiteBlacklistedChannels) UnmarshalJSON(b []byte) error {
type Alias ChannelPlanSiteBlacklistedChannels
aux := &struct {
Channel emptyStringInt `json:"channel"`
Timestamp emptyStringInt `json:"timestamp"`
*Alias
}{
Alias: (*Alias)(dst),
}
err := json.Unmarshal(b, &aux)
if err != nil {
return fmt.Errorf("unable to unmarshal alias: %w", err)
}
dst.Channel = int(aux.Channel)
dst.Timestamp = int(aux.Timestamp)
return nil
}
func (c *Client) listChannelPlan(ctx context.Context, site string) ([]ChannelPlan, error) {
var respBody struct {
Meta meta `json:"meta"`