File: structtype

package info (click to toggle)
golang-github-digitalocean-go-qemu 0.0~git20200529.1b453d0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,264 kB
  • sloc: sh: 34; makefile: 7
file content (52 lines) | stat: -rw-r--r-- 1,223 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// {{ .Name }} -> {{ .Name.Go }} (struct)

// {{ .Name.Go }} implements the "{{ .Name }}" QMP API type.
type {{ .Name.Go }} struct {
{{- range .AllFields API }}
  {{ render . }}
{{- end }}
}

{{- if .HasInterfaceField API }}
// UnmarshalJSON implements json.Unmarshaler.
func (s *{{ .Name.Go }}) UnmarshalJSON(bs []byte) error {
  v := struct{
  {{- range .AllFields API }}
    {{- if .Type.InterfaceType API }}
      {{- if .List }}
        {{ abort "rendering of list of union/alternate not supported" }}
      {{- else }}
        {{ .Name.Go }} json.RawMessage `json:"{{ .Name }}"`
      {{- end }}
    {{- else }}
      {{ render . }}
    {{- end }}
  {{- end }}
  }{}
  err := json.Unmarshal(bs, &v)
  if err != nil {
    return err
  }

  {{- range .AllFields API }}
    {{- if .Type.InterfaceType API }}
      {{- if .Optional }}
        if len(v.{{ .Name.Go }}) > 0 {
      {{- end }}
      s.{{ .Name.Go }}, err = decode{{ .Type.Go }}(v.{{ .Name.Go }})
      if err != nil {
        return err
      }
      {{- if .Optional }}
      } else {
        s.{{ .Name.Go }} = nil
      }
      {{- end }}
    {{- else }}
      s.{{ .Name.Go }} = v.{{ .Name.Go }}
    {{- end }}
  {{- end }}
  
  return nil
}
{{- end }}