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 SettingRadioAi struct {
@@ -38,6 +40,43 @@ type SettingRadioAi struct {
UseXY bool `json:"useXY"`
}
func (dst *SettingRadioAi) UnmarshalJSON(b []byte) error {
type Alias SettingRadioAi
aux := &struct {
ChannelsNa []emptyStringInt `json:"channels_na"`
ChannelsNg []emptyStringInt `json:"channels_ng"`
HtModesNa []emptyStringInt `json:"ht_modes_na"`
HtModesNg []emptyStringInt `json:"ht_modes_ng"`
*Alias
}{
Alias: (*Alias)(dst),
}
err := json.Unmarshal(b, &aux)
if err != nil {
return fmt.Errorf("unable to unmarshal alias: %w", err)
}
dst.ChannelsNa = make([]int, len(aux.ChannelsNa))
for i, v := range aux.ChannelsNa {
dst.ChannelsNa[i] = int(v)
}
dst.ChannelsNg = make([]int, len(aux.ChannelsNg))
for i, v := range aux.ChannelsNg {
dst.ChannelsNg[i] = int(v)
}
dst.HtModesNa = make([]int, len(aux.HtModesNa))
for i, v := range aux.HtModesNa {
dst.HtModesNa[i] = int(v)
}
dst.HtModesNg = make([]int, len(aux.HtModesNg))
for i, v := range aux.HtModesNg {
dst.HtModesNg[i] = int(v)
}
return nil
}
func (c *Client) getSettingRadioAi(ctx context.Context, site string) (*SettingRadioAi, error) {
var respBody struct {
Meta meta `json:"meta"`