| 1 |
// {{ .Name }} -> {{ .Name.Go }} (struct) |
| 2 |
|
| 3 |
// {{ .Name.Go }} implements the "{{ .Name }}" QMP API type. |
| 4 |
type {{ .Name.Go }} struct { |
| 5 |
{{- range .AllFields API }} |
| 6 |
{{ render . }} |
| 7 |
{{- end }} |
| 8 |
} |
| 9 |
|
| 10 |
{{- if .HasInterfaceField API }} |
| 11 |
// UnmarshalJSON implements json.Unmarshaler. |
| 12 |
func (s *{{ .Name.Go }}) UnmarshalJSON(bs []byte) error { |
| 13 |
v := struct{ |
| 14 |
{{- range .AllFields API }} |
| 15 |
{{- if .Type.InterfaceType API }} |
| 16 |
{{- if .List }} |
| 17 |
{{ abort "rendering of list of union/alternate not supported" }} |
| 18 |
{{- else }} |
| 19 |
{{ .Name.Go }} json.RawMessage `json:"{{ .Name }}"` |
| 20 |
{{- end }} |
| 21 |
{{- else }} |
| 22 |
{{ render . }} |
| 23 |
{{- end }} |
| 24 |
{{- end }} |
| 25 |
}{} |
| 26 |
err := json.Unmarshal(bs, &v) |
| 27 |
if err != nil { |
| 28 |
return err |
| 29 |
} |
| 30 |
|
| 31 |
{{- range .AllFields API }} |
| 32 |
{{- if .Type.InterfaceType API }} |
| 33 |
{{- if .Optional }} |
| 34 |
if len(v.{{ .Name.Go }}) > 0 { |
| 35 |
{{- end }} |
| 36 |
s.{{ .Name.Go }}, err = decode{{ .Type.Go }}(v.{{ .Name.Go }}) |
| 37 |
if err != nil { |
| 38 |
return err |
| 39 |
} |
| 40 |
{{- if .Optional }} |
| 41 |
} else { |
| 42 |
s.{{ .Name.Go }} = nil |
| 43 |
} |
| 44 |
{{- end }} |
| 45 |
{{- else }} |
| 46 |
s.{{ .Name.Go }} = v.{{ .Name.Go }} |
| 47 |
{{- end }} |
| 48 |
{{- end }} |
| 49 |
|
| 50 |
return nil |
| 51 |
} |
| 52 |
{{- end }} |