| 1 |
{{/* |
| 2 |
|
| 3 |
There are very few alternates in the API, but one of them |
| 4 |
(BlockDevRef) is quite tricky, because it references a flat union as |
| 5 |
one of its implementations. So, a lot of this logic is specific to |
| 6 |
supporting flat unions as implementations of an alternate. |
| 7 |
|
| 8 |
*/}} |
| 9 |
|
| 10 |
{{ $basename := .Name.Go }} |
| 11 |
|
| 12 |
// {{ .Name }} -> {{ $basename }} (alternate) |
| 13 |
|
| 14 |
// {{ $basename }} implements the "{{ .Name }}" QMP API type. |
| 15 |
// |
| 16 |
// Can be one of: |
| 17 |
{{- range $suffix, $type := .Options }} |
| 18 |
// - {{ $basename }}{{ $suffix.Go }} |
| 19 |
{{- end }} |
| 20 |
type {{ $basename }} interface { |
| 21 |
is{{ $basename }}() |
| 22 |
} |
| 23 |
|
| 24 |
{{- range $suffix, $type := .Options }} |
| 25 |
{{ $subname := printf "%s%s" $basename $suffix.Go }} |
| 26 |
|
| 27 |
{{- if $type.NullType }} |
| 28 |
// {{ $subname }} is a JSON null type, so it must |
| 29 |
// also implement the isNullable interface. |
| 30 |
type {{ $subname }} struct {} |
| 31 |
func ({{ $subname }}) isNull() bool { return true } |
| 32 |
{{- else }} |
| 33 |
// {{ $subname }} is an implementation of {{ $basename }} |
| 34 |
type {{ $subname }} {{ $type.Go }} |
| 35 |
{{- end }} |
| 36 |
|
| 37 |
{{- if eq (typeOf (index API $type)) "flatunion" }} |
| 38 |
{{ $u := index API $type }} |
| 39 |
{{- range $suffix, $type := $u.Options }} |
| 40 |
func ({{ $u.Name.Go }}{{ $suffix.Go }}) is{{ $basename }}() {} |
| 41 |
{{- end }} |
| 42 |
{{- else }} |
| 43 |
func ({{ $subname }}) is{{ $basename }}() {} |
| 44 |
{{- end }} |
| 45 |
{{- end }} |
| 46 |
|
| 47 |
func decode{{ $basename }}(bs json.RawMessage) ({{ $basename }}, error) { |
| 48 |
{{/* |
| 49 |
Unfortunately, we have to range through the types three times in order |
| 50 |
for us to do the type discovery via unmarshalling in the correct order |
| 51 |
*/}} |
| 52 |
{{- range $suffix, $type := .Options }} |
| 53 |
{{- if $type.NullType }} |
| 54 |
// Always try unmarshalling for nil first if it's an option |
| 55 |
// because other types could unmarshal successfully in the case |
| 56 |
// where a Null json type was provided. |
| 57 |
var {{ $suffix }} *int |
| 58 |
if err := json.Unmarshal([]byte(bs), &{{ $suffix }}); err == nil { |
| 59 |
if {{ $suffix }} == nil { |
| 60 |
return {{ printf "%s%s" $basename $suffix.Go }}{}, nil |
| 61 |
} |
| 62 |
} |
| 63 |
{{- end }} |
| 64 |
{{- end }} |
| 65 |
{{- range $suffix, $type := .Options }} |
| 66 |
{{- if $type.SimpleType }} |
| 67 |
var {{ $suffix }} {{ printf "%s%s" $basename $suffix.Go }} |
| 68 |
if err := json.Unmarshal([]byte(bs), &{{ $suffix }}); err == nil { |
| 69 |
return {{ $suffix }}, nil |
| 70 |
} |
| 71 |
{{- end }} |
| 72 |
{{- end }} |
| 73 |
{{- range $suffix, $type := .Options }} |
| 74 |
{{- if not $type.NullType }} |
| 75 |
{{- if not $type.SimpleType }} |
| 76 |
{{ $subtype := index API $type }} |
| 77 |
{{- if eq (typeOf $subtype) "flatunion" }} |
| 78 |
if {{ $suffix }}, err := decode{{ $type.Go }}([]byte(bs)); err == nil { |
| 79 |
switch impl := {{ $suffix }}.(type) { |
| 80 |
{{- range $suffix, $type := $subtype.Options }} |
| 81 |
case {{ $subtype.Name.Go }}{{ $suffix.Go }}: |
| 82 |
return impl, nil |
| 83 |
{{- end }} |
| 84 |
} |
| 85 |
} |
| 86 |
{{- else }} |
| 87 |
var {{ $suffix }} {{ printf "%s%s" $basename $suffix.Go }} |
| 88 |
if err := json.Unmarshal([]byte(bs), &{{ $suffix }}); err == nil { |
| 89 |
return {{ $suffix }}, nil |
| 90 |
} |
| 91 |
{{- end }} |
| 92 |
{{- end }} |
| 93 |
{{- end }} |
| 94 |
{{- end }} |
| 95 |
return nil, fmt.Errorf("unable to decode %q as a {{ $basename }}", string(bs)) |
| 96 |
} |