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 SettingGuestAccess struct {
|
||||
@@ -116,6 +118,31 @@ type SettingGuestAccess struct {
|
||||
XWechatSecretKey string `json:"x_wechat_secret_key,omitempty"`
|
||||
}
|
||||
|
||||
func (dst *SettingGuestAccess) UnmarshalJSON(b []byte) error {
|
||||
type Alias SettingGuestAccess
|
||||
aux := &struct {
|
||||
ExpireNumber emptyStringInt `json:"expire_number"`
|
||||
ExpireUnit emptyStringInt `json:"expire_unit"`
|
||||
PortalCustomizedBoxOpacity emptyStringInt `json:"portal_customized_box_opacity"`
|
||||
RADIUSDisconnectPort emptyStringInt `json:"radius_disconnect_port"`
|
||||
|
||||
*Alias
|
||||
}{
|
||||
Alias: (*Alias)(dst),
|
||||
}
|
||||
|
||||
err := json.Unmarshal(b, &aux)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to unmarshal alias: %w", err)
|
||||
}
|
||||
dst.ExpireNumber = int(aux.ExpireNumber)
|
||||
dst.ExpireUnit = int(aux.ExpireUnit)
|
||||
dst.PortalCustomizedBoxOpacity = int(aux.PortalCustomizedBoxOpacity)
|
||||
dst.RADIUSDisconnectPort = int(aux.RADIUSDisconnectPort)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) getSettingGuestAccess(ctx context.Context, site string) (*SettingGuestAccess, error) {
|
||||
var respBody struct {
|
||||
Meta meta `json:"meta"`
|
||||
|
||||
Reference in New Issue
Block a user