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 DpiApp struct {
|
||||
@@ -33,6 +35,37 @@ type DpiApp struct {
|
||||
QOSRateMaxUp int `json:"qos_rate_max_up,omitempty"` // -1|[2-9]|[1-9][0-9]{1,4}|100000|10[0-1][0-9]{3}|102[0-3][0-9]{2}|102400
|
||||
}
|
||||
|
||||
func (dst *DpiApp) UnmarshalJSON(b []byte) error {
|
||||
type Alias DpiApp
|
||||
aux := &struct {
|
||||
Apps []emptyStringInt `json:"apps"`
|
||||
Cats []emptyStringInt `json:"cats"`
|
||||
QOSRateMaxDown emptyStringInt `json:"qos_rate_max_down"`
|
||||
QOSRateMaxUp emptyStringInt `json:"qos_rate_max_up"`
|
||||
|
||||
*Alias
|
||||
}{
|
||||
Alias: (*Alias)(dst),
|
||||
}
|
||||
|
||||
err := json.Unmarshal(b, &aux)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to unmarshal alias: %w", err)
|
||||
}
|
||||
dst.Apps = make([]int, len(aux.Apps))
|
||||
for i, v := range aux.Apps {
|
||||
dst.Apps[i] = int(v)
|
||||
}
|
||||
dst.Cats = make([]int, len(aux.Cats))
|
||||
for i, v := range aux.Cats {
|
||||
dst.Cats[i] = int(v)
|
||||
}
|
||||
dst.QOSRateMaxDown = int(aux.QOSRateMaxDown)
|
||||
dst.QOSRateMaxUp = int(aux.QOSRateMaxUp)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) listDpiApp(ctx context.Context, site string) ([]DpiApp, error) {
|
||||
var respBody struct {
|
||||
Meta meta `json:"meta"`
|
||||
|
||||
Reference in New Issue
Block a user