diff --git a/fields/api.go.tmpl b/fields/api.go.tmpl index bded5ff..4a4fd6a 100644 --- a/fields/api.go.tmpl +++ b/fields/api.go.tmpl @@ -4,9 +4,11 @@ {{ .FieldName }} {{ if .IsArray }}[]{{end}}{{ .FieldType }} `json:"{{ .JSONName }}{{ if .OmitEmpty }},omitempty{{ end }}"` {{ if .FieldValidation }}// {{ .FieldValidation }}{{ end }} {{- end }} {{ define "field-customUnmarshalType" }} {{- if eq .CustomUnmarshalType "" }}{{else}} - {{ .FieldName }} {{ if .IsArray }}[]{{end}}{{ .CustomUnmarshalType }} `json:"{{ .JSONName }}{{ if .OmitEmpty }}{{ end }}"`{{ end }} {{- end }} + {{ .FieldName }} {{ if .IsArray }}[]{{end}}{{ .CustomUnmarshalType }} `json:"{{ .JSONName }}"`{{ end }} {{- end }} {{ define "typecast" }} - {{- if eq .CustomUnmarshalType "" }}{{else}} + {{- if ne .CustomUnmarshalFunc "" }} + dst.{{ .FieldName }}= {{ .CustomUnmarshalFunc }}(aux.{{ .FieldName }}) + {{- else if eq .CustomUnmarshalType "" }}{{else}} {{- if .IsArray }} dst.{{ .FieldName }}= make([]{{ .FieldType }}, len(aux.{{ .FieldName }})) for i, v := range aux.{{ .FieldName }} { diff --git a/fields/main.go b/fields/main.go index c3c4938..1e30270 100644 --- a/fields/main.go +++ b/fields/main.go @@ -108,6 +108,7 @@ type FieldInfo struct { IsArray bool Fields map[string]*FieldInfo CustomUnmarshalType string + CustomUnmarshalFunc string } func NewResource(structName string, resourcePath string) *Resource { @@ -346,6 +347,17 @@ func main() { f.OmitEmpty = true return nil } + case "Network": + resource.FieldProcessor = func(name string, f *FieldInfo) error { + switch name { + case "InternetAccessEnabled", "IntraNetworkAccessEnabled": + if f.FieldType == "bool" { + f.CustomUnmarshalType = "*bool" + f.CustomUnmarshalFunc = "emptyBoolToTrue" + } + } + return nil + } case "SettingGlobalAp": resource.FieldProcessor = func(name string, f *FieldInfo) error { if strings.HasPrefix(name, "6E") { diff --git a/unifi/network.generated.go b/unifi/network.generated.go index 469366b..5484a68 100644 --- a/unifi/network.generated.go +++ b/unifi/network.generated.go @@ -198,8 +198,8 @@ func (dst *Network) UnmarshalJSON(b []byte) error { IPSecIkeDhGroup emptyStringInt `json:"ipsec_ike_dh_group"` IPV6RaPreferredLifetime emptyStringInt `json:"ipv6_ra_preferred_lifetime"` IPV6RaValidLifetime emptyStringInt `json:"ipv6_ra_valid_lifetime"` - InternetAccessEnabled *bool `json:"internet_access_enabled,omitempty"` - IntraNetworkAccessEnabled *bool `json:"intra_network_access_enabled,omitempty"` + InternetAccessEnabled *bool `json:"internet_access_enabled"` + IntraNetworkAccessEnabled *bool `json:"intra_network_access_enabled"` OpenVPNLocalPort emptyStringInt `json:"openvpn_local_port"` OpenVPNRemotePort emptyStringInt `json:"openvpn_remote_port"` PptpcRouteDistance emptyStringInt `json:"pptpc_route_distance"` diff --git a/unifi/network_test.go b/unifi/network_test.go index 93d1f38..bf37adb 100644 --- a/unifi/network_test.go +++ b/unifi/network_test.go @@ -10,69 +10,75 @@ import ( func TestNetworkUnmarshalJSON(t *testing.T) { for n, c := range map[string]struct { - expected unifi.Network + expected func(n *unifi.Network) json string }{ "int vlan": { - expected: unifi.Network{VLAN: 1}, + expected: func(n *unifi.Network) { n.VLAN = 1 }, json: `{ "vlan": 1 }`, }, "string vlan": { - expected: unifi.Network{VLAN: 1}, + expected: func(n *unifi.Network) { n.VLAN = 1 }, json: `{ "vlan": "1" }`, }, "empty string vlan": { - expected: unifi.Network{VLAN: 0}, + expected: func(n *unifi.Network) { n.VLAN = 0 }, json: `{ "vlan": "" }`, }, "int dhcpd_leasetime": { - expected: unifi.Network{DHCPDLeaseTime: 1}, + expected: func(n *unifi.Network) { n.DHCPDLeaseTime = 1 }, json: `{ "dhcpd_leasetime": 1 }`, }, "string dhcpd_leasetime": { - expected: unifi.Network{DHCPDLeaseTime: 1}, + expected: func(n *unifi.Network) { n.DHCPDLeaseTime = 1 }, json: `{ "dhcpd_leasetime": "1" }`, }, "empty string dhcpd_leasetime": { - expected: unifi.Network{DHCPDLeaseTime: 0}, + expected: func(n *unifi.Network) { n.DHCPDLeaseTime = 0 }, json: `{ "dhcpd_leasetime": "" }`, }, "int wan_egress_qos": { - expected: unifi.Network{WANEgressQOS: 1}, + expected: func(n *unifi.Network) { n.WANEgressQOS = 1 }, json: `{ "wan_egress_qos": 1 }`, }, "string wan_egress_qos": { - expected: unifi.Network{WANEgressQOS: 1}, + expected: func(n *unifi.Network) { n.WANEgressQOS = 1 }, json: `{ "wan_egress_qos": "1" }`, }, "empty string wan_egress_qos": { - expected: unifi.Network{WANEgressQOS: 0}, + expected: func(n *unifi.Network) { n.WANEgressQOS = 0 }, json: `{ "wan_egress_qos": "" }`, }, "int wan_vlan": { - expected: unifi.Network{WANVLAN: 1}, + expected: func(n *unifi.Network) { n.WANVLAN = 1 }, json: `{ "wan_vlan": 1 }`, }, "string wan_vlan": { - expected: unifi.Network{WANVLAN: 1}, + expected: func(n *unifi.Network) { n.WANVLAN = 1 }, json: `{ "wan_vlan": "1" }`, }, "empty wan_vlan vlan": { - expected: unifi.Network{WANVLAN: 0}, + expected: func(n *unifi.Network) { n.WANVLAN = 0 }, json: `{ "wan_vlan": "" }`, }, } { t.Run(n, func(t *testing.T) { + // set some non-zero value defaults + expected := unifi.Network{ + InternetAccessEnabled: true, + IntraNetworkAccessEnabled: true, + } + c.expected(&expected) var actual unifi.Network err := json.Unmarshal(([]byte)(c.json), &actual) if err != nil { t.Fatal(err) } - if !reflect.DeepEqual(c.expected, actual) { - t.Fatalf("not equal:\nexpected: %#v\nactual: %#v", c.expected, actual) + if !reflect.DeepEqual(expected, actual) { + t.Fatalf("not equal:\nexpected: %#v\nactual: %#v", expected, actual) } }) }