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
@@ -3,33 +3,8 @@ package unifi
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func (dst *Account) UnmarshalJSON(b []byte) error {
|
||||
type Alias Account
|
||||
aux := &struct {
|
||||
TunnelType emptyStringInt `json:"tunnel_type"`
|
||||
TunnelMediumType emptyStringInt `json:"tunnel_medium_type"`
|
||||
VLAN emptyStringInt `json:"vlan"`
|
||||
|
||||
*Alias
|
||||
}{
|
||||
Alias: (*Alias)(dst),
|
||||
}
|
||||
|
||||
err := json.Unmarshal(b, &aux)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to unmarshal alias: %w", err)
|
||||
}
|
||||
|
||||
dst.TunnelType = int(aux.TunnelType)
|
||||
dst.TunnelMediumType = int(aux.TunnelMediumType)
|
||||
dst.VLAN = int(aux.VLAN)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dst *Account) MarshalJSON() ([]byte, error) {
|
||||
type Alias Account
|
||||
aux := &struct {
|
||||
|
||||
Reference in New Issue
Block a user