Skip to content
  • Victor Toso's avatar
    qapi: golang: Add CommandResult type to Go · a6bfd981
    Victor Toso authored
    
    
    This patch adds a struct type in Go that will handle return values
    for QAPI's command types.
    
    The return value of a Command is, encouraged to be, QAPI's complex
    types or an Array of those.
    
    Every Command has a underlying CommandResult. The EmptyCommandReturn
    is for those that don't expect any data e.g: `{ "return": {} }`.
    
    All CommandReturn types implement the CommandResult interface.
    
    Example:
    qapi:
        | { 'command': 'query-sev', 'returns': 'SevInfo',
        |   'if': 'TARGET_I386' }
    
    go:
        | type QuerySevCommandReturn struct {
        |     CommandId string     `json:"id,omitempty"`
        |     Result    *SevInfo   `json:"return"`
        |     Error     *QapiError `json:"error,omitempty"`
        | }
    
    usage:
        | // One can use QuerySevCommandReturn directly or
        | // command's interface GetReturnType() instead.
        |
        | input := `{ "return": { "enabled": true, "api-major" : 0,` +
        |                        `"api-minor" : 0, "build-id" : 0,` +
        |                        `"policy" : 0, "state" : "running",` +
        |                        `"handle" : 1 } } `
        |
        | ret := QuerySevCommandReturn{}
        | err := json.Unmarshal([]byte(input), &ret)
        | if ret.Error != nil {
        |     // Handle command failure {"error": { ...}}
        | } else if ret.Result != nil {
        |     // ret.Result.Enable == true
        | }
    
    Signed-off-by: default avatarVictor Toso <victortoso@redhat.com>
    a6bfd981