Skip to content

Commit

Permalink
handle omitted is true bools
Browse files Browse the repository at this point in the history
  • Loading branch information
paultyng committed Oct 20, 2022
1 parent 84afde7 commit 887fae8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
6 changes: 4 additions & 2 deletions fields/api.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }} {
Expand Down
12 changes: 12 additions & 0 deletions fields/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ type FieldInfo struct {
IsArray bool
Fields map[string]*FieldInfo
CustomUnmarshalType string
CustomUnmarshalFunc string
}

func NewResource(structName string, resourcePath string) *Resource {
Expand Down Expand Up @@ -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") {
Expand Down
4 changes: 2 additions & 2 deletions unifi/network.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 21 additions & 15 deletions unifi/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
Expand Down

0 comments on commit 887fae8

Please sign in to comment.