Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/qmp-gen: generate implementations for events. #135

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions internal/qmp-gen/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,36 @@ type StatusInfo struct {
}
`),
},
{
name: "MEM_UNPLUG_ERROR",
in: []byte(`##
# @MEM_UNPLUG_ERROR
#
# Emitted when memory hot unplug error occurs.
#
# @device: device name
#
# @msg: Informative message
#
# Since: 2.4
##
{
"event": "MEM_UNPLUG_ERROR",
"data": {
"device": "str",
"msg": "str"
}
}`),
out: []byte(`// MEM_UNPLUG_ERROR -> MemUnplugError (event)

// MemUnplugError implements the "MEM_UNPLUG_ERROR" event.
type MemUnplugError struct {
Device string 'json:"device"'
Msg string 'json:"msg"'
}

func (MemUnplugError) isEvent() {}`),
},
}

for _, tt := range tests {
Expand Down
55 changes: 54 additions & 1 deletion internal/qmp-gen/templates/event
Original file line number Diff line number Diff line change
@@ -1 +1,54 @@
// EVENT {{ .Name }}
// {{ .Name }} -> {{ .Name.Go }} (event)

// {{ .Name.Go }} implements the "{{ .Name }}" event.
type {{ .Name.Go }} struct {
{{- range .Data.Fields API }}
{{ render . }}
{{- end }}
}

{{- if (.Data.Fields API).HasInterfaceField API }}
// UnmarshalJSON implements json.Unmarshaler.
func (s *{{ .Name.Go }}) UnmarshalJSON(bs []byte) error {
v := struct{
{{- range .Data.Fields 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 .Data.Fields 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 }}

func ({{ .Name.Go }}) isEvent() {}
27 changes: 25 additions & 2 deletions internal/qmp-gen/templates/main
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,33 @@ package raw
// Generated using `go generate`, do not edit by hand!

import (
"encoding/json"
"fmt"
"encoding/json"
"fmt"

"github.com/digitalocean/go-qemu/qmp"
)

{{ range . }}
{{ render . }}
{{ end }}

func UnmarshalEvent(evt qmp.Event) (Event, error) {
raw, err := json.Marshal(evt.Data)
if err != nil {
return nil, err
}
switch evt.Event {
{{- range . }}
{{- if eq (typeOf .) "event" }}
case "{{ .Name }}":
var ret {{ .Name.Go }}
if err = json.Unmarshal(raw, &ret); err != nil {
return nil, err
}
return ret, nil
{{- end }}
{{- end }}
default:
return nil, fmt.Errorf("unknown event kind %q", evt.Event)
}
}
5 changes: 4 additions & 1 deletion internal/qmp-gen/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func readDefinitions(path string) ([]definition, error) {
default:
return nil, fmt.Errorf("unexpected part of spec file %q: %s", path, string(part))
case 1:
if len(fs) == 1 && part[0] == '{' {
if len(fs) == 1 && part[0] == '{' && !bytes.HasPrefix(part, []byte("{ 'pragma'")) {
return nil, fmt.Errorf("found type definition without a docstring in %q: %s", path, string(part))
}
// This part looks like a non-docstring comment, just skip it.
Expand Down Expand Up @@ -187,6 +187,9 @@ func parse(defs []definition) (map[name]interface{}, error) {
return nil, err
}
ret[v.Name] = v
case m["pragma"] != nil:
// We ignore pragmas, they're there for the benefit of
// qemu's own self-validation.
default:
return nil, fmt.Errorf("unknown definition kind: %q", string(def.JSON))
}
Expand Down
2 changes: 1 addition & 1 deletion qemu/string.gen.go

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

Loading