Skip to content

Commit

Permalink
Generate UnmarshalJSON to handle emptyStringInt types
Browse files Browse the repository at this point in the history
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
  • Loading branch information
T4cC0re authored and paultyng committed Jan 3, 2021
1 parent c5ff8c8 commit 4aed7d7
Show file tree
Hide file tree
Showing 67 changed files with 2,298 additions and 155 deletions.
38 changes: 37 additions & 1 deletion fields/api.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

{{ define "field" }}
{{ .FieldName }} {{ if .IsArray }}[]{{end}}{{ .FieldType }} `json:"{{ .JSONName }}{{ if .OmitEmpty }},omitempty{{ end }}"` {{ if .FieldValidation }}// {{ .FieldValidation }}{{ end }} {{- end }}
{{ define "field-emptyStringInt" }}
{{- if ne .FieldType "int" }}{{else}}
{{ .FieldName }} {{ if .IsArray }}[]{{end}}emptyStringInt `json:"{{ .JSONName }}{{ if .OmitEmpty }}{{ end }}"`{{ end }} {{- end }}
{{ define "typecast" }}
{{- if eq .FieldType "int" }}{{- if .IsArray }}
dst.{{ .FieldName }}= make([]int, len(aux.{{ .FieldName }}))
for i, v := range aux.{{ .FieldName }} {
dst.{{ .FieldName }}[i] = int(v)
}
{{- else }}
dst.{{ .FieldName }} = int(aux.{{ .FieldName }})
{{- end }}{{- end }}{{- end }}
{{ define "field-embed" }}
{{ .FieldName }} {{ if .IsArray }}[]{{end}}{{ if not .Fields }}{{ .FieldType }}{{ else }}struct {
{{ range $fk, $fv := .Fields }}{{ if not $fv }}
Expand All @@ -14,13 +26,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
)

{{ if embedTypes -}}
Expand All @@ -36,6 +50,28 @@ type {{ $k }} struct {
{{ range $fk, $fv := $v.Fields }}{{ if not $fv }}
{{ else }}{{- template "field" $fv }}{{ end }}{{ end }}
}

func (dst *{{ $k }}) UnmarshalJSON(b []byte) error {
type Alias {{ $k }}
aux := &struct {
{{- range $fk, $fv := $v.Fields }}{{ if not $fv }}
{{- else }}{{- template "field-emptyStringInt" $fv }}{{ end }}{{- end }}

*Alias
}{
Alias: (*Alias)(dst),
}

err := json.Unmarshal(b, &aux)
if err != nil {
return fmt.Errorf("unable to unmarshal alias: %w", err)
}

{{- range $fk, $fv := $v.Fields }}{{ if not $fv }}
{{- else }}{{- template "typecast" $fv }}{{ end }}{{ end }}

return nil
}
{{ end }}
{{- end -}}

Expand Down
27 changes: 26 additions & 1 deletion unifi/account.generated.go

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

25 changes: 0 additions & 25 deletions unifi/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 19 additions & 1 deletion unifi/broadcast_group.generated.go

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

116 changes: 115 additions & 1 deletion unifi/channel_plan.generated.go

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

Loading

0 comments on commit 4aed7d7

Please sign in to comment.