handle omitted is true bools
This commit is contained in:
@@ -4,9 +4,11 @@
|
|||||||
{{ .FieldName }} {{ if .IsArray }}[]{{end}}{{ .FieldType }} `json:"{{ .JSONName }}{{ if .OmitEmpty }},omitempty{{ end }}"` {{ if .FieldValidation }}// {{ .FieldValidation }}{{ end }} {{- end }}
|
{{ .FieldName }} {{ if .IsArray }}[]{{end}}{{ .FieldType }} `json:"{{ .JSONName }}{{ if .OmitEmpty }},omitempty{{ end }}"` {{ if .FieldValidation }}// {{ .FieldValidation }}{{ end }} {{- end }}
|
||||||
{{ define "field-customUnmarshalType" }}
|
{{ define "field-customUnmarshalType" }}
|
||||||
{{- if eq .CustomUnmarshalType "" }}{{else}}
|
{{- 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" }}
|
{{ define "typecast" }}
|
||||||
{{- if eq .CustomUnmarshalType "" }}{{else}}
|
{{- if ne .CustomUnmarshalFunc "" }}
|
||||||
|
dst.{{ .FieldName }}= {{ .CustomUnmarshalFunc }}(aux.{{ .FieldName }})
|
||||||
|
{{- else if eq .CustomUnmarshalType "" }}{{else}}
|
||||||
{{- if .IsArray }}
|
{{- if .IsArray }}
|
||||||
dst.{{ .FieldName }}= make([]{{ .FieldType }}, len(aux.{{ .FieldName }}))
|
dst.{{ .FieldName }}= make([]{{ .FieldType }}, len(aux.{{ .FieldName }}))
|
||||||
for i, v := range aux.{{ .FieldName }} {
|
for i, v := range aux.{{ .FieldName }} {
|
||||||
|
|||||||
@@ -108,6 +108,7 @@ type FieldInfo struct {
|
|||||||
IsArray bool
|
IsArray bool
|
||||||
Fields map[string]*FieldInfo
|
Fields map[string]*FieldInfo
|
||||||
CustomUnmarshalType string
|
CustomUnmarshalType string
|
||||||
|
CustomUnmarshalFunc string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewResource(structName string, resourcePath string) *Resource {
|
func NewResource(structName string, resourcePath string) *Resource {
|
||||||
@@ -346,6 +347,17 @@ func main() {
|
|||||||
f.OmitEmpty = true
|
f.OmitEmpty = true
|
||||||
return nil
|
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":
|
case "SettingGlobalAp":
|
||||||
resource.FieldProcessor = func(name string, f *FieldInfo) error {
|
resource.FieldProcessor = func(name string, f *FieldInfo) error {
|
||||||
if strings.HasPrefix(name, "6E") {
|
if strings.HasPrefix(name, "6E") {
|
||||||
|
|||||||
4
unifi/network.generated.go
generated
4
unifi/network.generated.go
generated
@@ -198,8 +198,8 @@ func (dst *Network) UnmarshalJSON(b []byte) error {
|
|||||||
IPSecIkeDhGroup emptyStringInt `json:"ipsec_ike_dh_group"`
|
IPSecIkeDhGroup emptyStringInt `json:"ipsec_ike_dh_group"`
|
||||||
IPV6RaPreferredLifetime emptyStringInt `json:"ipv6_ra_preferred_lifetime"`
|
IPV6RaPreferredLifetime emptyStringInt `json:"ipv6_ra_preferred_lifetime"`
|
||||||
IPV6RaValidLifetime emptyStringInt `json:"ipv6_ra_valid_lifetime"`
|
IPV6RaValidLifetime emptyStringInt `json:"ipv6_ra_valid_lifetime"`
|
||||||
InternetAccessEnabled *bool `json:"internet_access_enabled,omitempty"`
|
InternetAccessEnabled *bool `json:"internet_access_enabled"`
|
||||||
IntraNetworkAccessEnabled *bool `json:"intra_network_access_enabled,omitempty"`
|
IntraNetworkAccessEnabled *bool `json:"intra_network_access_enabled"`
|
||||||
OpenVPNLocalPort emptyStringInt `json:"openvpn_local_port"`
|
OpenVPNLocalPort emptyStringInt `json:"openvpn_local_port"`
|
||||||
OpenVPNRemotePort emptyStringInt `json:"openvpn_remote_port"`
|
OpenVPNRemotePort emptyStringInt `json:"openvpn_remote_port"`
|
||||||
PptpcRouteDistance emptyStringInt `json:"pptpc_route_distance"`
|
PptpcRouteDistance emptyStringInt `json:"pptpc_route_distance"`
|
||||||
|
|||||||
@@ -10,69 +10,75 @@ import (
|
|||||||
|
|
||||||
func TestNetworkUnmarshalJSON(t *testing.T) {
|
func TestNetworkUnmarshalJSON(t *testing.T) {
|
||||||
for n, c := range map[string]struct {
|
for n, c := range map[string]struct {
|
||||||
expected unifi.Network
|
expected func(n *unifi.Network)
|
||||||
json string
|
json string
|
||||||
}{
|
}{
|
||||||
"int vlan": {
|
"int vlan": {
|
||||||
expected: unifi.Network{VLAN: 1},
|
expected: func(n *unifi.Network) { n.VLAN = 1 },
|
||||||
json: `{ "vlan": 1 }`,
|
json: `{ "vlan": 1 }`,
|
||||||
},
|
},
|
||||||
"string vlan": {
|
"string vlan": {
|
||||||
expected: unifi.Network{VLAN: 1},
|
expected: func(n *unifi.Network) { n.VLAN = 1 },
|
||||||
json: `{ "vlan": "1" }`,
|
json: `{ "vlan": "1" }`,
|
||||||
},
|
},
|
||||||
"empty string vlan": {
|
"empty string vlan": {
|
||||||
expected: unifi.Network{VLAN: 0},
|
expected: func(n *unifi.Network) { n.VLAN = 0 },
|
||||||
json: `{ "vlan": "" }`,
|
json: `{ "vlan": "" }`,
|
||||||
},
|
},
|
||||||
|
|
||||||
"int dhcpd_leasetime": {
|
"int dhcpd_leasetime": {
|
||||||
expected: unifi.Network{DHCPDLeaseTime: 1},
|
expected: func(n *unifi.Network) { n.DHCPDLeaseTime = 1 },
|
||||||
json: `{ "dhcpd_leasetime": 1 }`,
|
json: `{ "dhcpd_leasetime": 1 }`,
|
||||||
},
|
},
|
||||||
"string dhcpd_leasetime": {
|
"string dhcpd_leasetime": {
|
||||||
expected: unifi.Network{DHCPDLeaseTime: 1},
|
expected: func(n *unifi.Network) { n.DHCPDLeaseTime = 1 },
|
||||||
json: `{ "dhcpd_leasetime": "1" }`,
|
json: `{ "dhcpd_leasetime": "1" }`,
|
||||||
},
|
},
|
||||||
"empty string dhcpd_leasetime": {
|
"empty string dhcpd_leasetime": {
|
||||||
expected: unifi.Network{DHCPDLeaseTime: 0},
|
expected: func(n *unifi.Network) { n.DHCPDLeaseTime = 0 },
|
||||||
json: `{ "dhcpd_leasetime": "" }`,
|
json: `{ "dhcpd_leasetime": "" }`,
|
||||||
},
|
},
|
||||||
|
|
||||||
"int wan_egress_qos": {
|
"int wan_egress_qos": {
|
||||||
expected: unifi.Network{WANEgressQOS: 1},
|
expected: func(n *unifi.Network) { n.WANEgressQOS = 1 },
|
||||||
json: `{ "wan_egress_qos": 1 }`,
|
json: `{ "wan_egress_qos": 1 }`,
|
||||||
},
|
},
|
||||||
"string wan_egress_qos": {
|
"string wan_egress_qos": {
|
||||||
expected: unifi.Network{WANEgressQOS: 1},
|
expected: func(n *unifi.Network) { n.WANEgressQOS = 1 },
|
||||||
json: `{ "wan_egress_qos": "1" }`,
|
json: `{ "wan_egress_qos": "1" }`,
|
||||||
},
|
},
|
||||||
"empty string wan_egress_qos": {
|
"empty string wan_egress_qos": {
|
||||||
expected: unifi.Network{WANEgressQOS: 0},
|
expected: func(n *unifi.Network) { n.WANEgressQOS = 0 },
|
||||||
json: `{ "wan_egress_qos": "" }`,
|
json: `{ "wan_egress_qos": "" }`,
|
||||||
},
|
},
|
||||||
|
|
||||||
"int wan_vlan": {
|
"int wan_vlan": {
|
||||||
expected: unifi.Network{WANVLAN: 1},
|
expected: func(n *unifi.Network) { n.WANVLAN = 1 },
|
||||||
json: `{ "wan_vlan": 1 }`,
|
json: `{ "wan_vlan": 1 }`,
|
||||||
},
|
},
|
||||||
"string wan_vlan": {
|
"string wan_vlan": {
|
||||||
expected: unifi.Network{WANVLAN: 1},
|
expected: func(n *unifi.Network) { n.WANVLAN = 1 },
|
||||||
json: `{ "wan_vlan": "1" }`,
|
json: `{ "wan_vlan": "1" }`,
|
||||||
},
|
},
|
||||||
"empty wan_vlan vlan": {
|
"empty wan_vlan vlan": {
|
||||||
expected: unifi.Network{WANVLAN: 0},
|
expected: func(n *unifi.Network) { n.WANVLAN = 0 },
|
||||||
json: `{ "wan_vlan": "" }`,
|
json: `{ "wan_vlan": "" }`,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(n, func(t *testing.T) {
|
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
|
var actual unifi.Network
|
||||||
err := json.Unmarshal(([]byte)(c.json), &actual)
|
err := json.Unmarshal(([]byte)(c.json), &actual)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(c.expected, actual) {
|
if !reflect.DeepEqual(expected, actual) {
|
||||||
t.Fatalf("not equal:\nexpected: %#v\nactual: %#v", c.expected, actual)
|
t.Fatalf("not equal:\nexpected: %#v\nactual: %#v", expected, actual)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user